Spaces:
Sleeping
Sleeping
File size: 1,427 Bytes
777b548 8eebb1a 777b548 8eebb1a 777b548 8eebb1a 777b548 8eebb1a 777b548 8eebb1a 777b548 8eebb1a 777b548 8eebb1a 777b548 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# app.py
import openai
import gradio as gr
import os
# ✅ Use the environment variable correctly
openai.api_key = os.environ.get("OPENAI_API_KEY")
openai.api_base = "https://api.groq.com/openai/v1"
# Tutor chatbot function
def tutor_chatbot(subject, question, chat_history=[]):
try:
messages = [
{"role": "system", "content": f"You are a helpful and expert tutor in {subject}."},
{"role": "user", "content": question}
]
response = openai.ChatCompletion.create(
model="llama3-8b-8192", # Or "gemma-7b-it"
messages=messages,
temperature=0.5,
max_tokens=800,
)
answer = response.choices[0].message.content
chat_history.append((question, answer))
return chat_history
except Exception as e:
return chat_history + [("Error", str(e))]
# List of available subjects
subjects = ["Math", "Physics", "Biology", "CSS Exam", "Computer Science", "History"]
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## 📚 AI Educational Tutor Chatbot (Groq API)")
subject = gr.Dropdown(choices=subjects, label="Choose Subject")
chatbot = gr.Chatbot()
question = gr.Textbox(label="Ask your question:")
state = gr.State([])
submit_btn = gr.Button("Get Answer")
submit_btn.click(fn=tutor_chatbot, inputs=[subject, question, state], outputs=[chatbot])
demo.launch()
|