Spaces:
Runtime error
Runtime error
Commit
·
762e183
1
Parent(s):
16e1447
Update app.py
Browse files
app.py
CHANGED
@@ -84,7 +84,7 @@ paragraphs = [f"{sentences[i]} {sentences[i + 1]}" if i + 1 < len(sentences) els
|
|
84 |
|
85 |
class SubjectiveTest:
|
86 |
|
87 |
-
def
|
88 |
self.summary = data
|
89 |
self.noOfQues = noOfQues
|
90 |
self.nlp = spacy.load("en_core_web_sm")
|
@@ -113,7 +113,7 @@ class SubjectiveTest:
|
|
113 |
for ent in sentence.ents:
|
114 |
entity_label = ent.label_
|
115 |
entity_text = ent.text
|
116 |
-
question_patterns = self.adjust_question_pattern(entity_label,"")
|
117 |
for pattern in question_patterns:
|
118 |
question = pattern.format(entity=entity_text, topic=topic)
|
119 |
if entity_label in question_answer_dict:
|
@@ -132,7 +132,6 @@ class SubjectiveTest:
|
|
132 |
|
133 |
return questions
|
134 |
|
135 |
-
|
136 |
# Use st.form to encapsulate the quiz generation and submission
|
137 |
with st.form("quiz_form"):
|
138 |
# Create a button to initiate quiz generation
|
@@ -145,53 +144,51 @@ if generate_quiz:
|
|
145 |
|
146 |
subjective_generator = SubjectiveTest(data, noOfQues)
|
147 |
questions = subjective_generator.generate_test(topic) # Use the user's input topic here
|
148 |
-
st.write(questions)
|
149 |
-
# Create a form for the quiz
|
150 |
-
with st.form("quiz_submission"):
|
151 |
-
st.write("Fill out the quiz:")
|
152 |
-
answers = {}
|
153 |
-
for i, question in enumerate(questions):
|
154 |
-
res = st.text_input(f'Q{i + 1}: {question}')
|
155 |
-
answers[f'Q{i + 1}'] = res
|
156 |
-
|
157 |
-
if st.form_submit_button("Submit"):
|
158 |
-
scores = []
|
159 |
-
client = Client("https://billbojangeles2000-zephyr-7b-alpha-chatbot-karki.hf.space/")
|
160 |
-
|
161 |
-
question_list = subjective_generator.generate_test(topic) # Define 'questions' here
|
162 |
-
questions = []
|
163 |
-
for i, question in enumerate(question_list):
|
164 |
-
if (question != "") and (len(tool.check(question)) == 0) and (grammar_sense(question)):
|
165 |
-
questions.append(f"Question: {question}")
|
166 |
-
|
167 |
-
for i, question in enumerate(questions):
|
168 |
-
res = answers[f'Q{i + 1}']
|
169 |
-
if res:
|
170 |
-
result = client.predict(
|
171 |
-
f'What would you rate this answer to the question: "{question}" as a percentage? Here is the answer: {res}. Make sure to write your answer as "Score" and then write your score of the response.',
|
172 |
-
0.9,
|
173 |
-
256,
|
174 |
-
0.9,
|
175 |
-
1.2,
|
176 |
-
api_name="/chat"
|
177 |
-
)
|
178 |
-
pattern = r'(\d+)%'
|
179 |
-
match = re.search(pattern, result)
|
180 |
-
if match:
|
181 |
-
score = int(match.group(1))
|
182 |
-
scores.append(score)
|
183 |
-
else:
|
184 |
-
scores.append(85)
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
return 0 # Return 0 for an empty list to avoid division by zero.
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
|
|
|
|
|
|
|
|
193 |
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
class SubjectiveTest:
|
86 |
|
87 |
+
def __init(self, data, noOfQues):
|
88 |
self.summary = data
|
89 |
self.noOfQues = noOfQues
|
90 |
self.nlp = spacy.load("en_core_web_sm")
|
|
|
113 |
for ent in sentence.ents:
|
114 |
entity_label = ent.label_
|
115 |
entity_text = ent.text
|
116 |
+
question_patterns = self.adjust_question_pattern(entity_label, "")
|
117 |
for pattern in question_patterns:
|
118 |
question = pattern.format(entity=entity_text, topic=topic)
|
119 |
if entity_label in question_answer_dict:
|
|
|
132 |
|
133 |
return questions
|
134 |
|
|
|
135 |
# Use st.form to encapsulate the quiz generation and submission
|
136 |
with st.form("quiz_form"):
|
137 |
# Create a button to initiate quiz generation
|
|
|
144 |
|
145 |
subjective_generator = SubjectiveTest(data, noOfQues)
|
146 |
questions = subjective_generator.generate_test(topic) # Use the user's input topic here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
+
# Filter out invalid and empty questions
|
149 |
+
questions = [q for q in questions if q.strip() and q != "E x p l a"]
|
|
|
150 |
|
151 |
+
# Ensure you have valid questions
|
152 |
+
if not questions:
|
153 |
+
st.write("No valid questions to process.")
|
154 |
+
else:
|
155 |
+
# Use the filtered questions in your code
|
156 |
+
scores = []
|
157 |
+
client = Client("https://billbojangeles2000-zephyr-7b-alpha-chatbot-karki.hf.space/")
|
158 |
|
159 |
+
question_list = subjective_generator.generate_test(topic) # Define 'questions' here
|
160 |
+
questions = []
|
161 |
+
for i, question in enumerate(question_list):
|
162 |
+
if (question != "") and (len(tool.check(question)) == 0) and (grammar_sense(question)):
|
163 |
+
questions.append(f"Question: {question}")
|
164 |
|
165 |
+
for i, question in enumerate(questions):
|
166 |
+
res = answers[f'Q{i + 1}']
|
167 |
+
if res:
|
168 |
+
result = client.predict(
|
169 |
+
f'What would you rate this answer to the question: "{question}" as a percentage? Here is the answer: {res}. Make sure to write your answer as "Score" and then write your score of the response.',
|
170 |
+
0.9,
|
171 |
+
256,
|
172 |
+
0.9,
|
173 |
+
1.2,
|
174 |
+
api_name="/chat"
|
175 |
+
)
|
176 |
+
pattern = r'(\d+)%'
|
177 |
+
match = re.search(pattern, result)
|
178 |
+
if match:
|
179 |
+
score = int(match.group(1))
|
180 |
+
scores.append(score)
|
181 |
+
else:
|
182 |
+
scores.append(85)
|
183 |
+
|
184 |
+
def calculate_average(numbers):
|
185 |
+
if not numbers:
|
186 |
+
return 0 # Return 0 for an empty list to avoid division by zero.
|
187 |
+
|
188 |
+
total = sum(numbers)
|
189 |
+
average = total / len(numbers)
|
190 |
+
return average
|
191 |
+
|
192 |
+
# Calculate and display the average score
|
193 |
+
average_score = calculate_average(scores)
|
194 |
+
st.write(f'Your average score is {average_score}')
|