CoolFace
Apppublic

ilsa15/educational_chatbot

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py46 linesDownload Raw Back to root
1# app.py2import openai3import gradio as gr4import os5 6# โœ… Use the environment variable correctly7openai.api_key = os.environ.get("OPENAI_API_KEY")8openai.api_base = "https://api.groq.com/openai/v1"9 10# Tutor chatbot function11def tutor_chatbot(subject, question, chat_history=[]):12    try:13        messages = [14            {"role": "system", "content": f"You are a helpful and expert tutor in {subject}."},15            {"role": "user", "content": question}16        ]17 18        response = openai.ChatCompletion.create(19            model="llama3-8b-8192",  # Or "gemma-7b-it"20            messages=messages,21            temperature=0.5,22            max_tokens=800,23        )24        answer = response.choices[0].message.content25        chat_history.append((question, answer))26        return chat_history27    except Exception as e:28        return chat_history + [("Error", str(e))]29 30# List of available subjects31subjects = ["Math", "Physics", "Biology", "CSS Exam", "Computer Science", "History"]32 33# Gradio UI34with gr.Blocks() as demo:35    gr.Markdown("## ๐Ÿ“š AI Educational Tutor Chatbot (Groq API)")36    37    subject = gr.Dropdown(choices=subjects, label="Choose Subject")38    chatbot = gr.Chatbot()39    question = gr.Textbox(label="Ask your question:")40    state = gr.State([])41 42    submit_btn = gr.Button("Get Answer")43    submit_btn.click(fn=tutor_chatbot, inputs=[subject, question, state], outputs=[chatbot])44 45demo.launch()46