Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,49 +5,45 @@ import openai
|
|
5 |
# Replace with your OpenAI API key
|
6 |
openai.api_key = "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"
|
7 |
|
8 |
-
def
|
9 |
response = openai.Completion.create(
|
10 |
engine="gpt-3.5-turbo-0301",
|
11 |
-
prompt=f"
|
12 |
max_tokens=50,
|
|
|
13 |
)
|
14 |
question = response.choices[0].text.strip()
|
15 |
-
return question
|
16 |
|
17 |
-
def check_answer(question, answer, text_content):
|
18 |
response = openai.Completion.create(
|
19 |
engine="gpt-3.5-turbo-0301",
|
20 |
-
prompt=f"
|
21 |
-
max_tokens=
|
|
|
22 |
)
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
def
|
27 |
-
if
|
28 |
-
return "
|
29 |
|
30 |
-
|
31 |
-
question = generate_question(text_content)
|
32 |
-
|
33 |
-
if continue_quiz.lower() == 'yes':
|
34 |
-
if answer is not None:
|
35 |
-
is_correct = check_answer(question, answer, text_content)
|
36 |
-
feedback = "Correct!" if is_correct else "Incorrect."
|
37 |
-
return question, feedback, "text"
|
38 |
-
else:
|
39 |
-
return question, "", "text"
|
40 |
-
else:
|
41 |
-
return "Thank you for participating!", "", "hidden"
|
42 |
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
iface = gr.Interface(
|
45 |
-
fn=
|
46 |
-
inputs=[
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
iface.launch()
|
53 |
-
|
|
|
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, user_answer=None, continue_quiz='Yes'):
|
28 |
+
if continue_quiz.lower() == 'no':
|
29 |
+
return "Quiz ended. Thank you for participating!"
|
30 |
|
31 |
+
question, correct_answer = generate_question_and_answer(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
if user_answer is not None:
|
34 |
+
feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}"
|
35 |
+
return f"Question: {question}\nFeedback: {feedback}"
|
36 |
+
else:
|
37 |
+
return f"Question: {question}"
|
38 |
|
39 |
iface = gr.Interface(
|
40 |
+
fn=get_feedback,
|
41 |
+
inputs=[
|
42 |
+
gr.inputs.Textbox(lines=5, label="Input Text"),
|
43 |
+
gr.inputs.Textbox(lines=1, label="Your Answer"),
|
44 |
+
gr.inputs.Radio(choices=["Yes", "No"], label="Continue?")
|
45 |
+
],
|
46 |
+
outputs=gr.outputs.Textbox(label="Model Feedback"),
|
47 |
)
|
48 |
|
49 |
+
iface.launch()
|
|
|
|