Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -65,6 +65,33 @@ class SafeGeocoder:
|
|
65 |
self.cache[location] = None
|
66 |
return None
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
def extract_info(template, text):
|
69 |
try:
|
70 |
prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
|
@@ -83,10 +110,10 @@ def extract_info(template, text):
|
|
83 |
response_json = response.json()
|
84 |
if "error" in response_json and "loading" in response_json["error"]:
|
85 |
estimated_time = response_json.get("estimated_time", "unknown")
|
86 |
-
return f"⏳
|
87 |
|
88 |
if response.status_code != 200:
|
89 |
-
return f"❌ API
|
90 |
|
91 |
result = response.json()
|
92 |
|
@@ -104,11 +131,11 @@ def extract_info(template, text):
|
|
104 |
extracted = json.loads(json_text)
|
105 |
formatted = json.dumps(extracted, indent=2)
|
106 |
except json.JSONDecodeError:
|
107 |
-
return "❌ JSON
|
108 |
|
109 |
-
return "✅
|
110 |
except Exception as e:
|
111 |
-
return f"❌
|
112 |
|
113 |
def create_map(df, location_col):
|
114 |
m = folium.Map(
|
@@ -210,7 +237,7 @@ def process_excel(file, places_column):
|
|
210 |
print(f"Spalten in der Excel-Tabelle: {list(df.columns)}")
|
211 |
|
212 |
if places_column not in df.columns:
|
213 |
-
return None, f"
|
214 |
|
215 |
map_html, processed_count = create_map(df, places_column)
|
216 |
|
@@ -221,14 +248,14 @@ def process_excel(file, places_column):
|
|
221 |
total_locations = df[places_column].count()
|
222 |
success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
|
223 |
|
224 |
-
stats = f"
|
225 |
|
226 |
return map_html, stats, processed_path
|
227 |
except Exception as e:
|
228 |
import traceback
|
229 |
trace = traceback.format_exc()
|
230 |
print(f"Error processing file: {e}\n{trace}")
|
231 |
-
return None, f"
|
232 |
|
233 |
custom_css = """
|
234 |
<style>
|
@@ -258,6 +285,11 @@ h2 {
|
|
258 |
background-color: #ff7518 !important;
|
259 |
}
|
260 |
|
|
|
|
|
|
|
|
|
|
|
261 |
.info-box {
|
262 |
background-color: #e8f4fd;
|
263 |
border-left: 4px solid #2c6bb3;
|
@@ -294,6 +326,20 @@ h2 {
|
|
294 |
.gr-box {
|
295 |
margin-bottom: 0 !important;
|
296 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
</style>
|
298 |
"""
|
299 |
|
@@ -403,6 +449,24 @@ with gr.Blocks(css=custom_css, title="Daten Strukturieren und Analysieren") as d
|
|
403 |
</div>
|
404 |
""")
|
405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
406 |
with gr.Row():
|
407 |
with gr.Column():
|
408 |
template = gr.Textbox(
|
@@ -415,7 +479,7 @@ with gr.Blocks(css=custom_css, title="Daten Strukturieren und Analysieren") as d
|
|
415 |
value="Nene Erdbeben in Japan. London, 15. Jan. (Drahtber.) Reuter meldet aus Osaka: Die telephonische Verbindung zwischen Osaka und Tokio ist heute um 5.45 Uhr durch ein Erdbeben unterbrochen worden. Die Straßenbahnen in Tokio liegen still. Der Eisenbahnverkehr Tokio — Osaka ist unterbrochen. Die kaiserliche Familie ist in Sicherheit. In Suvamo, einer Borstadt Tokios, sind Brände ausgebrochen. Ein Eisenbahnzug stürzte in den Bajubawo, einem Fluß zwischen Gotemba und Tokio. Sechs Züge wurden umgeworfen. Nenqork, 15. Jan. (Drahtber.) Aus Tokio wird berichtet, daß in Uokohama bei dem Erdbeben sechs Personen getötet und 22 verletzt wurden. In Tokio wurden vier Personen getötet und 20 verletzt. In Nokohama wurden 800 Häuser zerstört.",
|
416 |
lines=8
|
417 |
)
|
418 |
-
extract_btn = gr.Button("Extrahieren Sie Informationen", variant="primary")
|
419 |
|
420 |
with gr.Column():
|
421 |
status = gr.Textbox(label="Status")
|
|
|
65 |
self.cache[location] = None
|
66 |
return None
|
67 |
|
68 |
+
# Function to just load the model
|
69 |
+
def load_model():
|
70 |
+
try:
|
71 |
+
# Send a minimal request just to trigger model loading
|
72 |
+
payload = {
|
73 |
+
"inputs": "<|input|>\n### Template:\n{\"test\": \"\"}\n### Text:\ntest\n\n<|output|>",
|
74 |
+
"parameters": {
|
75 |
+
"max_new_tokens": 10,
|
76 |
+
"do_sample": False
|
77 |
+
}
|
78 |
+
}
|
79 |
+
|
80 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
81 |
+
|
82 |
+
if response.status_code == 503:
|
83 |
+
response_json = response.json()
|
84 |
+
if "error" in response_json and "loading" in response_json["error"]:
|
85 |
+
estimated_time = response_json.get("estimated_time", "unknown")
|
86 |
+
return f"⏳ Modell lädt... (ca. {int(float(estimated_time)) if isinstance(estimated_time, (int, float, str)) else 'unbekannt'} Sekunden)"
|
87 |
+
|
88 |
+
if response.status_code != 200:
|
89 |
+
return f"❌ API Fehler: {response.status_code}"
|
90 |
+
|
91 |
+
return "✅ Modell erfolgreich geladen! Sie können jetzt mit der Extraktion beginnen."
|
92 |
+
except Exception as e:
|
93 |
+
return f"❌ Fehler: {str(e)}"
|
94 |
+
|
95 |
def extract_info(template, text):
|
96 |
try:
|
97 |
prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
|
|
|
110 |
response_json = response.json()
|
111 |
if "error" in response_json and "loading" in response_json["error"]:
|
112 |
estimated_time = response_json.get("estimated_time", "unknown")
|
113 |
+
return f"⏳ Modell lädt... (ca. {int(float(estimated_time)) if isinstance(estimated_time, (int, float, str)) else 'unbekannt'} Sekunden)", "Bitte versuchen Sie es in einigen Minuten erneut oder nutzen Sie den 'Modell laden' Button"
|
114 |
|
115 |
if response.status_code != 200:
|
116 |
+
return f"❌ API Fehler: {response.status_code}", response.text
|
117 |
|
118 |
result = response.json()
|
119 |
|
|
|
131 |
extracted = json.loads(json_text)
|
132 |
formatted = json.dumps(extracted, indent=2)
|
133 |
except json.JSONDecodeError:
|
134 |
+
return "❌ JSON Parsing Fehler", json_text
|
135 |
|
136 |
+
return "✅ Erfolgreich extrahiert", formatted
|
137 |
except Exception as e:
|
138 |
+
return f"❌ Fehler: {str(e)}", "{}"
|
139 |
|
140 |
def create_map(df, location_col):
|
141 |
m = folium.Map(
|
|
|
237 |
print(f"Spalten in der Excel-Tabelle: {list(df.columns)}")
|
238 |
|
239 |
if places_column not in df.columns:
|
240 |
+
return None, f"Spalte '{places_column}' wurde in der Excel-Datei nicht gefunden. Verfügbare Spalten: {', '.join(df.columns)}", None
|
241 |
|
242 |
map_html, processed_count = create_map(df, places_column)
|
243 |
|
|
|
248 |
total_locations = df[places_column].count()
|
249 |
success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
|
250 |
|
251 |
+
stats = f"Gefunden: {processed_count} von {total_locations} Orten ({success_rate:.1f}%)"
|
252 |
|
253 |
return map_html, stats, processed_path
|
254 |
except Exception as e:
|
255 |
import traceback
|
256 |
trace = traceback.format_exc()
|
257 |
print(f"Error processing file: {e}\n{trace}")
|
258 |
+
return None, f"Fehler bei der Verarbeitung der Datei: {str(e)}", None
|
259 |
|
260 |
custom_css = """
|
261 |
<style>
|
|
|
285 |
background-color: #ff7518 !important;
|
286 |
}
|
287 |
|
288 |
+
.gradio-button.secondary {
|
289 |
+
background-color: #5a87ca !important;
|
290 |
+
color: white !important;
|
291 |
+
}
|
292 |
+
|
293 |
.info-box {
|
294 |
background-color: #e8f4fd;
|
295 |
border-left: 4px solid #2c6bb3;
|
|
|
326 |
.gr-box {
|
327 |
margin-bottom: 0 !important;
|
328 |
}
|
329 |
+
|
330 |
+
/* Model status styling */
|
331 |
+
.model-status {
|
332 |
+
padding: 10px;
|
333 |
+
border-radius: 4px;
|
334 |
+
margin-bottom: 15px;
|
335 |
+
background-color: #f8f9fa;
|
336 |
+
font-size: 14px;
|
337 |
+
}
|
338 |
+
|
339 |
+
.separator {
|
340 |
+
margin: 20px 0;
|
341 |
+
border-top: 1px solid #eaeaea;
|
342 |
+
}
|
343 |
</style>
|
344 |
"""
|
345 |
|
|
|
449 |
</div>
|
450 |
""")
|
451 |
|
452 |
+
# Add model loading button and status at the top
|
453 |
+
with gr.Row():
|
454 |
+
load_btn = gr.Button("Modell laden (1. Schritt)", variant="secondary")
|
455 |
+
model_status = gr.Textbox(
|
456 |
+
label="Modell Status",
|
457 |
+
value="Modell nicht geladen. Bitte zuerst das Modell laden, um Verzögerungen zu vermeiden.",
|
458 |
+
elem_classes="model-status"
|
459 |
+
)
|
460 |
+
|
461 |
+
# Connect loading button
|
462 |
+
load_btn.click(
|
463 |
+
fn=load_model,
|
464 |
+
inputs=[],
|
465 |
+
outputs=[model_status]
|
466 |
+
)
|
467 |
+
|
468 |
+
gr.HTML("""<div class="separator"></div>""")
|
469 |
+
|
470 |
with gr.Row():
|
471 |
with gr.Column():
|
472 |
template = gr.Textbox(
|
|
|
479 |
value="Nene Erdbeben in Japan. London, 15. Jan. (Drahtber.) Reuter meldet aus Osaka: Die telephonische Verbindung zwischen Osaka und Tokio ist heute um 5.45 Uhr durch ein Erdbeben unterbrochen worden. Die Straßenbahnen in Tokio liegen still. Der Eisenbahnverkehr Tokio — Osaka ist unterbrochen. Die kaiserliche Familie ist in Sicherheit. In Suvamo, einer Borstadt Tokios, sind Brände ausgebrochen. Ein Eisenbahnzug stürzte in den Bajubawo, einem Fluß zwischen Gotemba und Tokio. Sechs Züge wurden umgeworfen. Nenqork, 15. Jan. (Drahtber.) Aus Tokio wird berichtet, daß in Uokohama bei dem Erdbeben sechs Personen getötet und 22 verletzt wurden. In Tokio wurden vier Personen getötet und 20 verletzt. In Nokohama wurden 800 Häuser zerstört.",
|
480 |
lines=8
|
481 |
)
|
482 |
+
extract_btn = gr.Button("Extrahieren Sie Informationen (2. Schritt)", variant="primary")
|
483 |
|
484 |
with gr.Column():
|
485 |
status = gr.Textbox(label="Status")
|