Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# Paste your OpenAI key here
|
| 5 |
+
openai.api_key = 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
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def chat_with_ai(message, history):
|
| 9 |
+
history = history or []
|
| 10 |
+
conversation = [{"role": "system", "content": "You are a friendly, helpful student buddy and tutor. Keep explanations clear and engaging, as if you are a supportive classmate."}]
|
| 11 |
+
|
| 12 |
+
for user, bot in history:
|
| 13 |
+
conversation.append({"role": "user", "content": user})
|
| 14 |
+
conversation.append({"role": "assistant", "content": bot})
|
| 15 |
+
conversation.append({"role": "user", "content": message})
|
| 16 |
+
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-4o-mini", # Fast & affordable
|
| 19 |
+
messages=conversation
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
bot_reply = response["choices"][0]["message"]["content"]
|
| 23 |
+
history.append((message, bot_reply))
|
| 24 |
+
return history, history
|
| 25 |
+
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# 🎓 SmartMate AI\nAsk me anything about your courses!")
|
| 28 |
+
chatbot = gr.Chatbot()
|
| 29 |
+
state = gr.State()
|
| 30 |
+
with gr.Row():
|
| 31 |
+
msg = gr.Textbox(placeholder="Type your question here...")
|
| 32 |
+
submit = gr.Button("Send")
|
| 33 |
+
submit.click(chat_with_ai, inputs=[msg, state], outputs=[chatbot, state])
|
| 34 |
+
|
| 35 |
+
demo.launch()
|