File size: 1,664 Bytes
ed2935f
41a41ff
ed2935f
41a41ff
ed2935f
 
41a41ff
ed2935f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41a41ff
ed2935f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41a41ff
24c1fa1
41a41ff
ed2935f
 
41a41ff
24c1fa1
 
 
ed2935f
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

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()