Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
history = []
|
| 6 |
|
| 7 |
def respond(user_input):
|
|
|
|
|
|
|
| 8 |
history.append(f"You: {user_input}")
|
| 9 |
prompt = "\n".join(history)
|
| 10 |
-
response = chatbot(prompt
|
| 11 |
history.append(f"AI: {response}")
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# --- Load the AI Model ---
|
| 6 |
+
model_name = "TheBloke/wizardLM-7B-HF"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
|
| 9 |
+
|
| 10 |
+
chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=200)
|
| 11 |
+
|
| 12 |
+
# --- Conversation Memory ---
|
| 13 |
history = []
|
| 14 |
|
| 15 |
def respond(user_input):
|
| 16 |
+
if not user_input.strip():
|
| 17 |
+
return ""
|
| 18 |
history.append(f"You: {user_input}")
|
| 19 |
prompt = "\n".join(history)
|
| 20 |
+
response = chatbot(prompt)[0]['generated_text']
|
| 21 |
history.append(f"AI: {response}")
|
| 22 |
+
|
| 23 |
+
# Format chat history
|
| 24 |
+
chat_text = ""
|
| 25 |
+
for msg in history:
|
| 26 |
+
if msg.startswith("You:"):
|
| 27 |
+
chat_text += f"<p style='color:blue;'><b>{msg}</b></p>"
|
| 28 |
+
else:
|
| 29 |
+
chat_text += f"<p style='color:green;'><b>{msg}</b></p>"
|
| 30 |
+
return chat_text
|
| 31 |
+
|
| 32 |
+
def clear_chat():
|
| 33 |
+
history.clear()
|
| 34 |
+
return ""
|
| 35 |
|
| 36 |
+
# --- Gradio Interface ---
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# Study Helper AI")
|
| 39 |
+
gr.Markdown("Ask me anything! Model: WizardLM-7B-HF (Open Access).")
|
| 40 |
+
chatbox = gr.HTML()
|
| 41 |
+
user_input = gr.Textbox(lines=2, placeholder="Type your question here...")
|
| 42 |
+
send_btn = gr.Button("Send")
|
| 43 |
+
clear_btn = gr.Button("Clear Chat")
|
| 44 |
+
|
| 45 |
+
send_btn.click(respond, inputs=user_input, outputs=chatbox)
|
| 46 |
+
clear_btn.click(clear_chat, outputs=chatbox)
|
| 47 |
|
| 48 |
+
demo.launch()
|