Spaces:
Sleeping
Sleeping
File size: 23,113 Bytes
5f097cc 03dba14 0807180 03dba14 0807180 03dba14 5f097cc 03dba14 0807180 03dba14 c74f24a 0807180 e2e7d60 0807180 e2e7d60 0807180 e2e7d60 0807180 e2e7d60 0807180 c74f24a 0807180 c74f24a 0807180 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
import streamlit as st
import sqlite3
import time
import datetime
from PIL import Image
import google.generativeai as genai
import os
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, letter
from io import BytesIO
import tempfile
import json
import re
from reportlab.platypus import Paragraph, Frame, Spacer
from reportlab.lib.styles import getSampleStyleSheet
import shutil
MODEL_ID = "gemini-2.0-flash-exp"
api_key = os.getenv("GEMINI_API_KEY")
model_id = MODEL_ID
genai.configure(api_key=api_key)
enable_stream = False
if "model" not in st.session_state:
st.session_state.model = genai.GenerativeModel(MODEL_ID)
if "chat" not in st.session_state:
st.session_state.chat = st.session_state.model.start_chat()
def get_system_instruction(username):
""" Retrieves the system instruction for the user from the database. """
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('SELECT instruction FROM system_instructions WHERE username=?', (username,))
instruction = c.fetchone()
conn.close()
if instruction:
return instruction[0]
else:
return "Default system instruction."
def save_user_prompt(username, prompt_time, prompt_type):
""" Saves the user prompt to the database for monitoring purposes. """
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('INSERT INTO user_prompts(username, prompt_time, prompt_type) VALUES (?,?,?)', (username, prompt_time, prompt_type))
conn.commit()
conn.close()
def merge_json_strings(json_str1, json_str2):
"""
Merges two JSON strings into one, handling potential markdown tags.
Args:
json_str1: The first JSON string, potentially with markdown tags.
json_str2: The second JSON string, potentially with markdown tags.
Returns:
A cleaned JSON string representing the merged JSON objects.
"""
# Clean the JSON strings by removing markdown tags
cleaned_json_str1 = _clean_markdown(json_str1)
cleaned_json_str2 = _clean_markdown(json_str2)
try:
# Parse the cleaned JSON strings into Python dictionaries
data1 = json.loads(cleaned_json_str1)
data2 = json.loads(cleaned_json_str2)
# Merge the dictionaries
merged_data = _merge_dicts(data1, data2)
# Convert the merged dictionary back into a JSON string
return json.dumps(merged_data, indent=2)
except json.JSONDecodeError as e:
return f"Error decoding JSON: {e}"
def _clean_markdown(text):
"""
Removes markdown tags from a string if they exist.
Otherwise, returns the original string unchanged.
Args:
text: The input string.
Returns:
The string with markdown tags removed, or the original string
if no markdown tags were found.
"""
try:
# Check if the string contains markdown
if re.match(r"^```json\s*", text) and re.search(r"\s*```$", text):
# Remove leading ```json
text = re.sub(r"^```json\s*", "", text)
# Remove trailing ```
text = re.sub(r"\s*```$", "", text)
return text
except Exception as e:
# Log the error
st.error(f"Error cleaning markdown: {e}")
return None
def _merge_dicts(data1, data2):
"""
Recursively merges two data structures.
Handles merging of dictionaries and lists.
For dictionaries, if a key exists in both and both values are dictionaries
or lists, they are merged recursively. Otherwise, the value from data2 is used.
For lists, the lists are concatenated.
Args:
data1: The first data structure (dictionary or list).
data2: The second data structure (dictionary or list).
Returns:
The merged data structure.
Raises:
ValueError: If the data types are not supported for merging.
"""
if isinstance(data1, dict) and isinstance(data2, dict):
for key, value in data2.items():
if key in data1 and isinstance(data1[key], (dict, list)) and isinstance(value, type(data1[key])):
_merge_dicts(data1[key], value)
else:
data1[key] = value
return data1
elif isinstance(data1, list) and isinstance(data2, list):
return data1 + data2
else:
raise ValueError("Unsupported data types for merging")
def create_json(metadata, content):
"""
Creates a JSON string combining metadata and content.
Args:
metadata: A dictionary containing metadata information.
content: A dictionary containing the quiz content.
Returns:
A string representing the combined JSON data.
"""
# Create metadata with timestamp
metadata = {
"subject": metadata.get("subject", ""),
"topic": metadata.get("topic", ""),
"num_questions": metadata.get("num_questions", 0),
"exam_type": metadata.get("exam_type", ""),
"timestamp": datetime.datetime.now().isoformat()
}
# Combine metadata and content
combined_data = {"metadata": metadata, "content": content}
# Convert to JSON string
json_string = json.dumps(combined_data, indent=4)
return json_string
def create_pdf(data):
"""Creates a PDF file with text wrapping for quiz content."""
try:
# Load the JSON data
data = json.loads(data)
if 'metadata' not in data or 'content' not in data:
st.error("Error: Invalid data format. Missing 'metadata' or 'content' keys.")
return None
metadata = data['metadata']
content = data['content']
# Validate metadata
required_metadata_keys = ['subject', 'topic', 'exam_type', 'num_questions']
if not all(key in metadata for key in required_metadata_keys):
st.error("Error: Invalid metadata format. Missing required keys.")
return None
# Create a unique filename with timestamp
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
pdf_filename = f"quiz_output_{timestamp}.pdf"
# Get the temporary directory
temp_dir = tempfile.gettempdir()
pdf_path = os.path.join(temp_dir, pdf_filename)
c = canvas.Canvas(pdf_path, pagesize=A4)
c.setFont("Helvetica", 10)
exam_type = metadata['exam_type']
styles = getSampleStyleSheet()
style_normal = styles["Normal"]
y_position = 750
line_height = 15
frame_width = 500
first_page = True
for idx, q in enumerate(content):
if not isinstance(q, dict):
st.error(f"Error: Invalid question format at index {idx}. Skipping...")
continue
if first_page:
# Print metadata once
for key, label in [("subject", "Subject"), ("topic", "Topic"),
("exam_type", "Type"), ("num_questions", "Number of Questions")]:
c.drawString(50, y_position, f"{label}: {metadata[key]}")
y_position -= line_height
y_position -= line_height # Extra space before questions
first_page = False
# Print question number
question_text = f"{idx+1}. "
c.drawString(50, y_position, question_text)
x_position = 70 # Adjust starting position for question text
# --- Changes for better text flow ---
# Split the question into words
words = q.get('question', q.get('statement', '')).split()
current_line = ""
for word in words:
temp_line = current_line + " " + word
text_width = c.stringWidth(temp_line, "Helvetica", 10)
if text_width <= frame_width:
current_line = temp_line
else:
c.drawString(x_position, y_position, current_line)
y_position -= line_height
current_line = word
if y_position < 50:
c.showPage()
c.setFont("Helvetica", 10)
y_position = 750
# Print the last line of the question
c.drawString(x_position, y_position, current_line)
y_position -= line_height
# --- End of changes ---
if exam_type == "Multiple Choice":
# Validate question structure
required_question_keys = ['question', 'options', 'correct_answer']
if not all(key in q for key in required_question_keys):
st.error(f"Error: Invalid question format at index {idx}. Skipping...")
continue
# Print options
for option_idx, option in enumerate(q['options'], ord('a')):
c.drawString(70, y_position, f"{chr(option_idx)}) {option}")
y_position -= line_height
if y_position < 50:
c.showPage()
c.setFont("Helvetica", 10)
y_position = 750
# Print correct answer
c.drawString(70, y_position, f"Correct Answer: {q['correct_answer']}")
y_position -= line_height * 2
elif exam_type == "True or False":
# Validate question structure
required_question_keys = ['statement', 'options', 'correct_answer']
if not all(key in q for key in required_question_keys):
st.error(f"Error: Invalid question format at index {idx}. Skipping...")
continue
# Print options
for option in q['options']:
c.drawString(70, y_position, f"{option}")
y_position -= line_height
if y_position < 50:
c.showPage()
c.setFont("Helvetica", 10)
y_position = 750
# Print correct answer
c.drawString(70, y_position, f"Correct Answer: {q['correct_answer']}")
y_position -= line_height * 2
elif exam_type in ["Short Response", "Essay Type"]:
# Validate question structure
required_question_keys = ['question', 'correct_answer']
if not all(key in q for key in required_question_keys):
st.error(f"Error: Invalid question format at index {idx}. Skipping...")
continue
# Print correct answer
answer_text = f"Correct Answer: {q['correct_answer']}"
# --- Changes for better text flow ---
# Split the answer into words
words = answer_text.split()
current_line = ""
for word in words:
temp_line = current_line + " " + word
text_width = c.stringWidth(temp_line, "Helvetica", 10)
if text_width <= frame_width:
current_line = temp_line
else:
c.drawString(x_position, y_position, current_line)
y_position -= line_height
current_line = word
if y_position < 50:
c.showPage()
c.setFont("Helvetica", 10)
y_position = 750
# Print the last line of the answer
c.drawString(x_position, y_position, current_line)
y_position -= line_height * 2
# --- End of changes ---
if y_position < 50:
c.showPage()
c.setFont("Helvetica", 10)
y_position = 750
# Add the notice at the end
notice = "This exam was generated by the WVSU Exam Maker (c) 2025 West Visayas State University"
c.drawString(50, y_position, notice)
c.save()
return pdf_path
except Exception as e:
st.error(f"Error creating PDF: {e}")
return None
def generate_quiz_content(data):
"""
Separates the metadata and content from a JSON string containing exam data.
Creates a markdown formatted text that contains the exam metadata and
enumerates the questions, options and answers nicely formatted for readability.
Args:
data: A JSON string containing the exam data.
Returns:
A markdown formatted string.
"""
data = json.loads(data)
metadata = data["metadata"]
content = data["content"]
exam_type = metadata["exam_type"]
if exam_type == "Multiple Choice":
md_text = f"""# {metadata['subject']} - {metadata['topic']}
**Exam Type:** {metadata['exam_type']}
**Number of Questions:** {metadata['num_questions']}
**Timestamp:** {metadata['timestamp']}
---
"""
for i, q in enumerate(content):
md_text += f"""Question {i+1}:
{q['question']}
"""
for j, option in enumerate(q['options'], ord('a')):
md_text += f"""{chr(j)}. {option}
"""
md_text += f"""**Correct Answer:** {q['correct_answer']}
---
"""
md_text += """This exam was generated by the WVSU Exam Maker
(c) 2025 West Visayas State University
"""
elif exam_type == "True or False":
md_text = f"""# {metadata['subject']} - {metadata['topic']}
**Exam Type:** {metadata['exam_type']}
**Number of Questions:** {metadata['num_questions']}
**Timestamp:** {metadata['timestamp']}
---
"""
for i, q in enumerate(content):
md_text += f"""Statement {i+1}:
{q['statement']}
"""
for j, option in enumerate(q['options'], ord('a')):
md_text += f"""{option}
"""
md_text += f"""**Correct Answer:** {q['correct_answer']}
---
"""
md_text += """This exam was generated by the WVSU Exam Maker
(c) 2025 West Visayas State University"""
elif exam_type == "Short Response" or exam_type == "Essay Type":
md_text = f"""# {metadata['subject']} - {metadata['topic']}
**Exam Type:** {metadata['exam_type']}
**Number of Questions:** {metadata['num_questions']}
**Timestamp:** {metadata['timestamp']}
---
"""
for i, q in enumerate(content):
md_text += f"""Question {i+1}:
{q['question']}
"""
md_text += f"""**Correct Answer:** {q['correct_answer']}
---
"""
md_text += """This exam was generated by the WVSU Exam Maker
(c) 2025 West Visayas State University"""
return md_text
def generate_metadata(subject, topic, num_questions, exam_type):
"""Generates quiz metadata as a dictionary combining num_questions,
exam_type, and timestamp.
Args:
num_questions: The number of questions in the exam (int).
exam_type: The type of exam (str).
Returns:
A dictionary containing the quiz metadata.
"""
# Format the timestamp
timestamp = datetime.datetime.now()
formatted_timestamp = timestamp.strftime("%Y-%m-%d %H:%M:%S")
metadata = {
"subject": subject,
"topic": topic,
"num_questions": num_questions,
"exam_type": exam_type,
"timestamp": formatted_timestamp
}
return metadata
def generate_text(prompt):
"""Generates text based on the prompt."""
try:
# Send a text prompt to Gemini API
chat = st.session_state.chat
response = chat.send_message(
[
prompt
],
stream=enable_stream
)
return response.text
except Exception as e:
st.error(f"An error occurred while generating text: {e}")
return None
def show_text_prompt():
st.subheader("Text Prompt")
username = st.session_state["username"]
st.write(f"Welcome, {username}! This page allows you to generate questions based on user inputs.")
# Display username and logout button on every page
st.sidebar.write(f"Current user: {st.session_state['username']}")
# User inputs
# Course selection
course = st.selectbox("Select Course",
["Diploma in Teaching",
"Post Baccalaureate Diploma in Early Childhood Education",
"Master of Arts in Education - Language Teaching (English)",
"Master in Education major in Early Childhood Education"])
# Year level selection
year_level = st.selectbox("Select Year Level",
["1st Year",
"2nd Year",
"3rd Year",
"4th Year"])
# Subject selection
subject = st.text_input("Enter Subject",
"e.g.,The Teaching Profession, Facilitating Learner-Centered Teaching")
# Topic selection
topic = st.text_input("Enter Topic",
"e.g., Teacher as a professional, Introduction to Learner-Centered Teaching")
# Question type selection
question_type = st.selectbox("Select Question Type",
["Multiple Choice",
"True or False",
"Short Response",
"Essay Type"])
difficulty = st.selectbox("Select Difficulty",["easy","average","hard"])
#number of questions to generate
if question_type != "Essay Type":
num_questions = st.selectbox("Number of Questions to Generate",
[10, 20, 30, 40, 50])
else:
num_questions = st.selectbox("Number of Questions to Generate",
[1, 2, 3, 4, 5])
# Combine user inputs into a prompt
prompt = f"""Refer to the uploaded document. Generate a {question_type} question for a {year_level} {course} student
in {subject} on the topic of {topic} with a {difficulty} difficulty level.
The questions should require higher order thinking skills.
"""
if question_type == "Multiple Choice":
prompt += """Provide 4 choices. Provide the correct answer in the format 'Answer: A'.
Use the following JSON format for each question:
[{
"question": "Your question here?",
"options": ["Option A", "Option B", "Option C", "Option D"],
"correct_answer": "full text of the correct answer"
}, ... more questions]
Ensure that the response only contains the JSON array of questions and nothing else.
"""
elif question_type == "True or False":
prompt += """Indicate whether the statement is true or false. Keep the statement brief and concise.
Use the following JSON format for each question:
[{
"statement": "Your statement here",
"options": ["True", "False"],
"correct_answer": True"
}, ... more questions]
Ensure that the response only contains the JSON array of questions and nothing else.
"""
elif question_type == "Short Response":
prompt += """Create question that require a word or short phrase as answer. Use the following JSON format for each question:
[{
"question": "Your question here?",
"correct_answer": A word or phrase"
}, ... more questions]
Ensure that the response only contains the JSON array of questions and nothing else.
"""
elif question_type == "Essay Type":
prompt += """Create questions that require a short essay between 300 to 500 words.
Provide a detailed answer. Use the following JSON format for each question:
[{
"question": "Your question here?",
"correct_answer": The essay answer goes here."
}, ... more questions]
Ensure that the response only contains the JSON array of questions and nothing else.
"""
if not question_type == "Essay Type":
prompt += f"Generate 10 questions. Do not repeat questions you have already given in previous prompts. Exclude markdown tags in the response."
else:
prompt += f" Generate {num_questions} questions. Do not repeat questions you have already given in previous prompts. Exclude markdown tags in the response"
full_quiz = ""
# Send button
if st.button("Generate Questions"):
if question_type == "Essay Type":
#prompt once
with st.spinner('Generating questions...'):
full_quiz = _clean_markdown(generate_text(prompt))
else:
if num_questions == 10:
#prompt once
with st.spinner('Generating questions...'):
full_quiz = _clean_markdown(generate_text(prompt))
else:
#prompt multiple times
times = num_questions//10
for i in range(times):
with st.spinner('Generating questions...'):
response = generate_text(prompt)
if i==0:
full_quiz = _clean_markdown(response)
else:
full_quiz = merge_json_strings(full_quiz, response)
metadata = generate_metadata(subject, topic, num_questions, question_type)
try:
# Attempt to load the string as JSON to validate it
content = json.loads(full_quiz)
except json.JSONDecodeError:
st.error("Error: Invalid JSON string for quiz content.")
st.stop()
json_string = create_json(metadata, content)
quiz_markdown = generate_quiz_content(json_string)
st.markdown(quiz_markdown)
pdf_path = create_pdf(json_string)
if pdf_path:
"""Click the button to download the generated PDF."""
try:
with open(pdf_path, "rb") as f:
st.download_button("Download PDF", f, file_name=os.path.basename(pdf_path))
except Exception as e:
st.error(f"Error handling file download: {e}")
else:
st.error("Failed to generate the PDF. Please try again.")
#record the prompt for monitoring
save_user_prompt(username, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "Multimodal")
if st.session_state["authenticated"]:
show_text_prompt()
else:
if not st.session_state["is_starting"]:
st.write("You are not authenticated. Please log in to access this page.")
|