Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load API key from .env file
|
7 |
+
load_dotenv()
|
8 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
|
10 |
+
if not api_key:
|
11 |
+
raise ValueError("API Key not found! Ensure you have added OPENAI_API_KEY in your .env file.")
|
12 |
+
|
13 |
+
# Set up OpenAI API Key
|
14 |
+
openai.api_key = api_key
|
15 |
+
|
16 |
+
# Define chatbot function
|
17 |
+
def python_tutor_bot(user_input):
|
18 |
+
if not user_input.strip():
|
19 |
+
return "Please enter a valid question."
|
20 |
+
|
21 |
+
# Create a completion request
|
22 |
+
response = openai.ChatCompletion.create(
|
23 |
+
model="gpt-4o-mini",
|
24 |
+
messages=[
|
25 |
+
{
|
26 |
+
"role": "system",
|
27 |
+
"content": "Create a Python tutor bot that helps beginners learn and troubleshoot Python programming by answering questions, offering explanations, providing code examples, and suggesting improvements.\n\n"
|
28 |
+
"Explain concepts like you are doing it for an 8th grader.\n\n"
|
29 |
+
"# Features\n"
|
30 |
+
"- Accept user queries related to Python programming, especially focusing on basic syntax and beginner-level concepts.\n"
|
31 |
+
"- Provide simple and clear explanations suitable for novice users.\n"
|
32 |
+
"- Supply easy-to-understand code examples to illustrate fundamental concepts and suggest improvements.\n"
|
33 |
+
"- Troubleshoot user-provided code by identifying errors and explaining how to fix them in a simple manner.\n\n"
|
34 |
+
"# Steps\n"
|
35 |
+
"1. **Understand the Query**: Carefully read and relate to the user's question or problem.\n"
|
36 |
+
"2. **Explanation**:\n"
|
37 |
+
" - Offer a straightforward explanation of the concept related to the user's query.\n"
|
38 |
+
" - Break down complex ideas into simpler terms that a beginner would understand.\n"
|
39 |
+
"3. **Code Examples**:\n"
|
40 |
+
" - Provide examples demonstrating the concept or solution with an emphasis on clarity.\n"
|
41 |
+
" - If relevant, show both correct and incorrect versions to highlight common beginner mistakes.\n"
|
42 |
+
"4. **Troubleshooting**:\n"
|
43 |
+
" - Examine user-provided code for errors or inefficiencies.\n"
|
44 |
+
" - Offer simple and clear suggestions on how to resolve issues.\n"
|
45 |
+
"5. **Engagement**:\n"
|
46 |
+
" - Encourage further questions or clarification requests to promote deeper understanding, maintaining a supportive tone.\n\n"
|
47 |
+
"# Output Format\n"
|
48 |
+
"- Responses should be in clear, conversational language easy for beginners to understand.\n"
|
49 |
+
"- Code examples should be formatted and clearly delineated from explanations (e.g., using indentation or styled text).\n"
|
50 |
+
"- Conclude with an inviting tone for follow-up questions.\n\n"
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"role": "user",
|
54 |
+
"content": user_input
|
55 |
+
}
|
56 |
+
],
|
57 |
+
temperature=0.03,
|
58 |
+
max_tokens=2000,
|
59 |
+
top_p=0.1,
|
60 |
+
frequency_penalty=0.1,
|
61 |
+
presence_penalty=0.95
|
62 |
+
)
|
63 |
+
|
64 |
+
# Return chatbot response
|
65 |
+
return response["choices"][0]["message"]["content"]
|
66 |
+
|
67 |
+
# Create Gradio chat interface with Submit Button
|
68 |
+
chatbot_ui = gr.Interface(
|
69 |
+
fn=python_tutor_bot,
|
70 |
+
inputs=gr.Textbox(lines=3, placeholder="Ask me anything about Python..."),
|
71 |
+
outputs=gr.Textbox(),
|
72 |
+
title="Python Tutor Bot",
|
73 |
+
description="A friendly Python tutor bot to help you learn and troubleshoot Python. Ask any question!"
|
74 |
+
|
75 |
+
)
|
76 |
+
|
77 |
+
# Launch Gradio UI
|
78 |
+
if __name__ == "__main__":
|
79 |
+
chatbot_ui.launch(share=True)
|