Sayandip commited on
Commit
bc96f9a
·
verified ·
1 Parent(s): d04682c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -19,7 +19,6 @@ st.title("🤖 Q&A - Document & Image")
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
 
@@ -30,12 +29,9 @@ if not GOOGLE_API_KEY:
30
  st.stop()
31
 
32
  genai.configure(api_key=GOOGLE_API_KEY)
33
- model = genai.GenerativeModel(
34
- model_name="gemini-2.0-flash",
35
- tools=[{"google_search": {}}]
36
- )
37
 
38
- # Helper: Extract text from various documents
39
  def load_and_extract_text(file_path):
40
  try:
41
  ext = file_path.split('.')[-1].lower()
@@ -89,7 +85,7 @@ def load_and_extract_text(file_path):
89
  except Exception as e:
90
  return f"Error processing file: {e}"
91
 
92
- # Multi-file uploader
93
  uploaded_files = st.file_uploader(
94
  "Upload one or more documents or images",
95
  type=["docx", "xlsx", "pdf", "csv", "txt", "pptx", "tex", "html", "htm", "jpg", "jpeg", "png"],
@@ -99,7 +95,7 @@ uploaded_files = st.file_uploader(
99
  # Question input
100
  question = st.text_input("💬 Enter your question:")
101
 
102
- # Processing
103
  if uploaded_files and question:
104
  combined_context = ""
105
  image_contents = []
@@ -136,7 +132,7 @@ if uploaded_files and question:
136
  try:
137
  content_blocks = []
138
 
139
- # Add prior Q&A as part of the context
140
  if st.session_state.chat_history:
141
  history_text = "\n\n".join(
142
  f"Q: {entry['question']}\nA: {entry['answer']}"
@@ -144,17 +140,22 @@ if uploaded_files and question:
144
  )
145
  content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
146
 
147
- # Add file text content
148
  if combined_context.strip():
149
  content_blocks.append({"text": f"Context:\n{combined_context}"})
150
 
151
- # Add image blocks
152
  content_blocks.extend(image_contents)
153
 
154
- # Add current question
155
  content_blocks.append({"text": f"Question: {question}"})
156
 
157
- response = model.generate_content(contents=content_blocks)
 
 
 
 
 
158
  st.success("💡 Answer:")
159
  st.write(response.text)
160
 
@@ -169,9 +170,9 @@ if uploaded_files and question:
169
  else:
170
  st.warning("No valid documents or images processed.")
171
 
172
- # Optional: Show full conversation history
173
  if st.session_state.chat_history:
174
  st.markdown("### 🗂️ Chat History")
175
  for i, entry in enumerate(st.session_state.chat_history, 1):
176
  st.markdown(f"**Q{i}:** {entry['question']}")
177
- st.markdown(f"**A{i}:** {entry['answer']}")
 
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
 
 
29
  st.stop()
30
 
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
  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
  # 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
  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
  )
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
  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']}")