daniyal-10 commited on
Commit
32ac7ec
Β·
verified Β·
1 Parent(s): 5c22f6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -1,22 +1,48 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2")
 
 
 
 
 
 
 
5
  history = []
6
 
7
  def respond(user_input):
 
 
8
  history.append(f"You: {user_input}")
9
  prompt = "\n".join(history)
10
- response = chatbot(prompt, max_new_tokens=200)[0]['generated_text']
11
  history.append(f"AI: {response}")
12
- return response
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- iface = gr.Interface(
15
- fn=respond,
16
- inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
17
- outputs="text",
18
- title="Study Helper AI",
19
- description="Ask questions and I’ll help you study! Model: Mistral-7B-Instruct-v0.2 (Apache 2.0 License)."
20
- )
 
 
 
 
21
 
22
- iface.launch()
 
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()