Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,241 +1,4 @@
|
|
1 |
|
2 |
-
# ##############??????????????????????????????
|
3 |
-
# import pandas as pd
|
4 |
-
# import json
|
5 |
-
# import gradio as gr
|
6 |
-
# from pathlib import Path
|
7 |
-
# from ragatouille import RAGPretrainedModel
|
8 |
-
# from gradio_client import Client
|
9 |
-
# from jinja2 import Environment, FileSystemLoader
|
10 |
-
# from tempfile import NamedTemporaryFile
|
11 |
-
|
12 |
-
# VECTOR_COLUMN_NAME = "vector"
|
13 |
-
# TEXT_COLUMN_NAME = "text"
|
14 |
-
# #proj_dir = Path(__file__).parent
|
15 |
-
# proj_dir = Path.cwd()
|
16 |
-
# # Setting up the logging
|
17 |
-
# import logging
|
18 |
-
# logging.basicConfig(level=logging.INFO)
|
19 |
-
# logger = logging.getLogger(__name__)
|
20 |
-
|
21 |
-
# # Replace Mixtral client with Qwen Client
|
22 |
-
# client = Client("Qwen/Qwen1.5-110B-Chat-demo")
|
23 |
-
|
24 |
-
# # Set up the template environment with the templates directory
|
25 |
-
# env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
|
26 |
-
|
27 |
-
# # Load the templates directly from the environment
|
28 |
-
# template = env.get_template('template.j2')
|
29 |
-
# template_html = env.get_template('template_html.j2')
|
30 |
-
|
31 |
-
# def system_instructions(question_difficulty, topic, documents_str):
|
32 |
-
# 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 is 'A10':'Q10:C3' [/INST]"""
|
33 |
-
|
34 |
-
# # RA
|
35 |
-
# RAG_db = gr.State()
|
36 |
-
|
37 |
-
# def json_to_excel(output_json):
|
38 |
-
# # Initialize list for DataFrame
|
39 |
-
# data = []
|
40 |
-
# gr.Warning('Generating Shareable file link..',duration=30)
|
41 |
-
# for i in range(1, 11): # Assuming there are 10 questions
|
42 |
-
# question_key = f"Q{i}"
|
43 |
-
# answer_key = f"A{i}"
|
44 |
-
|
45 |
-
# question = output_json.get(question_key, '')
|
46 |
-
# correct_answer_key = output_json.get(answer_key, '')
|
47 |
-
# correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
|
48 |
-
|
49 |
-
# # Extract options
|
50 |
-
# option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
|
51 |
-
# options = [output_json.get(key, '') for key in option_keys]
|
52 |
-
|
53 |
-
# # Add data row
|
54 |
-
# data.append([
|
55 |
-
# question, # Question Text
|
56 |
-
# "Multiple Choice", # Question Type
|
57 |
-
# options[0], # Option 1
|
58 |
-
# options[1], # Option 2
|
59 |
-
# options[2] if len(options) > 2 else '', # Option 3
|
60 |
-
# options[3] if len(options) > 3 else '', # Option 4
|
61 |
-
# options[4] if len(options) > 4 else '', # Option 5
|
62 |
-
# correct_answer, # Correct Answer
|
63 |
-
# 30, # Time in seconds
|
64 |
-
# '' # Image Link
|
65 |
-
# ])
|
66 |
-
|
67 |
-
# # Create DataFrame
|
68 |
-
# df = pd.DataFrame(data, columns=[
|
69 |
-
# "Question Text",
|
70 |
-
# "Question Type",
|
71 |
-
# "Option 1",
|
72 |
-
# "Option 2",
|
73 |
-
# "Option 3",
|
74 |
-
# "Option 4",
|
75 |
-
# "Option 5",
|
76 |
-
# "Correct Answer",
|
77 |
-
# "Time in seconds",
|
78 |
-
# "Image Link"
|
79 |
-
# ])
|
80 |
-
# # Create a temporary file and save the DataFrame to it
|
81 |
-
# temp_file = NamedTemporaryFile(delete=False, suffix=".xlsx")
|
82 |
-
# df.to_excel(temp_file.name, index=False)
|
83 |
-
|
84 |
-
# # # Save to Excel file
|
85 |
-
# # excel_path = proj_dir / "quiz_questions.xlsx"
|
86 |
-
# # df.to_excel(str(excel_path), index=False)
|
87 |
-
# return temp_file.name
|
88 |
-
|
89 |
-
# with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green"), css="style.css") as QUIZBOT:
|
90 |
-
# def load_model():
|
91 |
-
# RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
92 |
-
# RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
93 |
-
# return 'Ready to Go!!'
|
94 |
-
|
95 |
-
# with gr.Column(scale=4):
|
96 |
-
# gr.HTML("""
|
97 |
-
# <center>
|
98 |
-
# <h1><span style="color: purple;">ADWITIYA</span> Customs Manual Quizbot</h1>
|
99 |
-
# <h2>Generative AI-powered Capacity building for Training Officers</h2>
|
100 |
-
# <i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
|
101 |
-
# </center>
|
102 |
-
# """)
|
103 |
-
|
104 |
-
# with gr.Column(scale=2):
|
105 |
-
# load_btn = gr.Button("Click to Load!🚀")
|
106 |
-
# load_text = gr.Textbox()
|
107 |
-
# load_btn.click(load_model, [], load_text)
|
108 |
-
|
109 |
-
# topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
|
110 |
-
|
111 |
-
# with gr.Row():
|
112 |
-
# radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
|
113 |
-
|
114 |
-
# generate_quiz_btn = gr.Button("Generate Quiz!🚀")
|
115 |
-
# quiz_msg = gr.Textbox()
|
116 |
-
|
117 |
-
# question_radios = [gr.Radio(visible=False) for _ in range(10)]
|
118 |
-
|
119 |
-
# #@gr.dependencies.GPU
|
120 |
-
# @generate_quiz_btn.click(inputs=[radio, topic], outputs=[quiz_msg] + question_radios + [gr.File(label="Download Excel")], api_name="generate_quiz")
|
121 |
-
# def generate_quiz(question_difficulty, topic):
|
122 |
-
# top_k_rank = 10
|
123 |
-
# RAG_db_ = RAG_db.value
|
124 |
-
# documents_full = RAG_db_.search(topic, k=top_k_rank)
|
125 |
-
|
126 |
-
# gr.Warning('Generation of Quiz may take 1 to 2 minutes. Please wait.',duration=60)
|
127 |
-
|
128 |
-
# question_radio_list = []
|
129 |
-
# excel_file = None
|
130 |
-
|
131 |
-
# count = 0
|
132 |
-
# while count <= 3:
|
133 |
-
# try:
|
134 |
-
# documents = [item['content'] for item in documents_full]
|
135 |
-
# document_summaries = [f"[DOCUMENT {i + 1}]: {summary}{count}" for i, summary in enumerate(documents)]
|
136 |
-
# documents_str = '\n'.join(document_summaries)
|
137 |
-
# formatted_prompt = system_instructions(question_difficulty, topic, documents_str)
|
138 |
-
|
139 |
-
# print(formatted_prompt)
|
140 |
-
|
141 |
-
# # Use Qwen Client for quiz generation
|
142 |
-
# response = client.predict(
|
143 |
-
# query=formatted_prompt,
|
144 |
-
# history=[],
|
145 |
-
# system="You are a helpful assistant.",
|
146 |
-
# api_name="/model_chat"
|
147 |
-
# )
|
148 |
-
# print(response)
|
149 |
-
# response1 = response[1][0][1]
|
150 |
-
|
151 |
-
# # Find the first and last curly braces
|
152 |
-
# start_index = response1.find('{')
|
153 |
-
# end_index = response1.rfind('}')
|
154 |
-
|
155 |
-
# # Extract only the JSON part
|
156 |
-
# if start_index != -1 and end_index != -1:
|
157 |
-
# cleaned_response = response1[start_index:end_index + 1]
|
158 |
-
|
159 |
-
# # Try parsing the cleaned JSON
|
160 |
-
# try:
|
161 |
-
# output_json = json.loads(cleaned_response)
|
162 |
-
# print('Parsed JSON:', output_json)
|
163 |
-
# global quiz_data
|
164 |
-
# quiz_data = output_json
|
165 |
-
|
166 |
-
# # Generate the Excel file
|
167 |
-
# excel_file = json_to_excel(output_json)
|
168 |
-
|
169 |
-
# for question_num in range(1, 11):
|
170 |
-
# question_key = f"Q{question_num}"
|
171 |
-
# answer_key = f"A{question_num}"
|
172 |
-
|
173 |
-
# question = quiz_data.get(question_key)
|
174 |
-
# answer = quiz_data.get(quiz_data.get(answer_key))
|
175 |
-
|
176 |
-
# if not question or not answer:
|
177 |
-
# continue
|
178 |
-
|
179 |
-
# choice_keys = [f"{question_key}:C{i}" for i in range(1, 5)]
|
180 |
-
# choice_list = [quiz_data.get(choice_key, "Choice not found") for choice_key in choice_keys]
|
181 |
-
|
182 |
-
# radio = gr.Radio(choices=choice_list, label=question, visible=True, interactive=True)
|
183 |
-
# question_radio_list.append(radio)
|
184 |
-
# print('question_radio_list', question_radio_list)
|
185 |
-
|
186 |
-
# if len(question_radio_list) == 10:
|
187 |
-
# break
|
188 |
-
# else:
|
189 |
-
# print('10 questions not generated. Trying again!')
|
190 |
-
# count += 1
|
191 |
-
# continue
|
192 |
-
# except json.JSONDecodeError as e:
|
193 |
-
# print(f"Failed to decode JSON: {e}")
|
194 |
-
# else:
|
195 |
-
# print("No valid JSON found in the response.")
|
196 |
-
|
197 |
-
# except Exception as e:
|
198 |
-
# count += 1
|
199 |
-
# print(f"Exception occurred: {e}")
|
200 |
-
# if count == 3:
|
201 |
-
# print('Retry exhausted')
|
202 |
-
# gr.Warning('Sorry. Please try with another topic!')
|
203 |
-
# else:
|
204 |
-
# print(f"Trying again.. {count} time... please wait")
|
205 |
-
# continue
|
206 |
-
|
207 |
-
# return ['Quiz Generated!'] + question_radio_list + [excel_file]
|
208 |
-
|
209 |
-
# check_button = gr.Button("Check Score")
|
210 |
-
# score_textbox = gr.Markdown()
|
211 |
-
|
212 |
-
# @check_button.click(inputs=question_radios, outputs=score_textbox)
|
213 |
-
# def compare_answers(*user_answers):
|
214 |
-
# user_answer_list = list(user_answers)
|
215 |
-
# answers_list = []
|
216 |
-
|
217 |
-
# for question_num in range(1, 20):
|
218 |
-
# answer_key = f"A{question_num}"
|
219 |
-
# answer = quiz_data.get(quiz_data.get(answer_key))
|
220 |
-
# if not answer:
|
221 |
-
# break
|
222 |
-
# answers_list.append(answer)
|
223 |
-
|
224 |
-
# score = sum(1 for item in user_answer_list if item in answers_list)
|
225 |
-
|
226 |
-
# if score > 7:
|
227 |
-
# message = f"### Excellent! You got {score} out of 10!"
|
228 |
-
# elif score > 5:
|
229 |
-
# message = f"### Good! You got {score} out of 10!"
|
230 |
-
# else:
|
231 |
-
# message = f"### You got {score} out of 10! Don't worry. You can prepare well and try better next time!"
|
232 |
-
|
233 |
-
# return message
|
234 |
-
|
235 |
-
# QUIZBOT.queue()
|
236 |
-
# QUIZBOT.launch(debug=True)
|
237 |
-
|
238 |
-
#?????????????????????????????????
|
239 |
import pandas as pd
|
240 |
import json
|
241 |
import gradio as gr
|
@@ -317,6 +80,7 @@ def json_to_excel(output_json):
|
|
317 |
return temp_file.name
|
318 |
|
319 |
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green")) as QUIZBOT:
|
|
|
320 |
with gr.Column(scale=4):
|
321 |
gr.HTML("""
|
322 |
<center>
|
@@ -325,6 +89,8 @@ with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green",
|
|
325 |
<i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
|
326 |
</center>
|
327 |
""")
|
|
|
|
|
328 |
|
329 |
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
|
330 |
|
|
|
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
import json
|
4 |
import gradio as gr
|
|
|
80 |
return temp_file.name
|
81 |
|
82 |
with gr.Blocks(title="Quiz Maker", theme=gr.themes.Default(primary_hue="green", secondary_hue="green")) as QUIZBOT:
|
83 |
+
|
84 |
with gr.Column(scale=4):
|
85 |
gr.HTML("""
|
86 |
<center>
|
|
|
89 |
<i>⚠️ NACIN Faculties create quiz from any topic dynamically for classroom evaluation after their sessions ! ⚠️</i>
|
90 |
</center>
|
91 |
""")
|
92 |
+
with gr.Column(scale=3):
|
93 |
+
gr.Image(value='logo.png', height=200, width=200)
|
94 |
|
95 |
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from Customs Manual")
|
96 |
|