# 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()