Sayandip commited on
Commit
308a00d
·
verified ·
1 Parent(s): 1499f23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -65,6 +65,25 @@ def process_image(image_file):
65
  }
66
  }
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def export_conversation_to_pdf(conversation_history):
69
  pdf_path = "Conversation.pdf"
70
  doc = SimpleDocTemplate(pdf_path, pagesize=A4)
@@ -83,7 +102,7 @@ def export_conversation_to_pdf(conversation_history):
83
  # --------- Main App ---------
84
  def main():
85
  st.set_page_config(page_title="Gemini 2.5 Pro Chat Q&A", layout="wide")
86
- st.title("📄 Chat with Your Documents leveraging Internet Using Gemini 2.5 Pro (Team 18))")
87
 
88
  if "conversation" not in st.session_state:
89
  st.session_state.conversation = []
@@ -95,8 +114,12 @@ def main():
95
  st.session_state.chat_history = []
96
 
97
  uploaded_files = st.file_uploader(
98
- "Upload files (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png):",
99
- type=["txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex", "jpg", "jpeg", "png"],
 
 
 
 
100
  accept_multiple_files=True
101
  )
102
 
@@ -122,19 +145,22 @@ def main():
122
  elif filetype == "tex":
123
  all_text_content += extract_text_from_tex(uploaded_file) + "\n"
124
  elif filetype in ["jpg", "jpeg", "png"]:
125
- image_data = process_image(uploaded_file)
126
- st.session_state.image_data = image_data
127
  all_text_content += "Image uploaded and processed.\n"
128
  st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
 
 
 
 
129
  else:
130
  st.error(f"Unsupported file format: {uploaded_file.name}")
131
  except Exception as e:
132
- st.error(f"Failed to extract text from {uploaded_file.name}: {e}")
133
 
134
  st.session_state.documents_text = all_text_content
135
 
136
  if st.session_state.documents_text:
137
- st.markdown("### 💬 Conversation")
138
  for user_q, gemini_a in st.session_state.conversation:
139
  st.markdown(f"**You:** {user_q}")
140
  st.markdown(f"**Gemini:**\n\n{gemini_a}")
@@ -165,9 +191,11 @@ def main():
165
  if "image_data" in st.session_state:
166
  content_blocks.append(st.session_state.image_data)
167
 
 
 
 
168
  content_blocks.append({"text": f"Question: {user_input}"})
169
 
170
- # ✅ Gemini with Internet Search
171
  response = client.models.generate_content(
172
  model="gemini-2.5-pro-preview-06-05",
173
  contents=content_blocks,
@@ -175,7 +203,7 @@ def main():
175
  )
176
 
177
  st.session_state.conversation.append((user_input, response.text))
178
- st.success("💡 Answer:")
179
  st.write(response.text)
180
 
181
  st.session_state.chat_history.append({
@@ -187,7 +215,7 @@ def main():
187
  pdf_path = export_conversation_to_pdf(st.session_state.conversation)
188
  with open(pdf_path, "rb") as f:
189
  st.download_button(
190
- label="📥 Export Conversation as PDF",
191
  data=f,
192
  file_name="Conversation.pdf",
193
  mime="application/pdf"
 
65
  }
66
  }
67
 
68
+ def process_video(video_file):
69
+ video_bytes = video_file.read()
70
+ filetype = video_file.name.split(".")[-1].lower()
71
+ mime_map = {
72
+ "avi": "video/x-msvideo",
73
+ "mkv": "video/x-matroska",
74
+ "flv": "video/x-flv",
75
+ "wmv": "video/x-ms-wmv",
76
+ "mpeg": "video/mpeg"
77
+ }
78
+ mime_type = mime_map.get(filetype, video_file.type)
79
+ encoded_video = base64.b64encode(video_bytes).decode("utf-8")
80
+ return {
81
+ "inline_data": {
82
+ "mime_type": mime_type,
83
+ "data": encoded_video
84
+ }
85
+ }
86
+
87
  def export_conversation_to_pdf(conversation_history):
88
  pdf_path = "Conversation.pdf"
89
  doc = SimpleDocTemplate(pdf_path, pagesize=A4)
 
102
  # --------- Main App ---------
103
  def main():
104
  st.set_page_config(page_title="Gemini 2.5 Pro Chat Q&A", layout="wide")
105
+ st.title("\U0001F4C4 Chat with Your Documents leveraging Internet Using Gemini 2.5 Pro (Team 18)")
106
 
107
  if "conversation" not in st.session_state:
108
  st.session_state.conversation = []
 
114
  st.session_state.chat_history = []
115
 
116
  uploaded_files = st.file_uploader(
117
+ "Upload files (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png, video formats):",
118
+ type=[
119
+ "txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex",
120
+ "jpg", "jpeg", "png",
121
+ "mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"
122
+ ],
123
  accept_multiple_files=True
124
  )
125
 
 
145
  elif filetype == "tex":
146
  all_text_content += extract_text_from_tex(uploaded_file) + "\n"
147
  elif filetype in ["jpg", "jpeg", "png"]:
148
+ st.session_state.image_data = process_image(uploaded_file)
 
149
  all_text_content += "Image uploaded and processed.\n"
150
  st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
151
+ elif filetype in ["mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"]:
152
+ st.session_state.video_data = process_video(uploaded_file)
153
+ all_text_content += f"Video file '{uploaded_file.name}' uploaded and processed.\n"
154
+ st.video(uploaded_file)
155
  else:
156
  st.error(f"Unsupported file format: {uploaded_file.name}")
157
  except Exception as e:
158
+ st.error(f"Failed to extract/process {uploaded_file.name}: {e}")
159
 
160
  st.session_state.documents_text = all_text_content
161
 
162
  if st.session_state.documents_text:
163
+ st.markdown("### \U0001F4AC Conversation")
164
  for user_q, gemini_a in st.session_state.conversation:
165
  st.markdown(f"**You:** {user_q}")
166
  st.markdown(f"**Gemini:**\n\n{gemini_a}")
 
191
  if "image_data" in st.session_state:
192
  content_blocks.append(st.session_state.image_data)
193
 
194
+ if "video_data" in st.session_state:
195
+ content_blocks.append(st.session_state.video_data)
196
+
197
  content_blocks.append({"text": f"Question: {user_input}"})
198
 
 
199
  response = client.models.generate_content(
200
  model="gemini-2.5-pro-preview-06-05",
201
  contents=content_blocks,
 
203
  )
204
 
205
  st.session_state.conversation.append((user_input, response.text))
206
+ st.success("\U0001F4A1 Answer:")
207
  st.write(response.text)
208
 
209
  st.session_state.chat_history.append({
 
215
  pdf_path = export_conversation_to_pdf(st.session_state.conversation)
216
  with open(pdf_path, "rb") as f:
217
  st.download_button(
218
+ label="\U0001F4E5 Export Conversation as PDF",
219
  data=f,
220
  file_name="Conversation.pdf",
221
  mime="application/pdf"