Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
client = openai.OpenAI(api_key=openai_api_key)
|
10 |
+
|
11 |
+
def get_python_help(question):
|
12 |
+
try:
|
13 |
+
messages = [
|
14 |
+
{"role": "system", "content": "You are a helpful assistant for Python programming."},
|
15 |
+
{"role": "user", "content": question}
|
16 |
+
]
|
17 |
+
|
18 |
+
response = client.chat.completions.create(
|
19 |
+
model="gpt-4",
|
20 |
+
messages=messages,
|
21 |
+
temperature=0.7,
|
22 |
+
max_tokens=150
|
23 |
+
)
|
24 |
+
|
25 |
+
return response.choices[0].message.content
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error: {str(e)}"
|
28 |
+
|
29 |
+
# Create Gradio interface
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=get_python_help,
|
32 |
+
inputs=gr.Textbox(label="Your Python Question:", placeholder="Type your Python question here..."),
|
33 |
+
outputs=gr.Textbox(label="Python Tutor Bot Response"),
|
34 |
+
title="Python Tutor Bot",
|
35 |
+
description="Ask your Python programming questions! Type 'exit' to end the session.",
|
36 |
+
theme="default",
|
37 |
+
examples=[["What is a tuple in Python?"], ["How do I use list comprehension?"]],
|
38 |
+
)
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
iface.launch()
|