Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -184,76 +184,122 @@ def main():
|
|
| 184 |
if "chat_history" not in st.session_state:
|
| 185 |
st.session_state.chat_history = []
|
| 186 |
|
| 187 |
-
|
| 188 |
-
"
|
| 189 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
capture_photo()
|
| 194 |
-
|
| 195 |
-
elif action == "Capture Video":
|
| 196 |
capture_video()
|
| 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 |
-
if st.session_state.documents_text.strip():
|
| 233 |
-
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 234 |
-
|
| 235 |
-
if "image_data" in st.session_state:
|
| 236 |
-
content_blocks.append(st.session_state.image_data)
|
| 237 |
-
|
| 238 |
-
if "video_data" in st.session_state:
|
| 239 |
-
content_blocks.append(st.session_state.video_data)
|
| 240 |
-
|
| 241 |
-
content_blocks.append({"text": f"Question: {user_input}"})
|
| 242 |
-
|
| 243 |
-
response = client.models.generate_content(
|
| 244 |
-
model="gemini-2.0-flash",
|
| 245 |
-
contents=content_blocks,
|
| 246 |
-
config={"tools": [{"google_search": {}}]}
|
| 247 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
|
| 258 |
if st.session_state.conversation:
|
| 259 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|
|
|
|
| 184 |
if "chat_history" not in st.session_state:
|
| 185 |
st.session_state.chat_history = []
|
| 186 |
|
| 187 |
+
uploaded_files = st.file_uploader(
|
| 188 |
+
"Upload files (.txt, .docx, .csv, .xlsx, .pptx, .pdf, .html, .htm, .tex, .jpg, .jpeg, .png, video formats):",
|
| 189 |
+
type=[
|
| 190 |
+
"txt", "docx", "csv", "xlsx", "pptx", "pdf", "html", "htm", "tex",
|
| 191 |
+
"jpg", "jpeg", "png",
|
| 192 |
+
"mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"
|
| 193 |
+
],
|
| 194 |
+
accept_multiple_files=True
|
| 195 |
)
|
| 196 |
|
| 197 |
+
if uploaded_files:
|
| 198 |
+
all_text_content = ""
|
| 199 |
+
for uploaded_file in uploaded_files:
|
| 200 |
+
filetype = uploaded_file.name.split(".")[-1].lower()
|
| 201 |
+
try:
|
| 202 |
+
if filetype == "txt":
|
| 203 |
+
all_text_content += uploaded_file.read().decode("utf-8") + "\n"
|
| 204 |
+
elif filetype == "docx":
|
| 205 |
+
all_text_content += extract_text_from_docx(uploaded_file) + "\n"
|
| 206 |
+
elif filetype == "csv":
|
| 207 |
+
all_text_content += extract_text_from_csv(uploaded_file) + "\n"
|
| 208 |
+
elif filetype == "xlsx":
|
| 209 |
+
all_text_content += extract_text_from_xlsx(uploaded_file) + "\n"
|
| 210 |
+
elif filetype == "pptx":
|
| 211 |
+
all_text_content += extract_text_from_pptx(uploaded_file) + "\n"
|
| 212 |
+
elif filetype == "pdf":
|
| 213 |
+
all_text_content += extract_text_from_pdf(uploaded_file) + "\n"
|
| 214 |
+
elif filetype in ["html", "htm"]:
|
| 215 |
+
all_text_content += extract_text_from_html(uploaded_file) + "\n"
|
| 216 |
+
elif filetype == "tex":
|
| 217 |
+
all_text_content += extract_text_from_tex(uploaded_file) + "\n"
|
| 218 |
+
elif filetype in ["jpg", "jpeg", "png"]:
|
| 219 |
+
st.session_state.image_data = process_image(uploaded_file)
|
| 220 |
+
all_text_content += "Image uploaded and processed.\n"
|
| 221 |
+
st.image(uploaded_file, caption=uploaded_file.name, use_container_width=True)
|
| 222 |
+
elif filetype in ["mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "mpeg"]:
|
| 223 |
+
st.session_state.video_data = process_video(uploaded_file)
|
| 224 |
+
all_text_content += f"Video file '{uploaded_file.name}' uploaded and processed.\n"
|
| 225 |
+
st.video(uploaded_file)
|
| 226 |
+
else:
|
| 227 |
+
st.error(f"Unsupported file format: {uploaded_file.name}")
|
| 228 |
+
except Exception as e:
|
| 229 |
+
st.error(f"Failed to extract/process {uploaded_file.name}: {e}")
|
| 230 |
+
|
| 231 |
+
st.session_state.documents_text = all_text_content
|
| 232 |
+
|
| 233 |
+
# Media Capture: Buttons to Capture Photo or Video
|
| 234 |
+
st.sidebar.header("Capture Media")
|
| 235 |
+
capture_option = st.sidebar.radio("Select Media Capture Option", ["None", "Capture Photo", "Capture Video"])
|
| 236 |
+
|
| 237 |
+
if capture_option == "Capture Photo":
|
| 238 |
capture_photo()
|
| 239 |
+
elif capture_option == "Capture Video":
|
|
|
|
| 240 |
capture_video()
|
| 241 |
|
| 242 |
+
# Display conversation and chat
|
| 243 |
+
if st.session_state.documents_text:
|
| 244 |
+
st.markdown("### \U0001F4AC Conversation")
|
| 245 |
+
for user_q, gemini_a in st.session_state.conversation:
|
| 246 |
+
st.markdown(f"**You:** {user_q}")
|
| 247 |
+
st.markdown(f"**Gemini:**\n\n{gemini_a}")
|
| 248 |
+
|
| 249 |
+
if st.session_state.chat_active:
|
| 250 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
| 251 |
+
user_input = st.text_input("Ask a question about the documents (type 'exit' to stop):")
|
| 252 |
+
submit = st.form_submit_button("Send")
|
| 253 |
+
|
| 254 |
+
if submit and user_input:
|
| 255 |
+
if user_input.strip().lower() == "exit":
|
| 256 |
+
st.session_state.chat_active = False
|
| 257 |
+
st.success("Chat ended. Reload to start again.")
|
| 258 |
+
else:
|
| 259 |
+
with st.spinner("Let me think..."):
|
| 260 |
+
content_blocks = []
|
| 261 |
+
|
| 262 |
+
content_blocks.append({
|
| 263 |
+
"text": (
|
| 264 |
+
"Your role is to analyze documents, images, and videos to help identify and classify driver states, "
|
| 265 |
+
"including: drowsy, alert, yawning, microsleep, eyes/lips open or closed, and other distractions. "
|
| 266 |
+
"The user may ask for timestamps of events in video, technical explanations from code/docs, "
|
| 267 |
+
"or structured summaries. Prioritize your answers for this use case and act as a helpful assistant "
|
| 268 |
+
"within this domain."
|
| 269 |
+
)
|
| 270 |
+
})
|
| 271 |
+
|
| 272 |
+
if st.session_state.conversation:
|
| 273 |
+
history_text = "\n\n".join(
|
| 274 |
+
f"Q: {entry[0]}\nA: {entry[1]}" for entry in st.session_state.conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
)
|
| 276 |
+
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 277 |
+
|
| 278 |
+
if st.session_state.documents_text.strip():
|
| 279 |
+
content_blocks.append({"text": f"Context:\n{st.session_state.documents_text}"})
|
| 280 |
+
|
| 281 |
+
if "image_data" in st.session_state:
|
| 282 |
+
content_blocks.append(st.session_state.image_data)
|
| 283 |
+
|
| 284 |
+
if "video_data" in st.session_state:
|
| 285 |
+
content_blocks.append(st.session_state.video_data)
|
| 286 |
+
|
| 287 |
+
content_blocks.append({"text": f"Question: {user_input}"})
|
| 288 |
+
|
| 289 |
+
response = client.models.generate_content(
|
| 290 |
+
model="gemini-2.0-flash",
|
| 291 |
+
contents=content_blocks,
|
| 292 |
+
config={"tools": [{"google_search": {}}]}
|
| 293 |
+
)
|
| 294 |
|
| 295 |
+
st.session_state.conversation.append((user_input, response.text))
|
| 296 |
+
st.success("\U0001F4A1 Answer:")
|
| 297 |
+
st.write(response.text)
|
| 298 |
|
| 299 |
+
st.session_state.chat_history.append({
|
| 300 |
+
"question": user_input,
|
| 301 |
+
"answer": response.text
|
| 302 |
+
})
|
| 303 |
|
| 304 |
if st.session_state.conversation:
|
| 305 |
pdf_path = export_conversation_to_pdf(st.session_state.conversation)
|