Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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(
|
|
|
|
| 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()
|