ilsa15 commited on
Commit
777b548
·
verified ·
1 Parent(s): fd48d70

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import openai
3
+ import gradio as gr
4
+ import os
5
+
6
+ # Use Hugging Face secrets (you'll set this on HF later)
7
+ openai.api_key = os.environ.get("GROQ_API_KEY")
8
+ openai.api_base = "https://api.groq.com/openai/v1"
9
+
10
+ def tutor_chatbot(subject, question, chat_history=[]):
11
+ try:
12
+ messages = [
13
+ {"role": "system", "content": f"You are a helpful and expert tutor in {subject}."},
14
+ {"role": "user", "content": question}
15
+ ]
16
+
17
+ response = openai.ChatCompletion.create(
18
+ model="llama3-8b-8192", # or gemma-7b-it
19
+ messages=messages,
20
+ temperature=0.5,
21
+ max_tokens=800,
22
+ )
23
+ answer = response.choices[0].message.content
24
+ chat_history.append((question, answer))
25
+ return chat_history
26
+ except Exception as e:
27
+ return chat_history + [("Error", str(e))]
28
+
29
+ subjects = ["Math", "Physics", "Biology", "CSS Exam", "Computer Science", "History"]
30
+
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("## 📚 AI Educational Tutor Chatbot (Groq API)")
33
+ subject = gr.Dropdown(choices=subjects, label="Choose Subject")
34
+ chatbot = gr.Chatbot()
35
+ question = gr.Textbox(label="Your Question")
36
+ state = gr.State([])
37
+
38
+ submit_btn = gr.Button("Get Answer")
39
+ submit_btn.click(fn=tutor_chatbot, inputs=[subject, question, state], outputs=[chatbot])
40
+
41
+ demo.launch()