Spaces:
Runtime error
Runtime error
File size: 1,822 Bytes
ed2935f 41a41ff ed2935f 41a41ff ed2935f 41a41ff 7e588a7 ed2935f 361c9e1 ed2935f 7e588a7 ed2935f 96b2682 ed2935f 361c9e1 7e588a7 ed2935f 361c9e1 41a41ff 961f0c2 0875dac 24c1fa1 7e588a7 961f0c2 7e588a7 a099ff3 961f0c2 a099ff3 24c1fa1 a099ff3 361c9e1 961f0c2 |
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 |
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 multiple-choice 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"Generate 4 possible answers for the question: \"{question}\" based on the text: \"{text}\".\n1. Answer A: \n2. Answer B: \n3. Answer C: \n4. Answer D: ",
max_tokens=50,
n=1,
)
answers_text = response.choices[0].text.strip()
answers = answers_text.split("\n")
return question, answers
def get_feedback(inputs, state):
text, user_answer, continue_quiz = inputs
if state is None:
state = {'step': 1, 'text': text}
if state['step'] == 1:
if text:
state['question'], state['answers'] = generate_question_and_answer(text)
state['step'] = 2
return {"output": f"Question: {state['question']}\nOptions:\n" + "\n".join(state['answers']), "state": state}
else:
return {"output": "Please input text to generate a question.", "state": state}
iface = gr.Interface(
fn=get_feedback,
inputs=[
gr.inputs.Textbox(lines=5, label="Input Text"),
gr.inputs.Radio(choices=["1", "2", "3", "4"], label="Your Answer", optional=True),
gr.inputs.Radio(choices=["Yes", "No"], label="Continue?", optional=True)
],
outputs=[
gr.outputs.Textbox(label="Model Output"),
gr.outputs.State(label="State")
],
live=True
)
iface.launch()
|