Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,60 +5,52 @@ import openai
|
|
5 |
# Replace with your OpenAI API key
|
6 |
openai.api_key = "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"
|
7 |
|
8 |
-
# Global variables to store the state
|
9 |
-
current_step = "question_generation"
|
10 |
-
current_question = ""
|
11 |
-
correct_answer = ""
|
12 |
-
|
13 |
-
def reset_state():
|
14 |
-
global current_step, current_question, correct_answer
|
15 |
-
current_step = "question_generation"
|
16 |
-
current_question = ""
|
17 |
-
correct_answer = ""
|
18 |
-
|
19 |
def generate_question_and_answer(text):
|
20 |
-
global current_question, correct_answer
|
21 |
-
|
22 |
response = openai.Completion.create(
|
23 |
engine="gpt-3.5-turbo-0301",
|
24 |
prompt=f"Create a question based on the following text: \"{text}\".\nQuestion: ",
|
25 |
max_tokens=50,
|
26 |
n=1,
|
27 |
)
|
28 |
-
|
29 |
|
30 |
response = openai.Completion.create(
|
31 |
engine="gpt-3.5-turbo-0301",
|
32 |
-
prompt=f"What is the answer to the following question based on the text: \"{text}\"?\nQuestion: {
|
33 |
max_tokens=50,
|
34 |
n=1,
|
35 |
)
|
36 |
-
|
|
|
|
|
37 |
|
38 |
-
def get_feedback(text, user_answer=None, continue_quiz=
|
39 |
-
|
|
|
40 |
|
41 |
-
if
|
|
|
|
|
|
|
42 |
if text:
|
43 |
-
generate_question_and_answer(text)
|
44 |
-
|
45 |
-
return f"Question: {
|
46 |
-
|
47 |
-
|
48 |
-
elif current_step == "answer_checking":
|
49 |
if user_answer is not None:
|
50 |
-
feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}"
|
51 |
-
|
52 |
-
return f"Feedback: {feedback}
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
|
63 |
iface = gr.Interface(
|
64 |
fn=get_feedback,
|
@@ -69,7 +61,7 @@ iface = gr.Interface(
|
|
69 |
],
|
70 |
outputs=[
|
71 |
gr.outputs.Textbox(label="Model Output"),
|
72 |
-
gr.outputs.
|
73 |
],
|
74 |
live=True
|
75 |
)
|
|
|
5 |
# Replace with your OpenAI API key
|
6 |
openai.api_key = "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"
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def generate_question_and_answer(text):
|
|
|
|
|
9 |
response = openai.Completion.create(
|
10 |
engine="gpt-3.5-turbo-0301",
|
11 |
prompt=f"Create a question based on the following text: \"{text}\".\nQuestion: ",
|
12 |
max_tokens=50,
|
13 |
n=1,
|
14 |
)
|
15 |
+
question = response.choices[0].text.strip()
|
16 |
|
17 |
response = openai.Completion.create(
|
18 |
engine="gpt-3.5-turbo-0301",
|
19 |
+
prompt=f"What is the answer to the following question based on the text: \"{text}\"?\nQuestion: {question}\nAnswer: ",
|
20 |
max_tokens=50,
|
21 |
n=1,
|
22 |
)
|
23 |
+
answer = response.choices[0].text.strip()
|
24 |
+
|
25 |
+
return question, answer
|
26 |
|
27 |
+
def get_feedback(text=None, user_answer=None, continue_quiz=None, state=None):
|
28 |
+
if state is None:
|
29 |
+
state = {}
|
30 |
|
31 |
+
if 'step' not in state:
|
32 |
+
state['step'] = 'get_text'
|
33 |
+
|
34 |
+
if state['step'] == 'get_text':
|
35 |
if text:
|
36 |
+
state['question'], state['correct_answer'] = generate_question_and_answer(text)
|
37 |
+
state['step'] = 'get_answer'
|
38 |
+
return f"Question: {state['question']}", state
|
39 |
+
|
40 |
+
elif state['step'] == 'get_answer':
|
|
|
41 |
if user_answer is not None:
|
42 |
+
feedback = "Correct!" if user_answer.lower() == state['correct_answer'].lower() else f"Incorrect! The correct answer was: {state['correct_answer']}"
|
43 |
+
state['step'] = 'continue_quiz'
|
44 |
+
return f"Feedback: {feedback}\nDo you want to answer another question?", state
|
45 |
+
|
46 |
+
elif state['step'] == 'continue_quiz':
|
47 |
+
if continue_quiz:
|
48 |
+
if continue_quiz.lower() == 'no':
|
49 |
+
state['step'] = 'get_text'
|
50 |
+
return "Quiz ended. Thank you for participating! Please enter new text to start again.", state
|
51 |
+
else:
|
52 |
+
state['step'] = 'get_text'
|
53 |
+
return "Please input text to generate a new question.", state
|
54 |
|
55 |
iface = gr.Interface(
|
56 |
fn=get_feedback,
|
|
|
61 |
],
|
62 |
outputs=[
|
63 |
gr.outputs.Textbox(label="Model Output"),
|
64 |
+
gr.outputs.State(label="State")
|
65 |
],
|
66 |
live=True
|
67 |
)
|