PatilShruti commited on
Commit
ee79f0b
·
verified ·
1 Parent(s): b42f40c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -51
app.py CHANGED
@@ -8,7 +8,7 @@ load_dotenv()
8
  api_key = os.getenv("openai")
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
@@ -17,63 +17,45 @@ openai.api_key = api_key
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)
 
8
  api_key = os.getenv("openai")
9
 
10
  if not api_key:
11
+ raise ValueError("API Key not found! Ensure you have set 'OPENAI_API_KEY' in your .env file.")
12
 
13
  # Set up OpenAI API Key
14
  openai.api_key = api_key
 
17
  def python_tutor_bot(user_input):
18
  if not user_input.strip():
19
  return "Please enter a valid question."
20
+
21
+ try:
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-4-turbo", # Corrected model name
24
+ messages=[
25
+ {
26
+ "role": "system",
27
+ "content": (
28
+ "You are a Python tutor bot designed to help beginners learn and troubleshoot Python programming. "
29
+ "Explain concepts in simple terms, provide clear examples, and help debug user code.\n\n"
30
+ "### Guidelines:\n"
31
+ "- Use beginner-friendly language, as if explaining to an 8th grader.\n"
32
+ "- Offer simple code examples to illustrate concepts.\n"
33
+ "- Identify and fix errors in user-provided code with explanations.\n"
34
+ "- Encourage follow-up questions to ensure understanding.\n"
35
+ ),
36
+ },
37
+ {"role": "user", "content": user_input}
38
+ ],
39
+ temperature=0.1,
40
+ max_tokens=1000,
41
+ top_p=0.9,
42
+ frequency_penalty=0,
43
+ presence_penalty=0.3
44
+ )
45
+ return response["choices"][0]["message"]["content"]
46
+
47
+ except openai.error.OpenAIError as e:
48
+ return f"Error: {str(e)}"
49
+
50
+ # Create Gradio chat interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  chatbot_ui = gr.Interface(
52
  fn=python_tutor_bot,
53
  inputs=gr.Textbox(lines=3, placeholder="Ask me anything about Python..."),
54
  outputs=gr.Textbox(),
55
  title="Python Tutor Bot",
56
  description="A friendly Python tutor bot to help you learn and troubleshoot Python. Ask any question!"
 
57
  )
58
 
59
  # Launch Gradio UI
60
  if __name__ == "__main__":
61
+ chatbot_ui.launch()