Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,8 +62,14 @@ def process_uploaded_files(files):
|
|
| 62 |
if mime_type == "application/pdf":
|
| 63 |
try:
|
| 64 |
# Try text extraction first
|
| 65 |
-
pdf_text =
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
processed.append(f"PDF Content:\n{pdf_text}")
|
| 68 |
else:
|
| 69 |
# Fallback to image processing
|
|
@@ -79,10 +85,13 @@ def process_uploaded_files(files):
|
|
| 79 |
st.error(f"PDF Processing Error: {str(e)}")
|
| 80 |
|
| 81 |
elif mime_type.startswith("image/"):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
return processed
|
| 88 |
|
|
@@ -114,10 +123,15 @@ def generate_response(prompt: str, language: str, files: list) -> str:
|
|
| 114 |
|
| 115 |
return response.text if response.text else "No actionable information found in documents"
|
| 116 |
|
| 117 |
-
except genai.types.GenerateContentError as e:
|
| 118 |
-
return f"Analysis Error: {e.message}"
|
| 119 |
except Exception as e:
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# Session State Management
|
| 123 |
if "messages" not in st.session_state:
|
|
|
|
| 62 |
if mime_type == "application/pdf":
|
| 63 |
try:
|
| 64 |
# Try text extraction first
|
| 65 |
+
pdf_text = ""
|
| 66 |
+
pdf_reader = PyPDF2.PdfReader(BytesIO(file_bytes))
|
| 67 |
+
for page in pdf_reader.pages:
|
| 68 |
+
page_text = page.extract_text()
|
| 69 |
+
if page_text:
|
| 70 |
+
pdf_text += page_text + "\n"
|
| 71 |
+
|
| 72 |
+
if pdf_text.strip():
|
| 73 |
processed.append(f"PDF Content:\n{pdf_text}")
|
| 74 |
else:
|
| 75 |
# Fallback to image processing
|
|
|
|
| 85 |
st.error(f"PDF Processing Error: {str(e)}")
|
| 86 |
|
| 87 |
elif mime_type.startswith("image/"):
|
| 88 |
+
try:
|
| 89 |
+
processed.append(genai.upload_file(
|
| 90 |
+
mime_type=mime_type,
|
| 91 |
+
data=file_bytes
|
| 92 |
+
))
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(f"Image Processing Error: {str(e)}")
|
| 95 |
|
| 96 |
return processed
|
| 97 |
|
|
|
|
| 123 |
|
| 124 |
return response.text if response.text else "No actionable information found in documents"
|
| 125 |
|
|
|
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
+
error_msg = str(e)
|
| 128 |
+
if "SAFETY" in error_msg:
|
| 129 |
+
return "Response blocked for safety reasons"
|
| 130 |
+
elif "timeout" in error_msg.lower():
|
| 131 |
+
return "Analysis timed out. Please try a simpler query"
|
| 132 |
+
elif "image" in error_msg.lower():
|
| 133 |
+
return "Document format not supported. Try PDF or common image formats"
|
| 134 |
+
return f"Analysis Error: {error_msg}"
|
| 135 |
|
| 136 |
# Session State Management
|
| 137 |
if "messages" not in st.session_state:
|