Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,41 @@
|
|
1 |
-
import os
|
2 |
-
import openai
|
3 |
import gradio as gr
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
|
|
6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
7 |
|
8 |
-
def
|
9 |
-
if not openai_api_key:
|
10 |
-
return "⚠️ API key is missing. Please configure it in Hugging Face Secrets.", history
|
11 |
-
|
12 |
-
history.append({"role": "user", "content": user_input})
|
13 |
-
|
14 |
try:
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
temperature=0.7,
|
19 |
-
max_tokens=
|
20 |
-
top_p=1,
|
21 |
-
api_key=openai_api_key # Explicitly passing API key
|
22 |
)
|
23 |
-
|
24 |
-
|
25 |
-
history.append({"role": "assistant", "content": bot_reply})
|
26 |
-
|
27 |
except Exception as e:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
title="
|
36 |
-
description="
|
37 |
-
theme="
|
|
|
38 |
)
|
39 |
|
40 |
-
# Launch the app
|
41 |
if __name__ == "__main__":
|
42 |
-
|
|
|
|
|
|
|
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()
|