oberbics commited on
Commit
d99e4d3
·
verified ·
1 Parent(s): cfc4da2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -51
app.py CHANGED
@@ -9,7 +9,6 @@ from geopy.exc import GeocoderTimedOut, GeocoderServiceError
9
  import time
10
  import random
11
  from typing import List, Tuple, Optional
12
- import tempfile
13
  import io
14
 
15
  # NuExtract API configuration
@@ -108,39 +107,54 @@ def create_location_map(df: pd.DataFrame,
108
 
109
  # Processing Functions
110
  def process_excel(file, places_column):
111
- # Read the Excel file
112
- df = pd.read_excel(io.BytesIO(file))
 
113
 
114
- if places_column not in df.columns:
115
- return None, f"Column '{places_column}' not found in the Excel file. Available columns: {', '.join(df.columns)}"
116
-
117
- # Initialize the geocoding service
118
- geocoder = GeocodingService(user_agent="gradio_map_visualization_app")
119
-
120
- # Process locations and add coordinates
121
- df['coordinates'] = df[places_column].apply(geocoder.process_locations)
122
-
123
- # Create the map
124
- map_obj = create_location_map(df, coordinates_col='coordinates', places_col=places_column)
125
-
126
- # Save the map to a temporary HTML file
127
- temp_map_path = "temp_map.html"
128
- map_obj.save(temp_map_path)
129
-
130
- # Save the processed DataFrame to Excel
131
- processed_file_path = "processed_data.xlsx"
132
- df.to_excel(processed_file_path, index=False)
133
-
134
- # Statistics
135
- total_locations = len(df)
136
- successful_geocodes = df['coordinates'].apply(lambda x: len([c for c in x if c is not None])).sum()
137
- failed_geocodes = df['coordinates'].apply(lambda x: len([c for c in x if c is None])).sum()
138
-
139
- stats = f"Total locations: {total_locations}\n"
140
- stats += f"Successfully geocoded: {successful_geocodes}\n"
141
- stats += f"Failed to geocode: {failed_geocodes}"
142
-
143
- return temp_map_path, stats, processed_file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  # NuExtract Functions
146
  def extract_info(template, text):
@@ -159,6 +173,13 @@ def extract_info(template, text):
159
 
160
  response = requests.post(API_URL, headers=headers, json=payload)
161
 
 
 
 
 
 
 
 
162
  if response.status_code != 200:
163
  return f"❌ API Error: {response.status_code}", response.text
164
 
@@ -200,12 +221,12 @@ with gr.Blocks() as demo:
200
  with gr.Column():
201
  template = gr.Textbox(
202
  label="JSON Template",
203
- value='{"earthquake location": "", "dateline location": "", "source origin": ""}',
204
  lines=5
205
  )
206
  text = gr.Textbox(
207
  label="Text to Extract From",
208
- value="Tokio, 16. Jan. Neues Erdbeben in Japan. Nach einer Havasmeldung aus Tokio ist in der Gegend von Tokio und Jokohama ein neues Erdbeben festgestellt worden. Der Umfang des angerichteten Schadens läßt sich im Augenblick noch nicht übersehen. Die telegraphischen und telephonischen Verbindungen zwischen Tokio und Jokohama sind vollständig zerstört. Der Eisenbahnverkehr zwischen den beiden Städten ist vollkommen lahmgelegt. Nach einem späteren Telegramm beträgt die Zahl der Toten in Tokio 50. Die Zahl der Verwundeten steht noch nicht fest. Das Erdbeben dauerte sechs Minuten. In Jokohama gab es sechs Tote und 200 Verwundete. Dazu sind in der letztgenannten Stadt 600 Häuser eingestürzt. In einem Vorort von Tok'o ist ein Brand ausgebrochen. Ein Zug wurde in eine Schlucht geschleudert, sechs andere Züge sind entgleist.",
209
  lines=8
210
  )
211
  extract_btn = gr.Button("Extract Information", variant="primary")
@@ -232,34 +253,30 @@ with gr.Blocks() as demo:
232
  with gr.Column():
233
  map_output = gr.HTML(label="Map Visualization")
234
  stats_output = gr.Textbox(label="Statistics", lines=3)
235
- download_btn = gr.Button("Download Processed Data")
236
- processed_file = gr.File(label="Processed Data", visible=False)
237
 
238
  def process_and_map(file, column):
239
  if file is None:
240
  return None, "Please upload an Excel file", None
241
 
242
- map_path, stats, processed_path = process_excel(file, column)
243
-
244
- if map_path:
245
- with open(map_path, "r") as f:
246
- map_html = f.read()
247
 
248
- return map_html, stats, processed_path
249
- else:
250
- return None, stats, None
 
 
 
 
 
 
251
 
252
  process_btn.click(
253
  fn=process_and_map,
254
  inputs=[excel_file, places_column],
255
  outputs=[map_output, stats_output, processed_file]
256
  )
257
-
258
- download_btn.click(
259
- fn=lambda x: x,
260
- inputs=[processed_file],
261
- outputs=[processed_file]
262
- )
263
 
264
  if __name__ == "__main__":
265
  demo.launch()
 
9
  import time
10
  import random
11
  from typing import List, Tuple, Optional
 
12
  import io
13
 
14
  # NuExtract API configuration
 
107
 
108
  # Processing Functions
109
  def process_excel(file, places_column):
110
+ # Check if file is None
111
+ if file is None:
112
+ return None, "No file uploaded", None
113
 
114
+ try:
115
+ # Handle various file object types that Gradio might provide
116
+ if hasattr(file, 'name'):
117
+ # Gradio file object
118
+ df = pd.read_excel(file.name)
119
+ elif isinstance(file, bytes):
120
+ # Raw bytes
121
+ df = pd.read_excel(io.BytesIO(file))
122
+ else:
123
+ # Assume it's a filepath string
124
+ df = pd.read_excel(file)
125
+
126
+ if places_column not in df.columns:
127
+ return None, f"Column '{places_column}' not found in the Excel file. Available columns: {', '.join(df.columns)}", None
128
+
129
+ # Initialize the geocoding service
130
+ geocoder = GeocodingService(user_agent="gradio_map_visualization_app")
131
+
132
+ # Process locations and add coordinates
133
+ df['coordinates'] = df[places_column].apply(geocoder.process_locations)
134
+
135
+ # Create the map
136
+ map_obj = create_location_map(df, coordinates_col='coordinates', places_col=places_column)
137
+
138
+ # Save the map to a temporary HTML file
139
+ temp_map_path = "temp_map.html"
140
+ map_obj.save(temp_map_path)
141
+
142
+ # Save the processed DataFrame to Excel
143
+ processed_file_path = "processed_data.xlsx"
144
+ df.to_excel(processed_file_path, index=False)
145
+
146
+ # Statistics
147
+ total_locations = len(df)
148
+ successful_geocodes = df['coordinates'].apply(lambda x: len([c for c in x if c is not None])).sum()
149
+ failed_geocodes = df['coordinates'].apply(lambda x: len([c for c in x if c is None])).sum()
150
+
151
+ stats = f"Total locations: {total_locations}\n"
152
+ stats += f"Successfully geocoded: {successful_geocodes}\n"
153
+ stats += f"Failed to geocode: {failed_geocodes}"
154
+
155
+ return temp_map_path, stats, processed_file_path
156
+ except Exception as e:
157
+ return None, f"Error processing file: {str(e)}", None
158
 
159
  # NuExtract Functions
160
  def extract_info(template, text):
 
173
 
174
  response = requests.post(API_URL, headers=headers, json=payload)
175
 
176
+ # If the model is loading, inform the user
177
+ if response.status_code == 503:
178
+ response_json = response.json()
179
+ if "error" in response_json and "loading" in response_json["error"]:
180
+ estimated_time = response_json.get("estimated_time", "unknown")
181
+ return f"⏳ Model is loading (ETA: {int(float(estimated_time)) if isinstance(estimated_time, (int, float, str)) else 'unknown'} seconds)", "Please try again in a few minutes"
182
+
183
  if response.status_code != 200:
184
  return f"❌ API Error: {response.status_code}", response.text
185
 
 
221
  with gr.Column():
222
  template = gr.Textbox(
223
  label="JSON Template",
224
+ value='{"name": "", "email": ""}',
225
  lines=5
226
  )
227
  text = gr.Textbox(
228
  label="Text to Extract From",
229
+ value="Contact: John Smith (john@example.com)",
230
  lines=8
231
  )
232
  extract_btn = gr.Button("Extract Information", variant="primary")
 
253
  with gr.Column():
254
  map_output = gr.HTML(label="Map Visualization")
255
  stats_output = gr.Textbox(label="Statistics", lines=3)
256
+ processed_file = gr.File(label="Processed Data", visible=True, interactive=False)
 
257
 
258
  def process_and_map(file, column):
259
  if file is None:
260
  return None, "Please upload an Excel file", None
261
 
262
+ try:
263
+ map_path, stats, processed_path = process_excel(file, column)
 
 
 
264
 
265
+ if map_path and processed_path:
266
+ with open(map_path, "r") as f:
267
+ map_html = f.read()
268
+
269
+ return map_html, stats, processed_path
270
+ else:
271
+ return None, stats, None
272
+ except Exception as e:
273
+ return None, f"Error: {str(e)}", None
274
 
275
  process_btn.click(
276
  fn=process_and_map,
277
  inputs=[excel_file, places_column],
278
  outputs=[map_output, stats_output, processed_file]
279
  )
 
 
 
 
 
 
280
 
281
  if __name__ == "__main__":
282
  demo.launch()