decodingdatascience commited on
Commit
e5ddcf4
·
verified ·
1 Parent(s): 0117838

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +71 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # Hugging Face Spaces secret name:
6
+ # Add this in Space Settings > Secrets:
7
+ # OPENAI_API_KEY = your_openai_api_key
8
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
+ MODEL_NAME = os.getenv("OPENAI_MODEL", "gpt-5.5")
10
+
11
+ client = OpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None
12
+
13
+ SYSTEM_INSTRUCTIONS = """
14
+ You are a helpful Python tutor and AI assistant.
15
+ Explain concepts clearly, give simple examples, and help beginners debug code step by step.
16
+ Keep answers practical and beginner-friendly.
17
+ """
18
+
19
+
20
+ def ask_ai(user_question):
21
+ if not OPENAI_API_KEY:
22
+ return (
23
+ "OPENAI_API_KEY is missing.\n\n"
24
+ "In Hugging Face Spaces, go to:\n"
25
+ "Settings > Secrets > New Secret\n\n"
26
+ "Name: OPENAI_API_KEY\n"
27
+ "Value: your OpenAI API key"
28
+ )
29
+
30
+ if not user_question or not user_question.strip():
31
+ return "Please enter a question."
32
+
33
+ try:
34
+ # Important:
35
+ # Do NOT pass temperature for gpt-5.5.
36
+ # Some GPT-5.x models only support the default temperature value.
37
+ response = client.responses.create(
38
+ model=MODEL_NAME,
39
+ instructions=SYSTEM_INSTRUCTIONS,
40
+ input=user_question.strip(),
41
+ )
42
+
43
+ return response.output_text
44
+
45
+ except Exception as e:
46
+ return f"Error: {type(e).__name__}\n\n{str(e)}"
47
+
48
+
49
+ demo = gr.Interface(
50
+ fn=ask_ai,
51
+ inputs=gr.Textbox(
52
+ label="Ask your question",
53
+ placeholder="Example: Explain Python lists with a simple example",
54
+ lines=5,
55
+ ),
56
+ outputs=gr.Markdown(label="AI Response"),
57
+ title="Python Tutor with OpenAI",
58
+ description=(
59
+ "A simple Hugging Face Spaces app using OpenAI. "
60
+ "Add OPENAI_API_KEY in Hugging Face Secrets before running."
61
+ ),
62
+ examples=[
63
+ ["Explain Python functions with one simple example."],
64
+ ["What is the difference between a list and a tuple in Python?"],
65
+ ["Fix this code: for i in range(5) print(i)"],
66
+ ],
67
+ )
68
+
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ openai