rameshmoorthy commited on
Commit
dae0806
·
verified ·
1 Parent(s): cfccad2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +447 -393
app.py CHANGED
@@ -1,393 +1,447 @@
1
- import gradio as gr
2
- from pathlib import Path
3
- from tempfile import NamedTemporaryFile
4
- from sentence_transformers import CrossEncoder
5
- import numpy as np
6
- from time import perf_counter
7
- import pandas as pd
8
- from pydantic import BaseModel, Field
9
- from phi.agent import Agent
10
- from phi.model.groq import Groq
11
- import os
12
- import logging
13
-
14
- # Set up logging
15
- logging.basicConfig(level=logging.INFO)
16
- logger = logging.getLogger(__name__)
17
-
18
- # API Key setup
19
- api_key = os.getenv("GROQ_API_KEY")
20
- if not api_key:
21
- gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
22
- logger.error("GROQ_API_KEY not found.")
23
- else:
24
- os.environ["GROQ_API_KEY"] = api_key
25
-
26
- # Pydantic Model for Quiz Structure
27
- class QuizItem(BaseModel):
28
- question: str = Field(..., description="The quiz question")
29
- choices: list[str] = Field(..., description="List of 4 multiple-choice options")
30
- correct_answer: str = Field(..., description="The correct choice (e.g., 'C1')")
31
-
32
- class QuizOutput(BaseModel):
33
- items: list[QuizItem] = Field(..., description="List of 10 quiz items")
34
-
35
- # Initialize Agents
36
- groq_agent = Agent(model=Groq(model="llama3-70b-8192", api_key=api_key), markdown=True)
37
-
38
- quiz_generator = Agent(
39
- name="Quiz Generator",
40
- role="Generates structured quiz questions and answers",
41
- instructions=[
42
- "Create 10 questions with 4 choices each based on the provided topic and documents.",
43
- "Use the specified difficulty level (easy, average, hard) to adjust question complexity.",
44
- "Ensure questions are derived only from the provided documents.",
45
- "Return the output in a structured format using the QuizOutput Pydantic model.",
46
- "Each question should have a unique correct answer from the choices (labeled C1, C2, C3, C4)."
47
- ],
48
- model=Groq(id="llama3-70b-8192", api_key=api_key),
49
- response_model=QuizOutput,
50
- markdown=True
51
- )
52
-
53
- VECTOR_COLUMN_NAME = "vector"
54
- TEXT_COLUMN_NAME = "text"
55
- proj_dir = Path.cwd()
56
-
57
- # Calling functions from backend (assuming they exist)
58
- from backend.semantic_search import table, retriever
59
-
60
- def generate_quiz_data(question_difficulty, topic, documents_str):
61
- prompt = f"""Generate a quiz with {question_difficulty} difficulty on topic '{topic}' using only the following documents:\n{documents_str}"""
62
- try:
63
- response = quiz_generator.run(prompt)
64
- return response.content
65
- except Exception as e:
66
- logger.error(f"Failed to generate quiz: {e}")
67
- return None
68
-
69
- def json_to_excel(quiz_data):
70
- data = []
71
- gr.Warning('Generating Shareable file link..', duration=30)
72
- for i, item in enumerate(quiz_data.items, 1):
73
- data.append([
74
- item.question,
75
- "Multiple Choice",
76
- item.choices[0],
77
- item.choices[1],
78
- item.choices[2],
79
- item.choices[3],
80
- '', # Option 5 (empty)
81
- item.correct_answer.replace('C', ''),
82
- 30,
83
- ''
84
- ])
85
- df = pd.DataFrame(data, columns=[
86
- "Question Text", "Question Type", "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Correct Answer", "Time in seconds", "Image Link"
87
- ])
88
- temp_file = NamedTemporaryFile(delete=True, suffix=".xlsx")
89
- df.to_excel(temp_file.name, index=False)
90
- return temp_file.name
91
-
92
- colorful_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")
93
-
94
- with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
95
- with gr.Row():
96
- with gr.Column(scale=2):
97
- gr.Image(value='logo.png', height=200, width=200)
98
- with gr.Column(scale=6):
99
- gr.HTML("""
100
- <center>
101
- <h1><span style="color: purple;">GOVERNMENT HIGH SCHOOL,SUTHUKENY</span> STUDENTS QUIZBOT </h1>
102
- <h2>Generative AI-powered Capacity building for STUDENTS</h2>
103
- <i>⚠️ Students can create quiz from any topic from 9th Science and evaluate themselves! ⚠️</i>
104
- </center>
105
- """)
106
-
107
- topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from 9TH Science CBSE")
108
- with gr.Row():
109
- difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
110
- model_radio = gr.Radio(choices=['(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings") # Removed ColBERT option
111
-
112
- generate_quiz_btn = gr.Button("Generate Quiz!🚀")
113
- quiz_msg = gr.Textbox(label="Status", interactive=False)
114
- question_display = gr.HTML(visible=False)
115
- download_excel = gr.File(label="Download Excel")
116
-
117
- @generate_quiz_btn.click(inputs=[difficulty_radio, topic, model_radio], outputs=[quiz_msg, question_display, download_excel])
118
- def generate_quiz(question_difficulty, topic, cross_encoder):
119
- top_k_rank = 10
120
- documents = []
121
- gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
122
-
123
- document_start = perf_counter()
124
- query_vec = retriever.encode(topic)
125
- documents = [doc[TEXT_COLUMN_NAME] for doc in table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()]
126
- if cross_encoder == '(ACCURATE) BGE reranker':
127
- cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
128
- query_doc_pair = [[topic, doc] for doc in documents]
129
- cross_scores = cross_encoder1.predict(query_doc_pair)
130
- sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
131
- documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
132
-
133
- documents_str = '\n'.join(documents)
134
- quiz_data = generate_quiz_data(question_difficulty, topic, documents_str)
135
- if not quiz_data or not quiz_data.items:
136
- return ["Error: Failed to generate quiz.", gr.HTML(visible=False), None]
137
-
138
- excel_file = json_to_excel(quiz_data)
139
- html_content = "<div>" + "".join(f"<h3>{i}. {item.question}</h3><p>{'<br>'.join(item.choices)}</p>" for i, item in enumerate(quiz_data.items[:10], 1)) + "</div>"
140
- return ["Quiz Generated!", gr.HTML(value=html_content, visible=True), excel_file]
141
-
142
- check_button = gr.Button("Check Score")
143
- score_textbox = gr.Markdown()
144
-
145
- @check_button.click(inputs=question_display, outputs=score_textbox)
146
- def compare_answers(html_content):
147
- if not quiz_data or not quiz_data.items:
148
- return "Please generate a quiz first."
149
- # Placeholder for user answers (adjust based on actual UI implementation)
150
- user_answers = [] # Implement parsing logic if using radio inputs
151
- correct_answers = [item.correct_answer for item in quiz_data.items[:10]]
152
- score = sum(1 for u, c in zip(user_answers, correct_answers) if u == c)
153
- if score > 7:
154
- message = f"### Excellent! You got {score} out of 10!"
155
- elif score > 5:
156
- message = f"### Good! You got {score} out of 10!"
157
- else:
158
- message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
159
- return message
160
-
161
- if __name__ == "__main__":
162
- QUIZBOT.queue().launch(debug=True)
163
-
164
- # # Importing libraries
165
- # import pandas as pd
166
- # import json
167
- # import gradio as gr
168
- # from pathlib import Path
169
- # from ragatouille import RAGPretrainedModel
170
- # from gradio_client import Client
171
- # from tempfile import NamedTemporaryFile
172
- # from sentence_transformers import CrossEncoder
173
- # import numpy as np
174
- # from time import perf_counter
175
- # from sentence_transformers import CrossEncoder
176
-
177
- # #calling functions from other files - to call the knowledge database tables (lancedb for accurate mode) for creating quiz
178
- # from backend.semantic_search import table, retriever
179
-
180
- # VECTOR_COLUMN_NAME = "vector"
181
- # TEXT_COLUMN_NAME = "text"
182
- # proj_dir = Path.cwd()
183
-
184
- # # Set up logging
185
- # import logging
186
- # logging.basicConfig(level=logging.INFO)
187
- # logger = logging.getLogger(__name__)
188
-
189
- # # Replace Mixtral client with Qwen Client
190
- # client = Client("Qwen/Qwen1.5-110B-Chat-demo")
191
-
192
- # def system_instructions(question_difficulty, topic, documents_str):
193
- # return f"""<s> [INST] You are a great teacher and your task is to create 10 questions with 4 choices with {question_difficulty} difficulty about the topic request "{topic}" only from the below given documents, {documents_str}. Then create answers. Index in JSON format, 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#". Example: 'A10':'Q10:C3' [/INST]"""
194
-
195
- # # Ragatouille database for Colbert ie highly accurate mode
196
- # RAG_db = gr.State()
197
- # quiz_data = None
198
-
199
-
200
- # #defining a function to convert json file to excel file
201
- # def json_to_excel(output_json):
202
- # # Initialize list for DataFrame
203
- # data = []
204
- # gr.Warning('Generating Shareable file link..', duration=30)
205
- # for i in range(1, 11): # Assuming there are 10 questions
206
- # question_key = f"Q{i}"
207
- # answer_key = f"A{i}"
208
-
209
- # question = output_json.get(question_key, '')
210
- # correct_answer_key = output_json.get(answer_key, '')
211
- # #correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
212
- # correct_answer = correct_answer_key.split(':')[-1].replace('C', '').strip() if correct_answer_key else ''
213
-
214
- # # Extract options
215
- # option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
216
- # options = [output_json.get(key, '') for key in option_keys]
217
-
218
- # # Add data row
219
- # data.append([
220
- # question, # Question Text
221
- # "Multiple Choice", # Question Type
222
- # options[0], # Option 1
223
- # options[1], # Option 2
224
- # options[2] if len(options) > 2 else '', # Option 3
225
- # options[3] if len(options) > 3 else '', # Option 4
226
- # options[4] if len(options) > 4 else '', # Option 5
227
- # correct_answer, # Correct Answer
228
- # 30, # Time in seconds
229
- # '' # Image Link
230
- # ])
231
-
232
- # # Create DataFrame
233
- # df = pd.DataFrame(data, columns=[
234
- # "Question Text",
235
- # "Question Type",
236
- # "Option 1",
237
- # "Option 2",
238
- # "Option 3",
239
- # "Option 4",
240
- # "Option 5",
241
- # "Correct Answer",
242
- # "Time in seconds",
243
- # "Image Link"
244
- # ])
245
-
246
- # temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
247
- # df.to_excel(temp_file.name, index=False)
248
- # return temp_file.name
249
- # # Define a colorful theme
250
- # colorful_theme = gr.themes.Default(
251
- # primary_hue="cyan", # Set a bright cyan as primary color
252
- # secondary_hue="yellow", # Set a bright magenta as secondary color
253
- # neutral_hue="purple" # Optionally set a neutral color
254
-
255
- # )
256
-
257
- # #gradio app creation for a user interface
258
- # with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
259
-
260
-
261
- # # Create a single row for the HTML and Image
262
- # with gr.Row():
263
- # with gr.Column(scale=2):
264
- # gr.Image(value='logo.png', height=200, width=200)
265
- # with gr.Column(scale=6):
266
- # gr.HTML("""
267
- # <center>
268
- # <h1><span style="color: purple;">GOVERNMENT HIGH SCHOOL,SUTHUKENY</span> STUDENTS QUIZBOT </h1>
269
- # <h2>Generative AI-powered Capacity building for STUDENTS</h2>
270
- # <i>⚠️ Students can create quiz from any topic from 10 science and evaluate themselves! ⚠️</i>
271
- # </center>
272
- # """)
273
-
274
-
275
-
276
-
277
- # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any CHAPTER NAME")
278
-
279
- # with gr.Row():
280
- # difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
281
- # model_radio = gr.Radio(choices=[ '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'],
282
- # value='(ACCURATE) BGE reranker', label="Embeddings",
283
- # info="First query to ColBERT may take a little time")
284
-
285
- # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
286
- # quiz_msg = gr.Textbox()
287
-
288
- # question_radios = [gr.Radio(visible=False) for _ in range(10)]
289
-
290
- # @generate_quiz_btn.click(inputs=[difficulty_radio, topic, model_radio], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")])
291
- # def generate_quiz(question_difficulty, topic, cross_encoder):
292
- # top_k_rank = 10
293
- # documents = []
294
- # gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
295
-
296
- # if cross_encoder == '(HIGH ACCURATE) ColBERT':
297
- # gr.Warning('Retrieving using ColBERT.. First-time query will take 2 minute for model to load.. please wait',duration=100)
298
- # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
299
- # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
300
- # documents_full = RAG_db.value.search(topic, k=top_k_rank)
301
- # documents = [item['content'] for item in documents_full]
302
-
303
- # else:
304
- # document_start = perf_counter()
305
- # query_vec = retriever.encode(topic)
306
- # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
307
-
308
- # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()
309
- # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
310
-
311
- # query_doc_pair = [[topic, doc] for doc in documents]
312
-
313
- # # if cross_encoder == '(FAST) MiniLM-L6v2':
314
- # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
315
- # if cross_encoder == '(ACCURATE) BGE reranker':
316
- # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
317
-
318
- # cross_scores = cross_encoder1.predict(query_doc_pair)
319
- # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
320
- # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
321
-
322
- # #creating a text prompt to Qwen model combining the documents and system instruction
323
- # formatted_prompt = system_instructions(question_difficulty, topic, '\n'.join(documents))
324
- # print(' Formatted Prompt : ' ,formatted_prompt)
325
- # try:
326
- # response = client.predict(query=formatted_prompt, history=[], system="You are a helpful assistant.", api_name="/model_chat")
327
- # response1 = response[1][0][1]
328
-
329
- # # Extract JSON
330
- # start_index = response1.find('{')
331
- # end_index = response1.rfind('}')
332
- # cleaned_response = response1[start_index:end_index + 1] if start_index != -1 and end_index != -1 else ''
333
- # print('Cleaned Response :',cleaned_response)
334
- # output_json = json.loads(cleaned_response)
335
- # # Assign the extracted JSON to quiz_data for use in the comparison function
336
- # global quiz_data
337
- # quiz_data = output_json
338
- # # Generate the Excel file
339
- # excel_file = json_to_excel(output_json)
340
-
341
-
342
- # #Create a Quiz display in app
343
- # question_radio_list = []
344
- # for question_num in range(1, 11):
345
- # question_key = f"Q{question_num}"
346
- # answer_key = f"A{question_num}"
347
-
348
- # question = output_json.get(question_key)
349
- # answer = output_json.get(output_json.get(answer_key))
350
-
351
- # if not question or not answer:
352
- # continue
353
-
354
- # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
355
- # choice_list = [output_json.get(choice_key, "Choice not found") for choice_key in choice_keys]
356
-
357
- # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
358
- # question_radio_list.append(radio)
359
-
360
- # return ['Quiz Generated!'] + question_radio_list + [excel_file]
361
-
362
- # except json.JSONDecodeError as e:
363
- # print(f"Failed to decode JSON: {e}")
364
-
365
- # check_button = gr.Button("Check Score")
366
- # score_textbox = gr.Markdown()
367
-
368
- # @check_button.click(inputs=question_radios, outputs=score_textbox)
369
- # def compare_answers(*user_answers):
370
- # user_answer_list = list(user_answers)
371
- # answers_list = []
372
-
373
- # for question_num in range(1, 11):
374
- # answer_key = f"A{question_num}"
375
- # answer = quiz_data.get(quiz_data.get(answer_key))
376
- # if not answer:
377
- # break
378
- # answers_list.append(answer)
379
-
380
- # score = sum(1 for item in user_answer_list if item in answers_list)
381
-
382
- # if score > 7:
383
- # message = f"### Excellent! You got {score} out of 10!"
384
- # elif score > 5:
385
- # message = f"### Good! You got {score} out of 10!"
386
- # else:
387
- # message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
388
-
389
- # return message
390
-
391
- # QUIZBOT.queue()
392
- # QUIZBOT.launch(debug=True)
393
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import CrossEncoder
3
+ import numpy as np
4
+ from time import perf_counter
5
+ from backend.semantic_search import table, retriever
6
+ import os
7
+ import logging
8
+
9
+ # Set up logging
10
+ logging.basicConfig(level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
+
13
+ # API Key setup (optional, depending on retriever needs)
14
+ api_key = os.getenv("GROQ_API_KEY")
15
+ if not api_key:
16
+ gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets' if required.")
17
+ logger.error("GROQ_API_KEY not found.")
18
+ else:
19
+ os.environ["GROQ_API_KEY"] = api_key
20
+
21
+ VECTOR_COLUMN_NAME = "vector"
22
+ TEXT_COLUMN_NAME = "text"
23
+
24
+ def retrieve_documents(topic):
25
+ gr.Warning('Retrieving documents may take a moment. Please wait.', duration=30)
26
+ top_k_rank = 10
27
+ documents = []
28
+
29
+ document_start = perf_counter()
30
+ query_vec = retriever.encode(topic)
31
+ documents = [doc[TEXT_COLUMN_NAME] for doc in table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()]
32
+
33
+ # Apply BGE reranker
34
+ cross_encoder = CrossEncoder('BAAI/bge-reranker-base')
35
+ query_doc_pair = [[topic, doc] for doc in documents]
36
+ cross_scores = cross_encoder.predict(query_doc_pair)
37
+ sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
38
+ documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
39
+
40
+ elapsed_time = perf_counter() - document_start
41
+ return f"Retrieved {len(documents)} documents in {elapsed_time:.2f} seconds:\n\n" + "\n".join(documents)
42
+
43
+ with gr.Blocks(title="Document Retrieval Test") as APP:
44
+ with gr.Row():
45
+ gr.Markdown("# Document Retrieval Test")
46
+
47
+ topic = gr.Textbox(label="Enter Topic", placeholder="Enter a topic to search")
48
+ output = gr.Textbox(label="Retrieved Documents", interactive=False)
49
+
50
+ submit_btn = gr.Button("Retrieve Documents")
51
+ submit_btn.click(fn=retrieve_documents, inputs=[topic], outputs=[output])
52
+
53
+ if __name__ == "__main__":
54
+ APP.queue().launch(server_name="0.0.0.0", server_port=7860)
55
+ # import gradio as gr
56
+ # from pathlib import Path
57
+ # from tempfile import NamedTemporaryFile
58
+ # from sentence_transformers import CrossEncoder
59
+ # import numpy as np
60
+ # from time import perf_counter
61
+ # import pandas as pd
62
+ # from pydantic import BaseModel, Field
63
+ # from phi.agent import Agent
64
+ # from phi.model.groq import Groq
65
+ # import os
66
+ # import logging
67
+
68
+ # # Set up logging
69
+ # logging.basicConfig(level=logging.INFO)
70
+ # logger = logging.getLogger(__name__)
71
+
72
+ # # API Key setup
73
+ # api_key = os.getenv("GROQ_API_KEY")
74
+ # if not api_key:
75
+ # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
76
+ # logger.error("GROQ_API_KEY not found.")
77
+ # else:
78
+ # os.environ["GROQ_API_KEY"] = api_key
79
+
80
+ # # Pydantic Model for Quiz Structure
81
+ # class QuizItem(BaseModel):
82
+ # question: str = Field(..., description="The quiz question")
83
+ # choices: list[str] = Field(..., description="List of 4 multiple-choice options")
84
+ # correct_answer: str = Field(..., description="The correct choice (e.g., 'C1')")
85
+
86
+ # class QuizOutput(BaseModel):
87
+ # items: list[QuizItem] = Field(..., description="List of 10 quiz items")
88
+
89
+ # # Initialize Agents
90
+ # groq_agent = Agent(model=Groq(model="llama3-70b-8192", api_key=api_key), markdown=True)
91
+
92
+ # quiz_generator = Agent(
93
+ # name="Quiz Generator",
94
+ # role="Generates structured quiz questions and answers",
95
+ # instructions=[
96
+ # "Create 10 questions with 4 choices each based on the provided topic and documents.",
97
+ # "Use the specified difficulty level (easy, average, hard) to adjust question complexity.",
98
+ # "Ensure questions are derived only from the provided documents.",
99
+ # "Return the output in a structured format using the QuizOutput Pydantic model.",
100
+ # "Each question should have a unique correct answer from the choices (labeled C1, C2, C3, C4)."
101
+ # ],
102
+ # model=Groq(id="llama3-70b-8192", api_key=api_key),
103
+ # response_model=QuizOutput,
104
+ # markdown=True
105
+ # )
106
+
107
+ # VECTOR_COLUMN_NAME = "vector"
108
+ # TEXT_COLUMN_NAME = "text"
109
+ # proj_dir = Path.cwd()
110
+
111
+ # # Calling functions from backend (assuming they exist)
112
+ # from backend.semantic_search import table, retriever
113
+
114
+ # def generate_quiz_data(question_difficulty, topic, documents_str):
115
+ # prompt = f"""Generate a quiz with {question_difficulty} difficulty on topic '{topic}' using only the following documents:\n{documents_str}"""
116
+ # try:
117
+ # response = quiz_generator.run(prompt)
118
+ # return response.content
119
+ # except Exception as e:
120
+ # logger.error(f"Failed to generate quiz: {e}")
121
+ # return None
122
+
123
+ # def json_to_excel(quiz_data):
124
+ # data = []
125
+ # gr.Warning('Generating Shareable file link..', duration=30)
126
+ # for i, item in enumerate(quiz_data.items, 1):
127
+ # data.append([
128
+ # item.question,
129
+ # "Multiple Choice",
130
+ # item.choices[0],
131
+ # item.choices[1],
132
+ # item.choices[2],
133
+ # item.choices[3],
134
+ # '', # Option 5 (empty)
135
+ # item.correct_answer.replace('C', ''),
136
+ # 30,
137
+ # ''
138
+ # ])
139
+ # df = pd.DataFrame(data, columns=[
140
+ # "Question Text", "Question Type", "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Correct Answer", "Time in seconds", "Image Link"
141
+ # ])
142
+ # temp_file = NamedTemporaryFile(delete=True, suffix=".xlsx")
143
+ # df.to_excel(temp_file.name, index=False)
144
+ # return temp_file.name
145
+
146
+ # colorful_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")
147
+
148
+ # with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
149
+ # with gr.Row():
150
+ # with gr.Column(scale=2):
151
+ # gr.Image(value='logo.png', height=200, width=200)
152
+ # with gr.Column(scale=6):
153
+ # gr.HTML("""
154
+ # <center>
155
+ # <h1><span style="color: purple;">GOVERNMENT HIGH SCHOOL,SUTHUKENY</span> STUDENTS QUIZBOT </h1>
156
+ # <h2>Generative AI-powered Capacity building for STUDENTS</h2>
157
+ # <i>⚠️ Students can create quiz from any topic from 9th Science and evaluate themselves! ⚠️</i>
158
+ # </center>
159
+ # """)
160
+
161
+ # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from 9TH Science CBSE")
162
+ # with gr.Row():
163
+ # difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
164
+ # model_radio = gr.Radio(choices=['(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings") # Removed ColBERT option
165
+
166
+ # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
167
+ # quiz_msg = gr.Textbox(label="Status", interactive=False)
168
+ # question_display = gr.HTML(visible=False)
169
+ # download_excel = gr.File(label="Download Excel")
170
+
171
+ # @generate_quiz_btn.click(inputs=[difficulty_radio, topic, model_radio], outputs=[quiz_msg, question_display, download_excel])
172
+ # def generate_quiz(question_difficulty, topic, cross_encoder):
173
+ # top_k_rank = 10
174
+ # documents = []
175
+ # gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
176
+
177
+ # document_start = perf_counter()
178
+ # query_vec = retriever.encode(topic)
179
+ # documents = [doc[TEXT_COLUMN_NAME] for doc in table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()]
180
+ # if cross_encoder == '(ACCURATE) BGE reranker':
181
+ # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
182
+ # query_doc_pair = [[topic, doc] for doc in documents]
183
+ # cross_scores = cross_encoder1.predict(query_doc_pair)
184
+ # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
185
+ # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
186
+
187
+ # documents_str = '\n'.join(documents)
188
+ # quiz_data = generate_quiz_data(question_difficulty, topic, documents_str)
189
+ # if not quiz_data or not quiz_data.items:
190
+ # return ["Error: Failed to generate quiz.", gr.HTML(visible=False), None]
191
+
192
+ # excel_file = json_to_excel(quiz_data)
193
+ # html_content = "<div>" + "".join(f"<h3>{i}. {item.question}</h3><p>{'<br>'.join(item.choices)}</p>" for i, item in enumerate(quiz_data.items[:10], 1)) + "</div>"
194
+ # return ["Quiz Generated!", gr.HTML(value=html_content, visible=True), excel_file]
195
+
196
+ # check_button = gr.Button("Check Score")
197
+ # score_textbox = gr.Markdown()
198
+
199
+ # @check_button.click(inputs=question_display, outputs=score_textbox)
200
+ # def compare_answers(html_content):
201
+ # if not quiz_data or not quiz_data.items:
202
+ # return "Please generate a quiz first."
203
+ # # Placeholder for user answers (adjust based on actual UI implementation)
204
+ # user_answers = [] # Implement parsing logic if using radio inputs
205
+ # correct_answers = [item.correct_answer for item in quiz_data.items[:10]]
206
+ # score = sum(1 for u, c in zip(user_answers, correct_answers) if u == c)
207
+ # if score > 7:
208
+ # message = f"### Excellent! You got {score} out of 10!"
209
+ # elif score > 5:
210
+ # message = f"### Good! You got {score} out of 10!"
211
+ # else:
212
+ # message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
213
+ # return message
214
+
215
+ # if __name__ == "__main__":
216
+ # QUIZBOT.queue().launch(debug=True)
217
+
218
+ # # # Importing libraries
219
+ # # import pandas as pd
220
+ # # import json
221
+ # # import gradio as gr
222
+ # # from pathlib import Path
223
+ # # from ragatouille import RAGPretrainedModel
224
+ # # from gradio_client import Client
225
+ # # from tempfile import NamedTemporaryFile
226
+ # # from sentence_transformers import CrossEncoder
227
+ # # import numpy as np
228
+ # # from time import perf_counter
229
+ # # from sentence_transformers import CrossEncoder
230
+
231
+ # # #calling functions from other files - to call the knowledge database tables (lancedb for accurate mode) for creating quiz
232
+ # # from backend.semantic_search import table, retriever
233
+
234
+ # # VECTOR_COLUMN_NAME = "vector"
235
+ # # TEXT_COLUMN_NAME = "text"
236
+ # # proj_dir = Path.cwd()
237
+
238
+ # # # Set up logging
239
+ # # import logging
240
+ # # logging.basicConfig(level=logging.INFO)
241
+ # # logger = logging.getLogger(__name__)
242
+
243
+ # # # Replace Mixtral client with Qwen Client
244
+ # # client = Client("Qwen/Qwen1.5-110B-Chat-demo")
245
+
246
+ # # def system_instructions(question_difficulty, topic, documents_str):
247
+ # # return f"""<s> [INST] You are a great teacher and your task is to create 10 questions with 4 choices with {question_difficulty} difficulty about the topic request "{topic}" only from the below given documents, {documents_str}. Then create answers. Index in JSON format, 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#". Example: 'A10':'Q10:C3' [/INST]"""
248
+
249
+ # # # Ragatouille database for Colbert ie highly accurate mode
250
+ # # RAG_db = gr.State()
251
+ # # quiz_data = None
252
+
253
+
254
+ # # #defining a function to convert json file to excel file
255
+ # # def json_to_excel(output_json):
256
+ # # # Initialize list for DataFrame
257
+ # # data = []
258
+ # # gr.Warning('Generating Shareable file link..', duration=30)
259
+ # # for i in range(1, 11): # Assuming there are 10 questions
260
+ # # question_key = f"Q{i}"
261
+ # # answer_key = f"A{i}"
262
+
263
+ # # question = output_json.get(question_key, '')
264
+ # # correct_answer_key = output_json.get(answer_key, '')
265
+ # # #correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
266
+ # # correct_answer = correct_answer_key.split(':')[-1].replace('C', '').strip() if correct_answer_key else ''
267
+
268
+ # # # Extract options
269
+ # # option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
270
+ # # options = [output_json.get(key, '') for key in option_keys]
271
+
272
+ # # # Add data row
273
+ # # data.append([
274
+ # # question, # Question Text
275
+ # # "Multiple Choice", # Question Type
276
+ # # options[0], # Option 1
277
+ # # options[1], # Option 2
278
+ # # options[2] if len(options) > 2 else '', # Option 3
279
+ # # options[3] if len(options) > 3 else '', # Option 4
280
+ # # options[4] if len(options) > 4 else '', # Option 5
281
+ # # correct_answer, # Correct Answer
282
+ # # 30, # Time in seconds
283
+ # # '' # Image Link
284
+ # # ])
285
+
286
+ # # # Create DataFrame
287
+ # # df = pd.DataFrame(data, columns=[
288
+ # # "Question Text",
289
+ # # "Question Type",
290
+ # # "Option 1",
291
+ # # "Option 2",
292
+ # # "Option 3",
293
+ # # "Option 4",
294
+ # # "Option 5",
295
+ # # "Correct Answer",
296
+ # # "Time in seconds",
297
+ # # "Image Link"
298
+ # # ])
299
+
300
+ # # temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
301
+ # # df.to_excel(temp_file.name, index=False)
302
+ # # return temp_file.name
303
+ # # # Define a colorful theme
304
+ # # colorful_theme = gr.themes.Default(
305
+ # # primary_hue="cyan", # Set a bright cyan as primary color
306
+ # # secondary_hue="yellow", # Set a bright magenta as secondary color
307
+ # # neutral_hue="purple" # Optionally set a neutral color
308
+
309
+ # # )
310
+
311
+ # # #gradio app creation for a user interface
312
+ # # with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
313
+
314
+
315
+ # # # Create a single row for the HTML and Image
316
+ # # with gr.Row():
317
+ # # with gr.Column(scale=2):
318
+ # # gr.Image(value='logo.png', height=200, width=200)
319
+ # # with gr.Column(scale=6):
320
+ # # gr.HTML("""
321
+ # # <center>
322
+ # # <h1><span style="color: purple;">GOVERNMENT HIGH SCHOOL,SUTHUKENY</span> STUDENTS QUIZBOT </h1>
323
+ # # <h2>Generative AI-powered Capacity building for STUDENTS</h2>
324
+ # # <i>⚠️ Students can create quiz from any topic from 10 science and evaluate themselves! ⚠️</i>
325
+ # # </center>
326
+ # # """)
327
+
328
+
329
+
330
+
331
+ # # topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any CHAPTER NAME")
332
+
333
+ # # with gr.Row():
334
+ # # difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
335
+ # # model_radio = gr.Radio(choices=[ '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'],
336
+ # # value='(ACCURATE) BGE reranker', label="Embeddings",
337
+ # # info="First query to ColBERT may take a little time")
338
+
339
+ # # generate_quiz_btn = gr.Button("Generate Quiz!🚀")
340
+ # # quiz_msg = gr.Textbox()
341
+
342
+ # # question_radios = [gr.Radio(visible=False) for _ in range(10)]
343
+
344
+ # # @generate_quiz_btn.click(inputs=[difficulty_radio, topic, model_radio], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")])
345
+ # # def generate_quiz(question_difficulty, topic, cross_encoder):
346
+ # # top_k_rank = 10
347
+ # # documents = []
348
+ # # gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
349
+
350
+ # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
351
+ # # gr.Warning('Retrieving using ColBERT.. First-time query will take 2 minute for model to load.. please wait',duration=100)
352
+ # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
353
+ # # RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
354
+ # # documents_full = RAG_db.value.search(topic, k=top_k_rank)
355
+ # # documents = [item['content'] for item in documents_full]
356
+
357
+ # # else:
358
+ # # document_start = perf_counter()
359
+ # # query_vec = retriever.encode(topic)
360
+ # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
361
+
362
+ # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank).to_list()
363
+ # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
364
+
365
+ # # query_doc_pair = [[topic, doc] for doc in documents]
366
+
367
+ # # # if cross_encoder == '(FAST) MiniLM-L6v2':
368
+ # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
369
+ # # if cross_encoder == '(ACCURATE) BGE reranker':
370
+ # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
371
+
372
+ # # cross_scores = cross_encoder1.predict(query_doc_pair)
373
+ # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
374
+ # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
375
+
376
+ # # #creating a text prompt to Qwen model combining the documents and system instruction
377
+ # # formatted_prompt = system_instructions(question_difficulty, topic, '\n'.join(documents))
378
+ # # print(' Formatted Prompt : ' ,formatted_prompt)
379
+ # # try:
380
+ # # response = client.predict(query=formatted_prompt, history=[], system="You are a helpful assistant.", api_name="/model_chat")
381
+ # # response1 = response[1][0][1]
382
+
383
+ # # # Extract JSON
384
+ # # start_index = response1.find('{')
385
+ # # end_index = response1.rfind('}')
386
+ # # cleaned_response = response1[start_index:end_index + 1] if start_index != -1 and end_index != -1 else ''
387
+ # # print('Cleaned Response :',cleaned_response)
388
+ # # output_json = json.loads(cleaned_response)
389
+ # # # Assign the extracted JSON to quiz_data for use in the comparison function
390
+ # # global quiz_data
391
+ # # quiz_data = output_json
392
+ # # # Generate the Excel file
393
+ # # excel_file = json_to_excel(output_json)
394
+
395
+
396
+ # # #Create a Quiz display in app
397
+ # # question_radio_list = []
398
+ # # for question_num in range(1, 11):
399
+ # # question_key = f"Q{question_num}"
400
+ # # answer_key = f"A{question_num}"
401
+
402
+ # # question = output_json.get(question_key)
403
+ # # answer = output_json.get(output_json.get(answer_key))
404
+
405
+ # # if not question or not answer:
406
+ # # continue
407
+
408
+ # # choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
409
+ # # choice_list = [output_json.get(choice_key, "Choice not found") for choice_key in choice_keys]
410
+
411
+ # # radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
412
+ # # question_radio_list.append(radio)
413
+
414
+ # # return ['Quiz Generated!'] + question_radio_list + [excel_file]
415
+
416
+ # # except json.JSONDecodeError as e:
417
+ # # print(f"Failed to decode JSON: {e}")
418
+
419
+ # # check_button = gr.Button("Check Score")
420
+ # # score_textbox = gr.Markdown()
421
+
422
+ # # @check_button.click(inputs=question_radios, outputs=score_textbox)
423
+ # # def compare_answers(*user_answers):
424
+ # # user_answer_list = list(user_answers)
425
+ # # answers_list = []
426
+
427
+ # # for question_num in range(1, 11):
428
+ # # answer_key = f"A{question_num}"
429
+ # # answer = quiz_data.get(quiz_data.get(answer_key))
430
+ # # if not answer:
431
+ # # break
432
+ # # answers_list.append(answer)
433
+
434
+ # # score = sum(1 for item in user_answer_list if item in answers_list)
435
+
436
+ # # if score > 7:
437
+ # # message = f"### Excellent! You got {score} out of 10!"
438
+ # # elif score > 5:
439
+ # # message = f"### Good! You got {score} out of 10!"
440
+ # # else:
441
+ # # message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
442
+
443
+ # # return message
444
+
445
+ # # QUIZBOT.queue()
446
+ # # QUIZBOT.launch(debug=True)
447
+