Spaces:
Runtime error
Runtime error
abhilashnl2006
commited on
Commit
•
13c16db
1
Parent(s):
d44fa2d
Update app.py
Browse files
app.py
CHANGED
@@ -10,46 +10,41 @@ if api_key is None:
|
|
10 |
|
11 |
openai.api_key = api_key
|
12 |
|
13 |
-
def
|
14 |
try:
|
15 |
-
# Construct the
|
16 |
-
messages = [{"role": "system", "content": "You are an AI tutor specialized in teaching Python programming. Your primary task is to create exercise questions based on the topics users want to learn. Provide clear, concise, and informative exercises.
|
17 |
-
|
18 |
-
messages.append({"role": "user", "content": h[0]})
|
19 |
-
if h[1] is not None:
|
20 |
-
messages.append({"role": "assistant", "content": h[1]})
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": history[-1][0]})
|
23 |
|
24 |
# Generate a response using gpt-3.5-turbo
|
25 |
response = openai.ChatCompletion.create(
|
26 |
model="gpt-3.5-turbo",
|
27 |
messages=messages,
|
28 |
-
max_tokens=
|
29 |
n=1,
|
30 |
stop=None,
|
31 |
temperature=0.7,
|
32 |
-
stream=
|
33 |
)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
for chunk in response:
|
38 |
-
reply += chunk['choices'][0]['delta'].get('content', '')
|
39 |
-
|
40 |
-
history[-1] = (history[-1][0], reply)
|
41 |
-
return history
|
42 |
except Exception as e:
|
43 |
-
|
44 |
-
history.append(("Error", error_message))
|
45 |
-
return history
|
46 |
|
47 |
def respond(message, chat_history):
|
48 |
if chat_history is None:
|
49 |
chat_history = []
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
def undo(chat_history):
|
55 |
if chat_history:
|
@@ -71,7 +66,7 @@ with gr.Blocks() as demo:
|
|
71 |
with gr.Column():
|
72 |
user_input = gr.Textbox(
|
73 |
show_label=False,
|
74 |
-
placeholder="Type the topic you want to learn
|
75 |
lines=1
|
76 |
)
|
77 |
submit_button = gr.Button("Submit")
|
@@ -83,11 +78,11 @@ with gr.Blocks() as demo:
|
|
83 |
clear_button.click(clear, None, [chatbot, state])
|
84 |
|
85 |
examples = [
|
86 |
-
["
|
87 |
-
["
|
88 |
-
["
|
89 |
-
["
|
90 |
-
["
|
91 |
]
|
92 |
|
93 |
# Add examples
|
|
|
10 |
|
11 |
openai.api_key = api_key
|
12 |
|
13 |
+
def generate_exercise_question(topic):
|
14 |
try:
|
15 |
+
# Construct the initial message for exercise generation
|
16 |
+
messages = [{"role": "system", "content": "You are an AI tutor specialized in teaching Python programming. Your primary task is to create exercise questions based on the topics users want to learn. Provide clear, concise, and informative exercises. Ensure that all your responses strictly adhere to the subject of Python programming and do not engage in any discussions that are insensitive, sexual, casual, comedic, or unrelated to Python programming."}]
|
17 |
+
messages.append({"role": "user", "content": f"Create an exercise question on {topic}."})
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Generate a response using gpt-3.5-turbo
|
20 |
response = openai.ChatCompletion.create(
|
21 |
model="gpt-3.5-turbo",
|
22 |
messages=messages,
|
23 |
+
max_tokens=150,
|
24 |
n=1,
|
25 |
stop=None,
|
26 |
temperature=0.7,
|
27 |
+
stream=False
|
28 |
)
|
29 |
|
30 |
+
question = response.choices[0].message['content'].strip()
|
31 |
+
return question
|
|
|
|
|
|
|
|
|
|
|
32 |
except Exception as e:
|
33 |
+
return f"Error: {str(e)}"
|
|
|
|
|
34 |
|
35 |
def respond(message, chat_history):
|
36 |
if chat_history is None:
|
37 |
chat_history = []
|
38 |
+
|
39 |
+
# Check if this is the first message to set the topic
|
40 |
+
if len(chat_history) == 0:
|
41 |
+
topic = message
|
42 |
+
question = generate_exercise_question(topic)
|
43 |
+
chat_history.append((message, question))
|
44 |
+
else:
|
45 |
+
chat_history.append((message, "Please answer the question or type 'next' for another question on the same topic."))
|
46 |
+
|
47 |
+
return "", chat_history, chat_history
|
48 |
|
49 |
def undo(chat_history):
|
50 |
if chat_history:
|
|
|
66 |
with gr.Column():
|
67 |
user_input = gr.Textbox(
|
68 |
show_label=False,
|
69 |
+
placeholder="Type the Python topic you want to learn about...",
|
70 |
lines=1
|
71 |
)
|
72 |
submit_button = gr.Button("Submit")
|
|
|
78 |
clear_button.click(clear, None, [chatbot, state])
|
79 |
|
80 |
examples = [
|
81 |
+
["lists"],
|
82 |
+
["decorators"],
|
83 |
+
["dictionaries"],
|
84 |
+
["file handling"],
|
85 |
+
["classes and objects"]
|
86 |
]
|
87 |
|
88 |
# Add examples
|