oberbics commited on
Commit
a90358f
·
verified ·
1 Parent(s): 277f8c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -63
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import json
3
- import requests
4
  import os
5
  import pandas as pd
6
  import folium
@@ -15,6 +14,8 @@ import tempfile
15
  import warnings
16
  import string
17
  import spaces
 
 
18
 
19
  warnings.filterwarnings("ignore")
20
 
@@ -26,9 +27,16 @@ MAP_TILES = {
26
  }
27
  }
28
 
29
- # NuExtract API configuration
30
- API_URL = "https://api-inference.huggingface.co/models/numind/NuExtract-1.5-tiny"
31
- headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
 
 
 
 
 
 
 
32
 
33
  class SafeGeocoder:
34
  def __init__(self):
@@ -42,7 +50,7 @@ class SafeGeocoder:
42
  elapsed = current_time - self.last_request
43
  if elapsed < 1.0:
44
  time.sleep(1.0 - elapsed)
45
- self.last_request = time.time()
46
 
47
  def get_coords(self, location: str):
48
  if not location or pd.isna(location):
@@ -67,82 +75,59 @@ class SafeGeocoder:
67
  self.cache[location] = None
68
  return None
69
 
70
- # Function to just load the model
71
-
72
-
73
  def load_model():
 
74
  try:
75
  # Generate a random location and text each time
76
  random_city = random.choice(["Berlin", "Paris", "London", "Tokyo", "Rome", "Madrid"])
77
  random_suffix = ''.join(random.choices(string.ascii_lowercase, k=5))
78
  test_text = f"Test in {random_city}_{random_suffix}."
79
-
80
  test_template = '{"test_location": ""}'
81
 
82
- prompt = f"<|input|>\n### Template:\n{test_template}\n### Text:\n{test_text}\n\n<|output|>"
83
-
84
- # Send request with randomized input
85
- payload = {
86
- "inputs": prompt,
87
- "parameters": {
88
- "max_new_tokens": 50,
89
- "do_sample": False
90
- }
91
- }
92
-
93
- response = requests.post(API_URL, headers=headers, json=payload)
94
-
95
- if response.status_code == 503:
96
- response_json = response.json()
97
- if "error" in response_json and "loading" in response_json["error"]:
98
- estimated_time = response_json.get("estimated_time", "unknown")
99
- return f"⏳ Modell lädt... (ca. {int(float(estimated_time)) if isinstance(estimated_time, (int, float, str)) else 'unbekannt'} Sekunden)"
100
 
101
- if response.status_code == 200:
102
- result = response.json()
103
-
104
- if isinstance(result, list) and len(result) > 0:
105
- result_text = result[0].get("generated_text", "")
106
-
107
- # Check if response contains the random city we included
108
- if "<|output|>" in result_text and random_city in result_text:
109
- return "✅ Modell erfolgreich geladen und getestet! Sie können jetzt mit der Extraktion beginnen."
110
 
111
- return "⚠️ Modell-Test nicht erfolgreich. Bitte versuchen Sie es erneut in einigen Sekunden."
 
 
112
 
113
  except Exception as e:
114
  return f"❌ Fehler beim Laden des Modells: {str(e)}"
 
115
  @spaces.GPU
116
-
117
  def extract_info(template, text):
 
 
 
 
 
118
  try:
119
  prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
 
120
 
121
- payload = {
122
- "inputs": prompt,
123
- "parameters": {
124
- "max_new_tokens": 1000,
125
- "do_sample": False
126
- }
127
- }
128
 
129
- response = requests.post(API_URL, headers=headers, json=payload)
130
-
131
- if response.status_code == 503:
132
- response_json = response.json()
133
- if "error" in response_json and "loading" in response_json["error"]:
134
- estimated_time = response_json.get("estimated_time", "unknown")
135
- 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"
136
-
137
- if response.status_code != 200:
138
- return f"❌ API Fehler: {response.status_code}", response.text
139
-
140
- result = response.json()
141
-
142
- if isinstance(result, list) and len(result) > 0:
143
- result_text = result[0].get("generated_text", "")
144
- else:
145
- result_text = str(result)
146
 
147
  if "<|output|>" in result_text:
148
  json_text = result_text.split("<|output|>")[1].strip()
@@ -152,10 +137,10 @@ def extract_info(template, text):
152
  try:
153
  extracted = json.loads(json_text)
154
  formatted = json.dumps(extracted, indent=2)
 
155
  except json.JSONDecodeError:
156
  return "❌ JSON Parsing Fehler", json_text
157
 
158
- return "✅ Erfolgreich extrahiert", formatted
159
  except Exception as e:
160
  return f"❌ Fehler: {str(e)}", "{}"
161
 
 
1
  import gradio as gr
2
  import json
 
3
  import os
4
  import pandas as pd
5
  import folium
 
14
  import warnings
15
  import string
16
  import spaces
17
+ from transformers import AutoModelForCausalLM, AutoTokenizer
18
+ import torch
19
 
20
  warnings.filterwarnings("ignore")
21
 
 
27
  }
28
  }
29
 
30
+ # Model configuration
31
+ MODEL_NAME = "numind/NuExtract-1.5-tiny"
32
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
33
+ TORCH_DTYPE = torch.bfloat16 if DEVICE == "cuda" else torch.float32
34
+ MAX_INPUT_LENGTH = 20000 # For sliding window processing
35
+ MAX_NEW_TOKENS = 1000
36
+
37
+ # Global model variables
38
+ tokenizer = None
39
+ model = None
40
 
41
  class SafeGeocoder:
42
  def __init__(self):
 
50
  elapsed = current_time - self.last_request
51
  if elapsed < 1.0:
52
  time.sleep(1.0 - elapsed)
53
+ self.last_request = current_time
54
 
55
  def get_coords(self, location: str):
56
  if not location or pd.isna(location):
 
75
  self.cache[location] = None
76
  return None
77
 
 
 
 
78
  def load_model():
79
+ global tokenizer, model
80
  try:
81
  # Generate a random location and text each time
82
  random_city = random.choice(["Berlin", "Paris", "London", "Tokyo", "Rome", "Madrid"])
83
  random_suffix = ''.join(random.choices(string.ascii_lowercase, k=5))
84
  test_text = f"Test in {random_city}_{random_suffix}."
 
85
  test_template = '{"test_location": ""}'
86
 
87
+ # Initialize model if not already loaded
88
+ if model is None:
89
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
90
+ model = AutoModelForCausalLM.from_pretrained(
91
+ MODEL_NAME,
92
+ torch_dtype=TORCH_DTYPE,
93
+ trust_remote_code=True,
94
+ device_map="auto"
95
+ ).eval()
96
+ print(f"✅ Loaded {MODEL_NAME} on {DEVICE}")
 
 
 
 
 
 
 
 
97
 
98
+ # Test the model
99
+ prompt = f"<|input|>\n### Template:\n{test_template}\n### Text:\n{test_text}\n\n<|output|>"
100
+ inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
101
+ outputs = model.generate(**inputs, max_new_tokens=50)
102
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
103
 
104
+ if "<|output|>" in result and random_city in result:
105
+ return "✅ Modell erfolgreich geladen und getestet! Sie können jetzt mit der Extraktion beginnen."
106
+ return "⚠️ Modell-Test nicht erfolgreich. Bitte versuchen Sie es erneut."
107
 
108
  except Exception as e:
109
  return f"❌ Fehler beim Laden des Modells: {str(e)}"
110
+
111
  @spaces.GPU
 
112
  def extract_info(template, text):
113
+ global tokenizer, model
114
+
115
+ if model is None:
116
+ return "❌ Modell nicht geladen", "Bitte zuerst das Modell laden (1. Schritt)"
117
+
118
  try:
119
  prompt = f"<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>"
120
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=MAX_INPUT_LENGTH).to(DEVICE)
121
 
122
+ outputs = model.generate(
123
+ **inputs,
124
+ max_new_tokens=MAX_NEW_TOKENS,
125
+ temperature=0.0,
126
+ do_sample=False,
127
+ pad_token_id=tokenizer.eos_token_id
128
+ )
129
 
130
+ result_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
  if "<|output|>" in result_text:
133
  json_text = result_text.split("<|output|>")[1].strip()
 
137
  try:
138
  extracted = json.loads(json_text)
139
  formatted = json.dumps(extracted, indent=2)
140
+ return "✅ Erfolgreich extrahiert", formatted
141
  except json.JSONDecodeError:
142
  return "❌ JSON Parsing Fehler", json_text
143
 
 
144
  except Exception as e:
145
  return f"❌ Fehler: {str(e)}", "{}"
146