Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,42 +78,38 @@ def load_and_extract_text(file_path):
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error processing file: {e}"
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
"Upload
|
| 84 |
-
type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"]
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
# Question input
|
| 88 |
question = st.text_input("💬 Enter your question:")
|
| 89 |
|
| 90 |
# Processing
|
| 91 |
-
if
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
except Exception as e:
|
| 113 |
-
st.error(f"Error processing image: {e}")
|
| 114 |
-
|
| 115 |
-
else:
|
| 116 |
-
# Document Q&A
|
| 117 |
temp_file_path = "temp." + file_ext
|
| 118 |
with open(temp_file_path, "wb") as temp_file:
|
| 119 |
temp_file.write(uploaded_file.read())
|
|
@@ -122,13 +118,31 @@ if uploaded_file and question:
|
|
| 122 |
os.remove(temp_file_path)
|
| 123 |
|
| 124 |
if "Error" in extracted_text or "Unsupported" in extracted_text:
|
| 125 |
-
st.error(extracted_text)
|
| 126 |
else:
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error processing file: {e}"
|
| 80 |
|
| 81 |
+
# Multi-file uploader
|
| 82 |
+
uploaded_files = st.file_uploader(
|
| 83 |
+
"Upload one or more documents or images",
|
| 84 |
+
type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"],
|
| 85 |
+
accept_multiple_files=True
|
| 86 |
)
|
| 87 |
|
| 88 |
# Question input
|
| 89 |
question = st.text_input("💬 Enter your question:")
|
| 90 |
|
| 91 |
# Processing
|
| 92 |
+
if uploaded_files and question:
|
| 93 |
+
combined_context = ""
|
| 94 |
+
image_contents = []
|
| 95 |
+
|
| 96 |
+
for uploaded_file in uploaded_files:
|
| 97 |
+
file_ext = uploaded_file.name.split('.')[-1].lower()
|
| 98 |
+
|
| 99 |
+
if file_ext in ['jpg', 'jpeg', 'png']:
|
| 100 |
+
# Store image data for later processing
|
| 101 |
+
image_bytes = uploaded_file.read()
|
| 102 |
+
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 103 |
+
image_contents.append({
|
| 104 |
+
"inline_data": {
|
| 105 |
+
"mime_type": uploaded_file.type,
|
| 106 |
+
"data": encoded_image
|
| 107 |
+
}
|
| 108 |
+
})
|
| 109 |
+
st.image(image_bytes, caption=uploaded_file.name, use_container_width=True)
|
| 110 |
+
continue
|
| 111 |
+
|
| 112 |
+
# Process document files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
temp_file_path = "temp." + file_ext
|
| 114 |
with open(temp_file_path, "wb") as temp_file:
|
| 115 |
temp_file.write(uploaded_file.read())
|
|
|
|
| 118 |
os.remove(temp_file_path)
|
| 119 |
|
| 120 |
if "Error" in extracted_text or "Unsupported" in extracted_text:
|
| 121 |
+
st.error(f"{uploaded_file.name}: {extracted_text}")
|
| 122 |
else:
|
| 123 |
+
combined_context += f"\n\n---\nDocument: {uploaded_file.name}\n\n{extracted_text}"
|
| 124 |
+
|
| 125 |
+
# Process if there's valid content
|
| 126 |
+
if combined_context.strip() or image_contents:
|
| 127 |
+
with st.spinner("Generating answer..."):
|
| 128 |
+
try:
|
| 129 |
+
content_blocks = []
|
| 130 |
+
|
| 131 |
+
# Add text context
|
| 132 |
+
if combined_context.strip():
|
| 133 |
+
content_blocks.append({"text": f"Context:\n{combined_context}"})
|
| 134 |
+
|
| 135 |
+
# Add image blocks
|
| 136 |
+
content_blocks.extend(image_contents)
|
| 137 |
+
|
| 138 |
+
# Add user question
|
| 139 |
+
content_blocks.append({"text": f"Question: {question}"})
|
| 140 |
+
|
| 141 |
+
response = model.generate_content(contents=content_blocks)
|
| 142 |
+
st.success("💡 Answer:")
|
| 143 |
+
st.write(response.text)
|
| 144 |
+
|
| 145 |
+
except Exception as e:
|
| 146 |
+
st.error(f"Error generating answer: {e}")
|
| 147 |
+
else:
|
| 148 |
+
st.warning("No valid documents or images processed.")
|