louiecerv commited on
Commit
5402e2b
·
1 Parent(s): f320031

add the download PDF button

Browse files
pages/Multimodal.py CHANGED
@@ -4,6 +4,10 @@ from datetime import datetime
4
  from PIL import Image
5
  import google.generativeai as genai
6
  import os
 
 
 
 
7
 
8
  MODEL_ID = "gemini-2.0-flash-exp"
9
  api_key = os.getenv("GEMINI_API_KEY")
@@ -38,16 +42,50 @@ def save_user_prompt(username, prompt_time, prompt_type):
38
  conn.commit()
39
  conn.close()
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def show_multimodal():
42
  st.subheader("Multimodal")
43
  username = st.session_state["username"]
 
44
 
45
  # we dont use the system instruction for now
46
  #system_instruction = get_system_instruction(username)
47
 
48
- # Streamlit app
49
- st.title("Gemini Image Chat")
50
-
51
  # File uploader with allowed types
52
  uploaded_file = st.file_uploader("Choose an image or PDF...", type=["jpg", "jpeg", "png", "pdf"])
53
 
@@ -110,7 +148,7 @@ def show_multimodal():
110
  [1, 2, 3, 4, 5])
111
 
112
  # Combine user inputs into a prompt
113
- prompt = f"""Refer to the provided Document. Generate {question_type} questions for a {year_level} {course} student
114
  in {subject} on the topic of {topic} with a {difficulty} difficulty level.
115
  The questions should require higher order thinking skills.
116
  """
@@ -206,8 +244,18 @@ def show_multimodal():
206
 
207
  #record the prompt for monitoring
208
  save_user_prompt(username, datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "Multimodal")
 
 
 
 
 
 
 
209
 
210
- st.success("Exam was generated succesfully.")
 
 
 
211
 
212
  if st.session_state["authenticated"]:
213
  show_multimodal()
 
4
  from PIL import Image
5
  import google.generativeai as genai
6
  import os
7
+ from reportlab.pdfgen import canvas
8
+ from reportlab.lib.pagesizes import A4
9
+ from io import BytesIO
10
+ import textwrap
11
 
12
  MODEL_ID = "gemini-2.0-flash-exp"
13
  api_key = os.getenv("GEMINI_API_KEY")
 
42
  conn.commit()
43
  conn.close()
44
 
45
+ def create_pdf(data):
46
+ try:
47
+ pdf_file = BytesIO()
48
+ c = canvas.Canvas(pdf_file, pagesize=A4)
49
+
50
+ # Set font and font size
51
+ c.setFont("Helvetica", 12)
52
+
53
+ # Set margin
54
+ margin = 72 # 1 inch in points
55
+ x_position = margin
56
+ y_position = A4[1] - margin # Start from the top of the page
57
+
58
+ # Add title
59
+ c.drawString(x_position, y_position, "Exam Questions")
60
+ y_position -= 20
61
+
62
+ # Wrap markdown data
63
+ lines = textwrap.wrap(data, width=60) # Adjust width to fit the page
64
+ for line in lines:
65
+ if y_position < margin: # Check if we need to start a new page
66
+ c.showPage()
67
+ y_position = A4[1] - margin # Reset y-position to the top of the new page
68
+
69
+ c.drawString(x_position, y_position, line)
70
+ y_position -= 20 # Adjust line spacing
71
+
72
+ c.showPage()
73
+ c.save()
74
+ pdf_file.seek(0)
75
+ return pdf_file
76
+
77
+ except Exception as e:
78
+ st.error(f"An error occurred while creating PDF file: {e}")
79
+ return None
80
+
81
  def show_multimodal():
82
  st.subheader("Multimodal")
83
  username = st.session_state["username"]
84
+ st.write(f"Welcome, {username}! This page allows you to generate questions based on an image or PDF file.")
85
 
86
  # we dont use the system instruction for now
87
  #system_instruction = get_system_instruction(username)
88
 
 
 
 
89
  # File uploader with allowed types
90
  uploaded_file = st.file_uploader("Choose an image or PDF...", type=["jpg", "jpeg", "png", "pdf"])
91
 
 
148
  [1, 2, 3, 4, 5])
149
 
150
  # Combine user inputs into a prompt
151
+ prompt = f"""Refer to the provided document. Generate {question_type} questions for a {year_level} {course} student
152
  in {subject} on the topic of {topic} with a {difficulty} difficulty level.
153
  The questions should require higher order thinking skills.
154
  """
 
244
 
245
  #record the prompt for monitoring
246
  save_user_prompt(username, datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "Multimodal")
247
+
248
+ # Create PDF
249
+ pdf_file = create_pdf(full_response)
250
+
251
+ # Generate a unique filename based on the current time
252
+ current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
253
+ pdf_filename = f"MC Type Exam_{current_time}.pdf"
254
 
255
+ # Download button
256
+ if pdf_file:
257
+ st.download_button("Download PDF", pdf_file, pdf_filename, "application/pdf")
258
+ st.success("Exam was generated succesfully.")
259
 
260
  if st.session_state["authenticated"]:
261
  show_multimodal()
pages/__pycache__/Multimodal.cpython-313.pyc CHANGED
Binary files a/pages/__pycache__/Multimodal.cpython-313.pyc and b/pages/__pycache__/Multimodal.cpython-313.pyc differ
 
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  streamlit
2
  passlib
3
  bcrypt==3.2.0
4
- google-generativeai
 
 
1
  streamlit
2
  passlib
3
  bcrypt==3.2.0
4
+ google-generativeai
5
+ reportlab
users.db CHANGED
Binary files a/users.db and b/users.db differ