PatilShruti commited on
Commit
164aa22
·
verified ·
1 Parent(s): a3d78a4

Create app.py

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