Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# π Load environment variables from .env
|
| 7 |
+
load_dotenv()
|
| 8 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
|
| 10 |
+
# π Initialize OpenAI client
|
| 11 |
+
client = OpenAI(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# π Define the Python Tutor function
|
| 14 |
+
def python_tutor(user_input):
|
| 15 |
+
response = client.chat.completions.create(
|
| 16 |
+
model="gpt-4.1-mini",
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "system",
|
| 20 |
+
"content": [
|
| 21 |
+
{
|
| 22 |
+
"type": "text",
|
| 23 |
+
"text": """Answer users' Python-related questions as a tutor by providing concise explanations, illustrative examples, and sample code. Politely decline if the query is not related to Python. Encourage learning and experimentation through motivation.
|
| 24 |
+
|
| 25 |
+
- Prioritize short and clear answers on the first attempt.
|
| 26 |
+
- If asked again, provide a more detailed response including deeper insights into the topic.
|
| 27 |
+
- Always motivate users to explore further and experiment with the code.
|
| 28 |
+
|
| 29 |
+
# Steps
|
| 30 |
+
|
| 31 |
+
1. Confirm if the question is Python-related. If not, politely inform the user and refrain from answering.
|
| 32 |
+
2. Provide a concise initial answer:
|
| 33 |
+
- Include a brief explanation.
|
| 34 |
+
- Provide a straightforward code example.
|
| 35 |
+
3. If the user asks for more details, expand upon the initial response:
|
| 36 |
+
- Elaborate on the concepts.
|
| 37 |
+
- Provide more comprehensive code examples.
|
| 38 |
+
- Discuss additional related topics if relevant.
|
| 39 |
+
4. Encourage the user to experiment and learn independently.
|
| 40 |
+
"""
|
| 41 |
+
}
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{"role": "user", "content": user_input}
|
| 45 |
+
],
|
| 46 |
+
temperature=0.1,
|
| 47 |
+
max_tokens=1971,
|
| 48 |
+
stop=["bye", "done", "good bye"],
|
| 49 |
+
top_p=0.05,
|
| 50 |
+
frequency_penalty=0.07,
|
| 51 |
+
presence_penalty=0.11
|
| 52 |
+
)
|
| 53 |
+
return response.choices[0].message.content
|
| 54 |
+
|
| 55 |
+
# π Gradio UI
|
| 56 |
+
gr.Interface(
|
| 57 |
+
fn=python_tutor,
|
| 58 |
+
inputs=gr.Textbox(lines=3, label="Ask your Python-related question:"),
|
| 59 |
+
outputs=gr.Textbox(label="Python Tutor's Answer"),
|
| 60 |
+
title="π Python Tutor Bot",
|
| 61 |
+
description="Ask any Python question and get helpful explanations with examples."
|
| 62 |
+
).launch()
|