Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
# Replace with your OpenAI API key | |
openai.api_key = "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" | |
def generate_question_and_answer(text): | |
response = openai.Completion.create( | |
engine="gpt-3.5-turbo-0301", | |
prompt=f"Create a question based on the following text: \"{text}\".\nQuestion: ", | |
max_tokens=50, | |
n=1, | |
) | |
question = response.choices[0].text.strip() | |
response = openai.Completion.create( | |
engine="gpt-3.5-turbo-0301", | |
prompt=f"What is the answer to the following question based on the text: \"{text}\"?\nQuestion: {question}\nAnswer: ", | |
max_tokens=50, | |
n=1, | |
) | |
answer = response.choices[0].text.strip() | |
return question, answer | |
def get_feedback(text, user_answer=None, continue_quiz='Yes'): | |
if continue_quiz.lower() == 'no': | |
return "Quiz ended. Thank you for participating!" | |
question, correct_answer = generate_question_and_answer(text) | |
if user_answer is not None: | |
feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}" | |
return f"Question: {question}\nFeedback: {feedback}" | |
else: | |
return f"Question: {question}" | |
iface = gr.Interface( | |
fn=get_feedback, | |
inputs=[ | |
gr.inputs.Textbox(lines=5, label="Input Text"), | |
gr.inputs.Textbox(lines=1, label="Your Answer"), | |
gr.inputs.Radio(choices=["Yes", "No"], label="Continue?") | |
], | |
outputs=gr.outputs.Textbox(label="Model Feedback"), | |
) | |
iface.launch() |