Sayandip commited on
Commit
fb7c648
·
verified ·
1 Parent(s): bd918bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -39
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
- # File uploader
82
- uploaded_file = st.file_uploader(
83
- "Upload a document or image",
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 uploaded_file and question:
92
- file_ext = uploaded_file.name.split('.')[-1].lower()
93
-
94
- if file_ext in ['jpg', 'jpeg', 'png']:
95
- # Image Q&A
96
- image_bytes = uploaded_file.read()
97
- encoded_image = base64.b64encode(image_bytes).decode("utf-8")
98
- mime_type = uploaded_file.type
99
-
100
- st.image(image_bytes, caption="Uploaded Image", use_container_width=True)
101
-
102
- with st.spinner("Processing image..."):
103
- try:
104
- content = [
105
- {"inline_data": {"mime_type": mime_type, "data": encoded_image}},
106
- {"text": question}
107
- ]
108
- response = model.generate_content(contents=content)
109
- st.success("💡 Answer:")
110
- st.write(response.text)
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
- with st.spinner("Processing document..."):
128
- try:
129
- prompt = f"Context: {extracted_text}\n\nQuestion: {question}\n\nAnswer:"
130
- response = model.generate_content(prompt)
131
- st.success("💡 Answer:")
132
- st.write(response.text)
133
- except Exception as e:
134
- st.error(f"Error generating answer: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")