File size: 2,672 Bytes
ed2935f
41a41ff
ed2935f
41a41ff
ed2935f
 
41a41ff
a099ff3
 
 
 
 
 
 
 
 
 
 
7e588a7
a099ff3
 
ed2935f
 
7e588a7
ed2935f
7e588a7
ed2935f
a099ff3
ed2935f
 
 
a099ff3
7e588a7
 
ed2935f
a099ff3
41a41ff
7e588a7
a099ff3
ed2935f
a099ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0875dac
24c1fa1
7e588a7
 
 
 
 
 
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
71
72
73
74
75
76
77
78

import gradio as gr
import openai

# Replace with your OpenAI API key
openai.api_key = "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"

# Global variables to store the state
current_step = "question_generation"
current_question = ""
correct_answer = ""

def reset_state():
    global current_step, current_question, correct_answer
    current_step = "question_generation"
    current_question = ""
    correct_answer = ""

def generate_question_and_answer(text):
    global current_question, correct_answer

    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,
    )
    current_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: {current_question}\nAnswer: ",
      max_tokens=50,
      n=1,
    )
    correct_answer = response.choices[0].text.strip()

def get_feedback(text, user_answer=None, continue_quiz='Yes'):
    global current_step, current_question, correct_answer

    if current_step == "question_generation":
        if text:
            generate_question_and_answer(text)
            current_step = "answer_checking"
            return f"Question: {current_question}", "", "hidden"
        else:
            return "Please input text to generate a question.", "", "hidden"
    elif current_step == "answer_checking":
        if user_answer is not None:
            feedback = "Correct!" if user_answer.lower() == correct_answer.lower() else f"Incorrect! The correct answer was: {correct_answer}"
            current_step = "continue_prompt"
            return f"Feedback: {feedback}", "Do you want to answer another question?", "text"
        else:
            return f"Question: {current_question}", "", "hidden"
    elif current_step == "continue_prompt":
        if continue_quiz.lower() == 'no':
            reset_state()
            return "Quiz ended. Thank you for participating!", "", "hidden"
        else:
            current_step = "question_generation"
            return "Please input text to generate a new question.", "", "hidden"

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.Textbox(label="Prompt"),
    ],
    live=True
)

iface.launch()