RAMYASRI-39
commited on
Commit
•
de7b575
1
Parent(s):
b705cd6
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,3 @@
|
|
1 |
-
import subprocess
|
2 |
-
import sys
|
3 |
-
|
4 |
-
# Upgrade pip, setuptools, and wheel
|
5 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel"])
|
6 |
-
|
7 |
-
# Install dependencies from requirements.txt
|
8 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
|
9 |
-
|
10 |
import pandas as pd
|
11 |
import json
|
12 |
import gradio as gr
|
@@ -17,9 +8,8 @@ from tempfile import NamedTemporaryFile
|
|
17 |
from sentence_transformers import CrossEncoder
|
18 |
import numpy as np
|
19 |
from time import perf_counter
|
20 |
-
from sentence_transformers import CrossEncoder
|
21 |
|
22 |
-
#calling functions from other files - to call the knowledge database tables (lancedb for accurate mode) for creating quiz
|
23 |
from backend.semantic_search import table, retriever
|
24 |
|
25 |
VECTOR_COLUMN_NAME = "vector"
|
@@ -31,20 +21,15 @@ import logging
|
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
logger = logging.getLogger(__name__)
|
33 |
|
34 |
-
# Replace Mixtral client with Qwen Client
|
35 |
client = Client("Qwen/Qwen1.5-110B-Chat-demo")
|
36 |
|
37 |
def system_instructions(question_difficulty, topic, documents_str):
|
38 |
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]"""
|
39 |
|
40 |
-
# Ragatouille database for Colbert ie highly accurate mode
|
41 |
RAG_db = gr.State()
|
42 |
quiz_data = None
|
43 |
|
44 |
-
|
45 |
-
#defining a function to convert json file to excel file
|
46 |
def json_to_excel(output_json):
|
47 |
-
# Initialize list for DataFrame
|
48 |
data = []
|
49 |
gr.Warning('Generating Shareable file link..', duration=30)
|
50 |
for i in range(1, 11): # Assuming there are 10 questions
|
@@ -53,57 +38,35 @@ def json_to_excel(output_json):
|
|
53 |
|
54 |
question = output_json.get(question_key, '')
|
55 |
correct_answer_key = output_json.get(answer_key, '')
|
56 |
-
#correct_answer = correct_answer_key.split(':')[-1] if correct_answer_key else ''
|
57 |
correct_answer = correct_answer_key.split(':')[-1].replace('C', '').strip() if correct_answer_key else ''
|
58 |
|
59 |
-
# Extract options
|
60 |
option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
|
61 |
options = [output_json.get(key, '') for key in option_keys]
|
62 |
-
|
63 |
-
# Add data row
|
64 |
data.append([
|
65 |
-
question,
|
66 |
-
"Multiple Choice",
|
67 |
-
options[0],
|
68 |
-
options[1],
|
69 |
-
options[2] if len(options) > 2 else '',
|
70 |
-
options[3] if len(options) > 3 else '',
|
71 |
-
options[4] if len(options) > 4 else '',
|
72 |
-
correct_answer,
|
73 |
-
30,
|
74 |
-
''
|
75 |
])
|
76 |
|
77 |
-
# Create DataFrame
|
78 |
df = pd.DataFrame(data, columns=[
|
79 |
-
"Question Text",
|
80 |
-
"Question Type",
|
81 |
-
"Option 1",
|
82 |
-
"Option 2",
|
83 |
-
"Option 3",
|
84 |
-
"Option 4",
|
85 |
-
"Option 5",
|
86 |
-
"Correct Answer",
|
87 |
-
"Time in seconds",
|
88 |
-
"Image Link"
|
89 |
])
|
90 |
|
91 |
-
temp_file = NamedTemporaryFile(delete=
|
92 |
df.to_excel(temp_file.name, index=False)
|
93 |
return temp_file.name
|
94 |
-
# Define a colorful theme
|
95 |
-
colorful_theme = gr.themes.Default(
|
96 |
-
primary_hue="cyan", # Set a bright cyan as primary color
|
97 |
-
secondary_hue="yellow", # Set a bright magenta as secondary color
|
98 |
-
neutral_hue="purple" # Optionally set a neutral color
|
99 |
-
|
100 |
-
)
|
101 |
|
102 |
-
|
|
|
103 |
with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
104 |
-
|
105 |
-
|
106 |
-
# Create a single row for the HTML and Image
|
107 |
with gr.Row():
|
108 |
with gr.Column(scale=2):
|
109 |
gr.Image(value='logo.png', height=200, width=200)
|
@@ -115,17 +78,11 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
115 |
<i>⚠️ Students can create quiz from any topic from 9 science and evaluate themselves! ⚠️</i>
|
116 |
</center>
|
117 |
""")
|
118 |
-
|
119 |
|
120 |
-
|
121 |
-
|
122 |
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from 9TH Science cbse")
|
123 |
-
|
124 |
with gr.Row():
|
125 |
difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
|
126 |
-
model_radio = gr.Radio(choices=[ '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'],
|
127 |
-
value='(ACCURATE) BGE reranker', label="Embeddings",
|
128 |
-
info="First query to ColBERT may take a little time")
|
129 |
|
130 |
generate_quiz_btn = gr.Button("Generate Quiz!🚀")
|
131 |
quiz_msg = gr.Textbox()
|
@@ -139,7 +96,7 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
139 |
gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
|
140 |
|
141 |
if cross_encoder == '(HIGH ACCURATE) ColBERT':
|
142 |
-
gr.Warning('Retrieving using ColBERT.. First-time query will take 2 minute for model to load.. please wait',duration=100)
|
143 |
RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
144 |
RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
145 |
documents_full = RAG_db.value.search(topic, k=top_k_rank)
|
@@ -154,9 +111,6 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
154 |
documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
|
155 |
|
156 |
query_doc_pair = [[topic, doc] for doc in documents]
|
157 |
-
|
158 |
-
# if cross_encoder == '(FAST) MiniLM-L6v2':
|
159 |
-
# cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
160 |
if cross_encoder == '(ACCURATE) BGE reranker':
|
161 |
cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
|
162 |
|
@@ -164,34 +118,28 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
164 |
sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
165 |
documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
|
166 |
|
167 |
-
#creating a text prompt to Qwen model combining the documents and system instruction
|
168 |
formatted_prompt = system_instructions(question_difficulty, topic, '\n'.join(documents))
|
169 |
-
print('
|
170 |
try:
|
171 |
response = client.predict(query=formatted_prompt, history=[], system="You are a helpful assistant.", api_name="/model_chat")
|
172 |
response1 = response[1][0][1]
|
173 |
|
174 |
-
# Extract JSON
|
175 |
start_index = response1.find('{')
|
176 |
end_index = response1.rfind('}')
|
177 |
cleaned_response = response1[start_index:end_index + 1] if start_index != -1 and end_index != -1 else ''
|
178 |
-
print('Cleaned Response
|
179 |
output_json = json.loads(cleaned_response)
|
180 |
-
# Assign the extracted JSON to quiz_data for use in the comparison function
|
181 |
global quiz_data
|
182 |
quiz_data = output_json
|
183 |
-
# Generate the Excel file
|
184 |
excel_file = json_to_excel(output_json)
|
185 |
-
|
186 |
|
187 |
-
#Create a Quiz display in app
|
188 |
question_radio_list = []
|
189 |
for question_num in range(1, 11):
|
190 |
question_key = f"Q{question_num}"
|
191 |
answer_key = f"A{question_num}"
|
192 |
|
193 |
question = output_json.get(question_key)
|
194 |
-
answer = output_json.get(
|
195 |
|
196 |
if not question or not answer:
|
197 |
continue
|
@@ -205,7 +153,7 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
205 |
return ['Quiz Generated!'] + question_radio_list + [excel_file]
|
206 |
|
207 |
except json.JSONDecodeError as e:
|
208 |
-
|
209 |
|
210 |
check_button = gr.Button("Check Score")
|
211 |
score_textbox = gr.Markdown()
|
@@ -217,7 +165,7 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
217 |
|
218 |
for question_num in range(1, 20):
|
219 |
answer_key = f"A{question_num}"
|
220 |
-
answer = quiz_data.get(
|
221 |
if not answer:
|
222 |
break
|
223 |
answers_list.append(answer)
|
@@ -235,4 +183,3 @@ with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
235 |
|
236 |
QUIZBOT.queue()
|
237 |
QUIZBOT.launch(debug=True)
|
238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
import json
|
3 |
import gradio as gr
|
|
|
8 |
from sentence_transformers import CrossEncoder
|
9 |
import numpy as np
|
10 |
from time import perf_counter
|
|
|
11 |
|
12 |
+
# calling functions from other files - to call the knowledge database tables (lancedb for accurate mode) for creating quiz
|
13 |
from backend.semantic_search import table, retriever
|
14 |
|
15 |
VECTOR_COLUMN_NAME = "vector"
|
|
|
21 |
logging.basicConfig(level=logging.INFO)
|
22 |
logger = logging.getLogger(__name__)
|
23 |
|
|
|
24 |
client = Client("Qwen/Qwen1.5-110B-Chat-demo")
|
25 |
|
26 |
def system_instructions(question_difficulty, topic, documents_str):
|
27 |
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]"""
|
28 |
|
|
|
29 |
RAG_db = gr.State()
|
30 |
quiz_data = None
|
31 |
|
|
|
|
|
32 |
def json_to_excel(output_json):
|
|
|
33 |
data = []
|
34 |
gr.Warning('Generating Shareable file link..', duration=30)
|
35 |
for i in range(1, 11): # Assuming there are 10 questions
|
|
|
38 |
|
39 |
question = output_json.get(question_key, '')
|
40 |
correct_answer_key = output_json.get(answer_key, '')
|
|
|
41 |
correct_answer = correct_answer_key.split(':')[-1].replace('C', '').strip() if correct_answer_key else ''
|
42 |
|
|
|
43 |
option_keys = [f"{question_key}:C{i}" for i in range(1, 6)]
|
44 |
options = [output_json.get(key, '') for key in option_keys]
|
45 |
+
|
|
|
46 |
data.append([
|
47 |
+
question,
|
48 |
+
"Multiple Choice",
|
49 |
+
options[0],
|
50 |
+
options[1],
|
51 |
+
options[2] if len(options) > 2 else '',
|
52 |
+
options[3] if len(options) > 3 else '',
|
53 |
+
options[4] if len(options) > 4 else '',
|
54 |
+
correct_answer,
|
55 |
+
30,
|
56 |
+
''
|
57 |
])
|
58 |
|
|
|
59 |
df = pd.DataFrame(data, columns=[
|
60 |
+
"Question Text", "Question Type", "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Correct Answer", "Time in seconds", "Image Link"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
])
|
62 |
|
63 |
+
temp_file = NamedTemporaryFile(delete=True, suffix=".xlsx")
|
64 |
df.to_excel(temp_file.name, index=False)
|
65 |
return temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
colorful_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")
|
68 |
+
|
69 |
with gr.Blocks(title="Quiz Maker", theme=colorful_theme) as QUIZBOT:
|
|
|
|
|
|
|
70 |
with gr.Row():
|
71 |
with gr.Column(scale=2):
|
72 |
gr.Image(value='logo.png', height=200, width=200)
|
|
|
78 |
<i>⚠️ Students can create quiz from any topic from 9 science and evaluate themselves! ⚠️</i>
|
79 |
</center>
|
80 |
""")
|
|
|
81 |
|
|
|
|
|
82 |
topic = gr.Textbox(label="Enter the Topic for Quiz", placeholder="Write any topic/details from 9TH Science cbse")
|
|
|
83 |
with gr.Row():
|
84 |
difficulty_radio = gr.Radio(["easy", "average", "hard"], label="How difficult should the quiz be?")
|
85 |
+
model_radio = gr.Radio(choices=[ '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'], value='(ACCURATE) BGE reranker', label="Embeddings")
|
|
|
|
|
86 |
|
87 |
generate_quiz_btn = gr.Button("Generate Quiz!🚀")
|
88 |
quiz_msg = gr.Textbox()
|
|
|
96 |
gr.Warning('Generating Quiz may take 1-2 minutes. Please wait.', duration=60)
|
97 |
|
98 |
if cross_encoder == '(HIGH ACCURATE) ColBERT':
|
99 |
+
gr.Warning('Retrieving using ColBERT.. First-time query will take 2 minute for model to load.. please wait', duration=100)
|
100 |
RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
|
101 |
RAG_db.value = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
|
102 |
documents_full = RAG_db.value.search(topic, k=top_k_rank)
|
|
|
111 |
documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
|
112 |
|
113 |
query_doc_pair = [[topic, doc] for doc in documents]
|
|
|
|
|
|
|
114 |
if cross_encoder == '(ACCURATE) BGE reranker':
|
115 |
cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
|
116 |
|
|
|
118 |
sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
119 |
documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
|
120 |
|
|
|
121 |
formatted_prompt = system_instructions(question_difficulty, topic, '\n'.join(documents))
|
122 |
+
print('Formatted Prompt: ', formatted_prompt)
|
123 |
try:
|
124 |
response = client.predict(query=formatted_prompt, history=[], system="You are a helpful assistant.", api_name="/model_chat")
|
125 |
response1 = response[1][0][1]
|
126 |
|
|
|
127 |
start_index = response1.find('{')
|
128 |
end_index = response1.rfind('}')
|
129 |
cleaned_response = response1[start_index:end_index + 1] if start_index != -1 and end_index != -1 else ''
|
130 |
+
print('Cleaned Response:', cleaned_response)
|
131 |
output_json = json.loads(cleaned_response)
|
|
|
132 |
global quiz_data
|
133 |
quiz_data = output_json
|
|
|
134 |
excel_file = json_to_excel(output_json)
|
|
|
135 |
|
|
|
136 |
question_radio_list = []
|
137 |
for question_num in range(1, 11):
|
138 |
question_key = f"Q{question_num}"
|
139 |
answer_key = f"A{question_num}"
|
140 |
|
141 |
question = output_json.get(question_key)
|
142 |
+
answer = output_json.get(answer_key)
|
143 |
|
144 |
if not question or not answer:
|
145 |
continue
|
|
|
153 |
return ['Quiz Generated!'] + question_radio_list + [excel_file]
|
154 |
|
155 |
except json.JSONDecodeError as e:
|
156 |
+
logger.error(f"Failed to decode JSON: {e}")
|
157 |
|
158 |
check_button = gr.Button("Check Score")
|
159 |
score_textbox = gr.Markdown()
|
|
|
165 |
|
166 |
for question_num in range(1, 20):
|
167 |
answer_key = f"A{question_num}"
|
168 |
+
answer = quiz_data.get(answer_key)
|
169 |
if not answer:
|
170 |
break
|
171 |
answers_list.append(answer)
|
|
|
183 |
|
184 |
QUIZBOT.queue()
|
185 |
QUIZBOT.launch(debug=True)
|
|