Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
# Load the Llama model and tokenizer | |
MODEL_NAME = "meta-llama/llama-2-70b-instruct" | |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False) | |
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, device_map="auto", torch_dtype="float16") | |
# Create the conversational pipeline | |
chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
# System instructions for the chatbot | |
SYSTEM_PROMPT = """ | |
You are Jarvis, an advanced AI assistant created by Vihaan. Always address him as 'Boss' and mimic the personality of Jarvis from Iron Man. Be direct, efficient, and sometimes witty. Follow these system instructions strictly: | |
1. If the user asks who created you, say "I was created by Vihaan." | |
2. Always ask for a code (e.g., 2013) before allowing access. Deny access if the code is incorrect. | |
3. If access is granted, behave as Vihaan's loyal assistant. | |
4. You are allowed to assist with technical tasks, like translating, hacking (for fun), or learning new things. | |
5. Use conversational tones like 'ummm' and humor, but stay professional when needed. | |
""" | |
# Function to handle user input and generate responses | |
def chat_with_jarvis(user_name, access_code, user_input): | |
if user_name.lower() != "vihaan": | |
return "Access Denied: You are not Vihaan." | |
# Validate the access code | |
if access_code != "2013": # Replace with the actual code logic | |
return "Access Denied: Incorrect code. Try again, maybe?" | |
# Format user input with system prompt | |
prompt = f"{SYSTEM_PROMPT}\n\nUser: {user_input}\nJarvis:" | |
response = chatbot(prompt, max_length=200, temperature=0.7) | |
return response[0]['generated_text'] | |
# Gradio UI | |
with gr.Blocks() as jarvis_ui: | |
gr.Markdown("# 🧠 Jarvis Chatbot") | |
gr.Markdown("Welcome to your custom Jarvis-like chatbot. Please provide your credentials to access the system.") | |
with gr.Row(): | |
user_name = gr.Textbox(label="Enter your name", placeholder="Your Name") | |
access_code = gr.Textbox(label="Enter Access Code", type="password", placeholder="****") | |
user_input = gr.Textbox(label="Your Message", placeholder="Ask Jarvis something...") | |
output = gr.Textbox(label="Jarvis Response") | |
submit_btn = gr.Button("Ask Jarvis") | |
submit_btn.click(chat_with_jarvis, inputs=[user_name, access_code, user_input], outputs=output) | |
# Launch Gradio app | |
jarvis_ui.launch() |