Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,51 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
{"question": "What is the setting of the poem?", "options": ["A city", "A forest", "A beach", "A mountain"], "correct_option": 1},
|
| 6 |
-
{"question": "What is the underlying message of the poem?", "options": ["The importance of making choices", "The beauty of nature", "The joy of youth", "The value of time"], "correct_option": 0}
|
| 7 |
-
]
|
| 8 |
-
|
| 9 |
-
def interactive_quiz(user_response=None, continue_quiz='yes', question_index=0):
|
| 10 |
-
output_messages = []
|
| 11 |
-
|
| 12 |
-
# Get the current question
|
| 13 |
-
current_question = questions[question_index]
|
| 14 |
-
output_messages.append(f"Question {question_index + 1}: {current_question['question']}")
|
| 15 |
-
for i, option in enumerate(current_question['options']):
|
| 16 |
-
output_messages.append(f"{i}. {option}")
|
| 17 |
-
|
| 18 |
-
if user_response is not None:
|
| 19 |
-
user_response = int(user_response)
|
| 20 |
-
if user_response == current_question['correct_option']:
|
| 21 |
-
output_messages.append("Correct!")
|
| 22 |
-
else:
|
| 23 |
-
output_messages.append(f"Incorrect. The correct answer was: {current_question['options'][current_question['correct_option']]}")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=interactive_quiz,
|
| 35 |
-
inputs=["
|
| 36 |
-
outputs=["text", "
|
| 37 |
live=True
|
| 38 |
)
|
| 39 |
|
| 40 |
iface.launch()
|
|
|
|
|
|
| 1 |
+
|
| 2 |
import gradio as gr
|
| 3 |
+
import openai
|
| 4 |
|
| 5 |
+
# Replace with your OpenAI API key
|
| 6 |
+
openai.api_key = "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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def generate_question(text_content):
|
| 9 |
+
response = openai.Completion.create(
|
| 10 |
+
engine="gpt-3.5-turbo-0301",
|
| 11 |
+
prompt=f"Based on the following text, generate a question:\n{text_content}\nQuestion:",
|
| 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"The text is: \n{text_content}\nThe question is: {question}\nThe answer given is: {answer}\nIs the answer correct?",
|
| 21 |
+
max_tokens=5,
|
| 22 |
+
)
|
| 23 |
+
feedback = response.choices[0].text.strip()
|
| 24 |
+
return feedback.lower() == "yes"
|
| 25 |
|
| 26 |
+
def interactive_quiz(file, answer=None, continue_quiz='yes'):
|
| 27 |
+
if file is None:
|
| 28 |
+
return "Please upload a txt file to start.", "", "hidden"
|
| 29 |
+
|
| 30 |
+
text_content = file.read().decode("utf-8")
|
| 31 |
+
|
| 32 |
+
if continue_quiz.lower() == 'yes':
|
| 33 |
+
question = generate_question(text_content)
|
| 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 |
iface = gr.Interface(
|
| 44 |
fn=interactive_quiz,
|
| 45 |
+
inputs=["file", "text", "text"],
|
| 46 |
+
outputs=["text", "text", {"type": "text", "label": "Continue? (yes/no)", "name": "continue_output"}],
|
| 47 |
live=True
|
| 48 |
)
|
| 49 |
|
| 50 |
iface.launch()
|
| 51 |
+
|