Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Get API key
|
| 10 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
+
# Create OpenAI client
|
| 13 |
+
client = OpenAI(api_key=api_key)
|
| 14 |
+
|
| 15 |
+
# Chatbot function
|
| 16 |
+
def tutor_bot(user_input):
|
| 17 |
+
|
| 18 |
+
response = client.chat.completions.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
messages=[
|
| 21 |
+
{
|
| 22 |
+
"role": "system",
|
| 23 |
+
"content": (
|
| 24 |
+
"You are a beginner-friendly Python tutor bot. "
|
| 25 |
+
"Explain concepts simply, provide examples, "
|
| 26 |
+
"help debug code, and encourage learning."
|
| 27 |
+
)
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": user_input
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
temperature=0.3,
|
| 35 |
+
max_tokens=500
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return response.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
# Gradio UI
|
| 41 |
+
demo = gr.Interface(
|
| 42 |
+
fn=tutor_bot,
|
| 43 |
+
inputs=gr.Textbox(
|
| 44 |
+
lines=2,
|
| 45 |
+
placeholder="Ask a Python question..."
|
| 46 |
+
),
|
| 47 |
+
outputs="text",
|
| 48 |
+
title="💡 Python Tutor Bot",
|
| 49 |
+
description="Ask me any Python-related question."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Launch app
|
| 53 |
+
demo.launch()
|