gospacedev commited on
Commit
e28a532
1 Parent(s): 6f93a45

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from huggingface_hub import InferenceClient
4
+
5
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
+
7
+
8
+ def system_instructions(question_difficulty, tone, topic):
9
+ return f"""<s> [INST] Your task is to create 10 questions with 4 choices with a {question_difficulty} difficulty in a {tone} tone about {topic}, then create an answers. Index in JSON, the questions as "Q#":"" to "Q#":"", the four choices as "Q#:C1":"" to "Q#:C4":"", and the answers as "A#":"Q#:C#" to "A#":"Q#:C#". [/INST]"""
10
+
11
+
12
+ with gr.Blocks(title="Quizmona", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as demo:
13
+ gr.HTML("""
14
+ <center>
15
+ <h1>Quizmona</h1>
16
+ <h2>AI-powered Learning Game</h2>
17
+ </center>
18
+ """)
19
+
20
+ topic = gr.Textbox(label="Topic", placeholder="Write any topic")
21
+
22
+ with gr.Row():
23
+ radio = gr.Radio(
24
+ ["easy", "average", "hard"], label="How difficult should the quiz be?"
25
+ )
26
+
27
+ radio_tone = gr.Radio(
28
+ ["casual", "professional", "academic"], label="What tone should the quiz be?"
29
+ )
30
+
31
+ generate_quiz_btn = gr.Button("Generate Quiz!🚀")
32
+
33
+ question_radios = [gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
34
+ visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(visible=False), gr.Radio(
35
+ visible=False), gr.Radio(visible=False), gr.Radio(visible=False)]
36
+
37
+ print(question_radios)
38
+
39
+ @generate_quiz_btn.click(inputs=[radio, radio_tone, topic], outputs=question_radios, api_name="generate_quiz")
40
+ def generate_quiz(question_difficulty, tone, user_prompt):
41
+ formatted_prompt = system_instructions(
42
+ question_difficulty, tone, user_prompt)
43
+
44
+ pre_prompt = [
45
+ {"role": "system", "content": formatted_prompt}
46
+ ]
47
+
48
+ generate_kwargs = dict(
49
+ temperature=0.5,
50
+ max_new_tokens=1024,
51
+ top_p=0.95,
52
+ repetition_penalty=1.0,
53
+ do_sample=True,
54
+ seed=42,
55
+ )
56
+
57
+ response = client.text_generation(
58
+ formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
59
+ # One question with choices and answer is on average 57 tokens
60
+ )
61
+
62
+ filtered_response = response.choices[0].message.content
63
+
64
+ output_json = json.loads(f"{filtered_response}")
65
+
66
+ print(output_json)
67
+
68
+ global quiz_data
69
+
70
+ quiz_data = output_json
71
+
72
+ question_radio_list = []
73
+
74
+ for question_num in range(1, 11):
75
+ question_key = f"Q{question_num}"
76
+ answer_key = f"A{question_num}"
77
+
78
+ question = quiz_data.get(question_key)
79
+ answer = quiz_data.get(quiz_data.get(answer_key))
80
+
81
+ if not question or not answer:
82
+ continue
83
+
84
+ choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
85
+ choice_list = []
86
+ for choice_key in choice_keys:
87
+ choice = quiz_data.get(choice_key, "Choice not found")
88
+ choice_list.append(f"{choice}")
89
+
90
+ radio = gr.Radio(choices=choice_list, label=question,
91
+ visible=True, interactive=True)
92
+
93
+ question_radio_list.append(radio)
94
+
95
+ print(question_radio_list)
96
+
97
+ return question_radio_list
98
+
99
+ check_button = gr.Button("Check Score")
100
+
101
+ score_textbox = gr.Markdown()
102
+
103
+ @check_button.click(inputs=question_radios, outputs=score_textbox)
104
+ def compare_answers(*user_answers):
105
+ user_anwser_list = []
106
+ user_anwser_list = user_answers
107
+
108
+ answers_list = []
109
+
110
+ for question_num in range(1, 20):
111
+ answer_key = f"A{question_num}"
112
+ answer = quiz_data.get(quiz_data.get(answer_key))
113
+ if not answer:
114
+ break
115
+ answers_list.append(answer)
116
+
117
+ score = 0
118
+
119
+ for item in user_anwser_list:
120
+ if item in answers_list:
121
+ score += 1
122
+
123
+ message = f"### You got {score} over 10!"
124
+
125
+ return message
126
+
127
+ if __name__ == "__main__":
128
+ demo.launch(show_api=False)