Spaces:
GIZ
/
Running on CPU Upgrade

leavoigt commited on
Commit
1b3f70d
·
1 Parent(s): fc37784
Files changed (1) hide show
  1. app.py +75 -27
app.py CHANGED
@@ -111,9 +111,9 @@ def handle_geojson_upload(file):
111
  def retrieve_paragraphs(query):
112
  """Connect to retriever and retrieve paragraphs"""
113
 
114
- try:
115
  # Call the API with the uploaded file
116
- client = Client("https://giz-eudr-retriever.hf.space/")
117
  result = client.predict(
118
  query=query,
119
  reports_filter="",
@@ -122,7 +122,7 @@ def retrieve_paragraphs(query):
122
  year_filter="",
123
  api_name="/retrieve"
124
  )
125
-
126
  return (
127
  results,
128
  gr.update(visible=True), # Keep status visible
@@ -152,11 +152,65 @@ def finish_chat():
152
  """Finish chat and reset input"""
153
  return gr.update(interactive=True, value="")
154
 
155
- async def chat_response(query, history, method, country, uploaded_file):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  """Generate chat response based on method and inputs"""
158
 
159
- # Validate inputs based on method
160
  if method == "Upload GeoJSON":
161
  if uploaded_file is None:
162
  warning_message = "⚠️ **No GeoJSON file uploaded.** Please upload a GeoJSON file first."
@@ -170,39 +224,27 @@ async def chat_response(query, history, method, country, uploaded_file):
170
  yield history, ""
171
  return
172
 
173
- # Get the formatted statistics if a file was just uploaded
174
  if method == "Upload GeoJSON" and uploaded_file:
175
  try:
176
- stats_result = handle_geojson_upload(uploaded_file)
177
- formatted_stats = stats_result[0] # Get the formatted statistics
178
  response = formatted_stats
179
  except Exception as e:
180
  response = f"Error processing file: {str(e)}"
181
 
182
- # Talk to report
183
  else:
184
  try:
185
- response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed against our {country} database."
186
-
187
- # Retrieve info
188
  retrieved_info = retrieve_paragraphs(query)[0]
189
  response = retrieved_info
190
-
191
  except Exception as e:
192
  response = f"Error retrieving information: {str(e)}"
193
 
194
-
195
- # else:
196
- # # Default response for other queries
197
- # if method == "Upload GeoJSON":
198
- # response = f"Based on your uploaded GeoJSON file, I can help you analyze the deforestation patterns and EUDR compliance aspects in your area of interest. Your question: '{query}' is being processed against the geographic data you provided."
199
- # else:
200
- # response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed against our {country} database."
201
-
202
- # Simulate streaming response
203
  words = response.split()
204
- for word in words:
205
- history[-1] = (query, " ".join(words[:words.index(word)+1]))
206
  yield history, "**Sources:** Sample source documents would appear here..."
207
  await asyncio.sleep(0.05)
208
 
@@ -475,10 +517,16 @@ with gr.Blocks(title="EUDR Q&A", theme=theme, css=custom_css) as demo:
475
  )
476
 
477
  # File upload - automatically process when file is uploaded
 
 
 
 
 
 
478
  uploaded_file.change(
479
- fn=handle_geojson_upload,
480
- inputs=[uploaded_file],
481
- outputs=[upload_status, upload_status, results_table]
482
  )
483
 
484
  # Chat functionality
 
111
  def retrieve_paragraphs(query):
112
  """Connect to retriever and retrieve paragraphs"""
113
 
114
+ try:
115
  # Call the API with the uploaded file
116
+ client = Client("https://giz-chatfed-retriever.hf.space/")
117
  result = client.predict(
118
  query=query,
119
  reports_filter="",
 
122
  year_filter="",
123
  api_name="/retrieve"
124
  )
125
+ print(client)
126
  return (
127
  results,
128
  gr.update(visible=True), # Keep status visible
 
152
  """Finish chat and reset input"""
153
  return gr.update(interactive=True, value="")
154
 
155
+ # async def chat_response(query, history, method, country, uploaded_file):
156
+
157
+ # """Generate chat response based on method and inputs"""
158
+
159
+ # # Validate inputs based on method
160
+ # if method == "Upload GeoJSON":
161
+ # if uploaded_file is None:
162
+ # warning_message = "⚠️ **No GeoJSON file uploaded.** Please upload a GeoJSON file first."
163
+ # history[-1] = (query, warning_message)
164
+ # yield history, ""
165
+ # return
166
+ # else: # "Talk to Reports"
167
+ # if not country:
168
+ # warning_message = "⚠️ **No country selected.** Please select a country to analyze reports."
169
+ # history[-1] = (query, warning_message)
170
+ # yield history, ""
171
+ # return
172
+
173
+ # # Get the formatted statistics if a file was just uploaded
174
+ # if method == "Upload GeoJSON" and uploaded_file:
175
+ # try:
176
+ # stats_result = handle_geojson_upload(uploaded_file)
177
+ # formatted_stats = stats_result[0] # Get the formatted statistics
178
+ # response = formatted_stats
179
+ # except Exception as e:
180
+ # response = f"Error processing file: {str(e)}"
181
+ # elif method == "Upload GeoJSON":
182
+ # response = f"Based on your uploaded GeoJSON file, I can help you analyze the deforestation patterns and EUDR compliance aspects in your area of interest. Your question: '{query}' is being processed against the geographic data you provided."
183
+
184
+ # # Talk to report
185
+ # else:
186
+ # try:
187
+ # # response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed against our {country} database."
188
+
189
+ # # Retrieve info
190
+ # print("retrieve info")
191
+ # retrieved_info = retrieve_paragraphs(query)[0]
192
+ # response = retrieved_info
193
+
194
+ # except Exception as e:
195
+ # response = f"Error retrieving information: {str(e)}"
196
+
197
 
198
+
199
+ # # else:
200
+ # # response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed against our {country} database."
201
+
202
+ # # Simulate streaming response
203
+ # words = response.split()
204
+ # for word in words:
205
+ # history[-1] = (query, " ".join(words[:words.index(word)+1]))
206
+ # yield history, "**Sources:** Sample source documents would appear here..."
207
+ # await asyncio.sleep(0.05)
208
+
209
+
210
+ async def chat_response(query, history, method, country, uploaded_file):
211
  """Generate chat response based on method and inputs"""
212
 
213
+ # Validate inputs
214
  if method == "Upload GeoJSON":
215
  if uploaded_file is None:
216
  warning_message = "⚠️ **No GeoJSON file uploaded.** Please upload a GeoJSON file first."
 
224
  yield history, ""
225
  return
226
 
227
+ # Handle GeoJSON upload put formatted stats into chat
228
  if method == "Upload GeoJSON" and uploaded_file:
229
  try:
230
+ formatted_stats = handle_geojson_upload(uploaded_file) # returns plain text
 
231
  response = formatted_stats
232
  except Exception as e:
233
  response = f"Error processing file: {str(e)}"
234
 
235
+ # Handle "Talk to Reports"
236
  else:
237
  try:
238
+ response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed..."
 
 
239
  retrieved_info = retrieve_paragraphs(query)[0]
240
  response = retrieved_info
 
241
  except Exception as e:
242
  response = f"Error retrieving information: {str(e)}"
243
 
244
+ # Stream response word by word into the chat
 
 
 
 
 
 
 
 
245
  words = response.split()
246
+ for i in range(len(words)):
247
+ history[-1] = (query, " ".join(words[:i+1]))
248
  yield history, "**Sources:** Sample source documents would appear here..."
249
  await asyncio.sleep(0.05)
250
 
 
517
  )
518
 
519
  # File upload - automatically process when file is uploaded
520
+ # uploaded_file.change(
521
+ # fn=handle_geojson_upload,
522
+ # inputs=[uploaded_file],
523
+ # outputs=[upload_status, upload_status, results_table]
524
+ # )
525
+
526
  uploaded_file.change(
527
+ lambda _: None,
528
+ inputs=[uploaded_file],
529
+ outputs=[]
530
  )
531
 
532
  # Chat functionality