File size: 3,953 Bytes
7728950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
600304b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceab7b4
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr
import llmConnect
import csv
from dotenv import load_dotenv
import os 


def generate_questions(topic):
    llmConnect.addToDataset(topic)

def readDataset():
    with open('dataset.csv', 'r') as file:
        data = list(csv.reader(file))
        return data

def getFirstQuestion(topic):
    generate_questions(topic)
    data = readDataset()
    question = data[0][1]
    answers = data[0][2:6]
    return question, gr.update("answer",choices=answers)

def nextQuestion(question_no):
    data = readDataset()
    if question_no < len(data):
        question = data[question_no][1]
        answers = data[question_no][2:6]
        return question, gr.update("answer",choices=answers)
    return "Quiz completed!", gr.update("answer",choices=["Quiz completed"])

with gr.Blocks() as demo:
    # Initialize states
    user_answers = gr.State([])
    question_no = gr.State(0)
    
    with gr.Row():
        with gr.Column():
            topic = gr.Textbox(
                label="Topic",
                placeholder="Enter the topic you want to generate questions for"
            )
            submit = gr.Button(value="Generate Questions")
            
        with gr.Column():
            question = gr.Textbox(label="Question", elem_id="question")
            answer = gr.Dropdown(
                label="Answer",
                interactive=True,
                elem_id="answer",
                multiselect=False,
                allow_custom_value=False,
                filterable=False
            )
            
            def handle_next(ans, answers, q_no):
                # Store the answer
                answers.append(ans)
                # Increment question number
                next_q_no = q_no + 1
                # Get next question
                question, new_answers = nextQuestion(next_q_no)
                return question, new_answers, answers, next_q_no

            next = gr.Button(value="Next Question")
            next.click(
                handle_next,
                inputs=[answer, user_answers, question_no],
                outputs=[question, answer, user_answers, question_no]
            )

        submit.click(
            getFirstQuestion,
            inputs=[topic],
            outputs=[question, answer]
        )

        with gr.Column():
            score = gr.Textbox(
                label="Score",
                placeholder="Your score will be displayed here"
            )
            
            def calculate_score(answers):
                data = readDataset()
                correct_answers = []
                for i in range(len(data)):
                    correct_answers.append(data[i][1+int(data[i][6])])
                score = sum(1 for a, c in zip(answers, correct_answers) if a == c)
                return f"Your score: {score}/{len(answers)}"

            submit_score = gr.Button(value="Submit")
            submit_score.click(
                calculate_score,
                inputs=[user_answers],
                outputs=[score]
            )

    with gr.Row():
        with gr.Column():
            report = gr.Textbox(
                label="Report",
                placeholder="Your report will be displayed here"
            )
            
            def generate_report(answers):
                data = readDataset()
                report = []
                for i in range(len(answers)):
                    question = data[i][1]
                    user_answer = answers[i]
                    correct_answer = data[i][1+int(data[i][6])]
                    report.append(f"Q{i+1}: {question}\nYour answer: {user_answer}\nCorrect answer: {correct_answer}\n")
                return "\n".join(report)
            
            reportGen = gr.Button(value="Generate Report")
            reportGen.click(
                generate_report,
                inputs=[user_answers],
                outputs=[report]
            )

demo.launch()