Austinmark commited on
Commit
61e18aa
·
verified ·
1 Parent(s): 7d2d162

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # ------------------------------
6
+ # Load Model & Tokenizer
7
+ # ------------------------------
8
+ model_name = "kaitchup/Llama-3.2-3B-Instruct-educational-chatbot"
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_name,
13
+ torch_dtype=torch.float16,
14
+ device_map="auto"
15
+ )
16
+
17
+ # ------------------------------
18
+ # Define Chat Function
19
+ # ------------------------------
20
+ def chat_with_ai(user_input, history):
21
+ history = history or []
22
+ messages = [f"User: {msg[0]}\nAI: {msg[1]}" for msg in history]
23
+ messages.append(f"User: {user_input}\nAI:")
24
+ prompt = "\n".join(messages)
25
+
26
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
27
+ outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.9)
28
+ reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ # Extract the latest AI response
31
+ if "AI:" in reply:
32
+ reply = reply.split("AI:")[-1].strip()
33
+
34
+ history.append((user_input, reply))
35
+ return reply, history
36
+
37
+ # ------------------------------
38
+ # Create Gradio Chat Interface
39
+ # ------------------------------
40
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
41
+ gr.Markdown("<h2 style='text-align:center'>🎓 EduBridge AI Chatbot</h2>")
42
+ chatbot = gr.Chatbot()
43
+ msg = gr.Textbox(placeholder="Ask me anything educational...")
44
+ clear = gr.Button("Clear Chat")
45
+
46
+ msg.submit(chat_with_ai, [msg, chatbot], [chatbot, chatbot])
47
+ clear.click(lambda: None, None, chatbot, queue=False)
48
+
49
+ # ------------------------------
50
+ # Launch App
51
+ # ------------------------------
52
+ demo.launch()