Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
112 |
-
|
|
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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='{"
|
204 |
lines=5
|
205 |
)
|
206 |
text = gr.Textbox(
|
207 |
label="Text to Extract From",
|
208 |
-
value="
|
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 |
-
|
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 |
-
|
243 |
-
|
244 |
-
if map_path:
|
245 |
-
with open(map_path, "r") as f:
|
246 |
-
map_html = f.read()
|
247 |
|
248 |
-
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|