Spaces:
Runtime error
Runtime error
File size: 2,351 Bytes
ed2935f 41a41ff ed2935f 41a41ff ed2935f 41a41ff 7e588a7 ed2935f 7e588a7 ed2935f 7e588a7 ed2935f 96b2682 ed2935f 96b2682 7e588a7 ed2935f 96b2682 41a41ff 96b2682 ed2935f 96b2682 a099ff3 96b2682 a099ff3 96b2682 0875dac 24c1fa1 7e588a7 a099ff3 96b2682 a099ff3 24c1fa1 a099ff3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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=None, user_answer=None, continue_quiz=None, state=None):
if state is None:
state = {}
if 'step' not in state:
state['step'] = 'get_text'
if state['step'] == 'get_text':
if text:
state['question'], state['correct_answer'] = generate_question_and_answer(text)
state['step'] = 'get_answer'
return f"Question: {state['question']}", state
elif state['step'] == 'get_answer':
if user_answer is not None:
feedback = "Correct!" if user_answer.lower() == state['correct_answer'].lower() else f"Incorrect! The correct answer was: {state['correct_answer']}"
state['step'] = 'continue_quiz'
return f"Feedback: {feedback}\nDo you want to answer another question?", state
elif state['step'] == 'continue_quiz':
if continue_quiz:
if continue_quiz.lower() == 'no':
state['step'] = 'get_text'
return "Quiz ended. Thank you for participating! Please enter new text to start again.", state
else:
state['step'] = 'get_text'
return "Please input text to generate a new question.", state
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 Output"),
gr.outputs.State(label="State")
],
live=True
)
iface.launch()
|