Sayandip commited on
Commit
992fc2e
·
verified ·
1 Parent(s): 8c470ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -19,6 +19,7 @@ st.title("🤖 Q&A - Document & Image")
19
  if "chat_history" not in st.session_state:
20
  st.session_state.chat_history = []
21
 
 
22
  if st.button("🧹 Clear Chat History"):
23
  st.session_state.chat_history = []
24
 
@@ -31,7 +32,7 @@ if not GOOGLE_API_KEY:
31
  genai.configure(api_key=GOOGLE_API_KEY)
32
  model = genai.GenerativeModel(model_name="gemini-2.0-flash")
33
 
34
- # Extract text from supported document formats
35
  def load_and_extract_text(file_path):
36
  try:
37
  ext = file_path.split('.')[-1].lower()
@@ -85,7 +86,7 @@ def load_and_extract_text(file_path):
85
  except Exception as e:
86
  return f"Error processing file: {e}"
87
 
88
- # File uploader
89
  uploaded_files = st.file_uploader(
90
  "Upload one or more documents or images",
91
  type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"],
@@ -95,7 +96,7 @@ uploaded_files = st.file_uploader(
95
  # Question input
96
  question = st.text_input("💬 Enter your question:")
97
 
98
- # Process on submit
99
  if uploaded_files and question:
100
  combined_context = ""
101
  image_contents = []
@@ -132,7 +133,7 @@ if uploaded_files and question:
132
  try:
133
  content_blocks = []
134
 
135
- # Add prior Q&A history
136
  if st.session_state.chat_history:
137
  history_text = "\n\n".join(
138
  f"Q: {entry['question']}\nA: {entry['answer']}"
@@ -140,22 +141,17 @@ if uploaded_files and question:
140
  )
141
  content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
142
 
143
- # Add extracted file text
144
  if combined_context.strip():
145
  content_blocks.append({"text": f"Context:\n{combined_context}"})
146
 
147
- # Add any images
148
  content_blocks.extend(image_contents)
149
 
150
- # Add the current question
151
  content_blocks.append({"text": f"Question: {question}"})
152
 
153
- # ENABLE GOOGLE SEARCH HERE
154
- response = model.generate_content(
155
- contents=content_blocks,
156
- tools=[{"google_search": {}}]
157
- )
158
-
159
  st.success("💡 Answer:")
160
  st.write(response.text)
161
 
@@ -170,9 +166,9 @@ if uploaded_files and question:
170
  else:
171
  st.warning("No valid documents or images processed.")
172
 
173
- # Optional: Show full chat history
174
  if st.session_state.chat_history:
175
  st.markdown("### 🗂️ Chat History")
176
  for i, entry in enumerate(st.session_state.chat_history, 1):
177
  st.markdown(f"**Q{i}:** {entry['question']}")
178
- st.markdown(f"**A{i}:** {entry['answer']}")
 
19
  if "chat_history" not in st.session_state:
20
  st.session_state.chat_history = []
21
 
22
+ # Optional: Clear history button
23
  if st.button("🧹 Clear Chat History"):
24
  st.session_state.chat_history = []
25
 
 
32
  genai.configure(api_key=GOOGLE_API_KEY)
33
  model = genai.GenerativeModel(model_name="gemini-2.0-flash")
34
 
35
+ # Helper: Extract text from various documents
36
  def load_and_extract_text(file_path):
37
  try:
38
  ext = file_path.split('.')[-1].lower()
 
86
  except Exception as e:
87
  return f"Error processing file: {e}"
88
 
89
+ # Multi-file uploader
90
  uploaded_files = st.file_uploader(
91
  "Upload one or more documents or images",
92
  type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"],
 
96
  # Question input
97
  question = st.text_input("💬 Enter your question:")
98
 
99
+ # Processing
100
  if uploaded_files and question:
101
  combined_context = ""
102
  image_contents = []
 
133
  try:
134
  content_blocks = []
135
 
136
+ # Add prior Q&A as part of the context
137
  if st.session_state.chat_history:
138
  history_text = "\n\n".join(
139
  f"Q: {entry['question']}\nA: {entry['answer']}"
 
141
  )
142
  content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
143
 
144
+ # Add file text content
145
  if combined_context.strip():
146
  content_blocks.append({"text": f"Context:\n{combined_context}"})
147
 
148
+ # Add image blocks
149
  content_blocks.extend(image_contents)
150
 
151
+ # Add current question
152
  content_blocks.append({"text": f"Question: {question}"})
153
 
154
+ response = model.generate_content(contents=content_blocks)
 
 
 
 
 
155
  st.success("💡 Answer:")
156
  st.write(response.text)
157
 
 
166
  else:
167
  st.warning("No valid documents or images processed.")
168
 
169
+ # Optional: Show full conversation history
170
  if st.session_state.chat_history:
171
  st.markdown("### 🗂️ Chat History")
172
  for i, entry in enumerate(st.session_state.chat_history, 1):
173
  st.markdown(f"**Q{i}:** {entry['question']}")
174
+ st.markdown(f"**A{i}:** {entry['answer']}")