Ilayda-j's picture
Update app.py
ed2935f
raw
history blame
1.66 kB
import gradio as gr
import openai
# Replace with your OpenAI API key
openai.api_key = "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"
def generate_question(text_content):
response = openai.Completion.create(
engine="gpt-3.5-turbo-0301",
prompt=f"Based on the following text, generate a question:\n{text_content}\nQuestion:",
max_tokens=50,
)
question = response.choices[0].text.strip()
return question
def check_answer(question, answer, text_content):
response = openai.Completion.create(
engine="gpt-3.5-turbo-0301",
prompt=f"The text is: \n{text_content}\nThe question is: {question}\nThe answer given is: {answer}\nIs the answer correct?",
max_tokens=5,
)
feedback = response.choices[0].text.strip()
return feedback.lower() == "yes"
def interactive_quiz(file, answer=None, continue_quiz='yes'):
if file is None:
return "Please upload a txt file to start.", "", "hidden"
text_content = file.read().decode("utf-8")
if continue_quiz.lower() == 'yes':
question = generate_question(text_content)
if answer is not None:
is_correct = check_answer(question, answer, text_content)
feedback = "Correct!" if is_correct else "Incorrect."
return question, feedback, "text"
else:
return question, "", "text"
else:
return "Thank you for participating!", "", "hidden"
iface = gr.Interface(
fn=interactive_quiz,
inputs=["file", "text", "text"],
outputs=["text", "text", {"type": "text", "label": "Continue? (yes/no)", "name": "continue_output"}],
live=True
)
iface.launch()