prachuryyaIITG commited on
Commit
73c4e06
·
verified ·
1 Parent(s): 126b2e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -44
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Import spaces & other imports
2
  import spaces
3
  import gradio as gr
4
  import torch
@@ -10,12 +10,12 @@ from transformers import pipeline
10
 
11
  # ZeroGPU Configuration
12
  MAX_MODELS_LOADED = 5
13
- MODEL_IDLE_TIMEOUT = 15 * 60 # 15 minutes
14
- CLEANUP_INTERVAL = 5 * 60 # check every 5 minutes
15
 
16
  DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32
17
 
18
- # Model mapping (unchanged)
19
  MODELS = {
20
  "Assamese": "prachuryyaIITG/CLASSER_Assamese_MuRIL",
21
  "Bengali": "prachuryyaIITG/MultiCoNER2_Bengali_XLM",
@@ -55,7 +55,7 @@ MODELS = {
55
  "Urdu": "prachuryyaIITG/CLASSER_Urdu_XLM",
56
  }
57
 
58
- # Cache and locking
59
  pipelines = OrderedDict()
60
  last_used = {}
61
  lock = threading.Lock()
@@ -69,7 +69,7 @@ def clear_memory():
69
  torch.cuda.empty_cache()
70
  torch.cuda.ipc_collect()
71
 
72
- # Load pipeline (ZeroGPU safe)
73
  def load_pipeline(model_id):
74
 
75
  ner = pipeline(
@@ -82,7 +82,7 @@ def load_pipeline(model_id):
82
 
83
  return ner
84
 
85
- # LRU manager
86
  def get_pipeline(model_id):
87
 
88
  with lock:
@@ -111,7 +111,7 @@ def get_pipeline(model_id):
111
 
112
  return ner
113
 
114
- # Background cleanup
115
  def cleanup_worker():
116
 
117
  while True:
@@ -122,26 +122,26 @@ def cleanup_worker():
122
 
123
  now = time.time()
124
 
125
- to_remove = []
126
 
127
  for model_id in last_used:
128
 
129
  if now - last_used[model_id] > MODEL_IDLE_TIMEOUT:
130
- to_remove.append(model_id)
131
 
132
- for model_id in to_remove:
133
 
134
  del pipelines[model_id]
135
  del last_used[model_id]
136
 
137
- if to_remove:
138
  clear_memory()
139
 
140
  threading.Thread(target=cleanup_worker, daemon=True).start()
141
 
142
- # GPU inference (required)
143
  @spaces.GPU
144
- def gpu_infer(text, language):
145
 
146
  model_id = MODELS[language]
147
 
@@ -149,20 +149,9 @@ def gpu_infer(text, language):
149
 
150
  results = ner(text)
151
 
152
- return {
153
- "text": text,
154
- "entities": results
155
- }
156
 
157
- # Public wrapper
158
- def get_ner_results(text, language):
159
-
160
- if not text:
161
- return {"text": "", "entities": []}
162
-
163
- return gpu_infer(text, language)
164
-
165
- # Original CSS (unchanged)
166
  custom_css = """
167
  #blue-button {
168
  background-color: #00568b !important;
@@ -173,24 +162,23 @@ custom_css = """
173
  background-color: #00488b !important;
174
  }
175
  """
176
- # UI with ORIGINAL MARKDOWN (exact)
 
177
  with gr.Blocks() as demo:
178
 
179
  gr.Markdown("# AWED-FiNER: Agents, Web applications, and Expert Detectors for Fine-grained Named Entity Recognition across 36 languages for 6.6 billion speakers")
180
 
181
  gr.Markdown("A tool for Fine-grained NER across 36 languages, ranging from four most spoken languages (English, Chinese, Hindi, & Spanish) to four extremely low-resource vulnerable languages (Bishnupriya, Bodo, Manipuri, & Mizo)")
182
 
183
- gr.Markdown("The models used in this tool are based on the [SampurNER](https://huggingface.co/datasets/prachuryyaIITG/SampurNER), [MultiCoNERv2](https://huggingface.co/datasets/MultiCoNER/multiconer_v2), [CLASSER](https://huggingface.co/datasets/prachuryyaIITG/CLASSER), [FiNE-MiBBiC](https://github.com/PrachuryyaKaushik/FiNE-MiBBiC), [FiNERVINER](https://huggingface.co/datasets/prachuryyaIITG/FiNERVINER) and [APTFiNER](https://huggingface.co/datasets/prachuryyaIITG/APTFiNER) research works.")
184
 
185
  gr.Markdown("To access the agentic tool, please check out: [AWED-FiNER tool](https://github.com/PrachuryyaKaushik/AWED-FiNER)")
186
 
187
- with gr.Row():
188
-
189
- lang_dropdown = gr.Dropdown(
190
- choices=list(MODELS.keys()),
191
- value="English",
192
- label="1. Select Language"
193
- )
194
 
195
  input_text = gr.Textbox(
196
  value="Jude Bellingham joined Real Madrid in 2023.",
@@ -199,11 +187,7 @@ with gr.Blocks() as demo:
199
  lines=3
200
  )
201
 
202
- submit_btn = gr.Button(
203
- "Analyze",
204
- variant="primary",
205
- elem_id="blue-button"
206
- )
207
 
208
  output = gr.HighlightedText(
209
  label="3. Fine-grained NER Results",
@@ -211,7 +195,7 @@ with gr.Blocks() as demo:
211
  )
212
 
213
  submit_btn.click(
214
- fn=get_ner_results,
215
  inputs=[input_text, lang_dropdown],
216
  outputs=output,
217
  api_name="predict"
@@ -236,7 +220,7 @@ with gr.Blocks() as demo:
236
  ["சச்சின் டெண்டுல்கர் சென்னையில் கிரிக்கெட் விளையாடினார்.", "Tamil"],
237
  ["فردوسی در توس به دنیا آمد.", "Farsi"],
238
  ["Dante Alighieri è nato a Firenze.", "Italian"],
239
- ["રવિન્દ્રનાથ ટાગોરે કલકત્તામાં ગીતાંજલિ લખી હતી.", "Gujarati"],
240
  ["ಕೆ. ಶಿವರಾಮ ಕಾರಂತರು ಪುತ್ತೂರಿನಲ್ಲಿ ಜನಿಸಿದರು.", "Kannada"],
241
  ["Тарас Шевченко народився в Моринцях.", "Ukrainian"],
242
  ["भानुभक्त आचार्य तनहुँमा जन्मिएका हुन्।", "Nepali"],
@@ -247,7 +231,7 @@ with gr.Blocks() as demo:
247
  ],
248
  inputs=[input_text, lang_dropdown],
249
  outputs=output,
250
- fn=get_ner_results,
251
  cache_examples=False
252
  )
253
 
 
1
+ # Import spaces and others
2
  import spaces
3
  import gradio as gr
4
  import torch
 
10
 
11
  # ZeroGPU Configuration
12
  MAX_MODELS_LOADED = 5
13
+ MODEL_IDLE_TIMEOUT = 15 * 60
14
+ CLEANUP_INTERVAL = 5 * 60
15
 
16
  DTYPE = torch.bfloat16 if torch.cuda.is_available() else torch.float32
17
 
18
+ # Model mapping
19
  MODELS = {
20
  "Assamese": "prachuryyaIITG/CLASSER_Assamese_MuRIL",
21
  "Bengali": "prachuryyaIITG/MultiCoNER2_Bengali_XLM",
 
55
  "Urdu": "prachuryyaIITG/CLASSER_Urdu_XLM",
56
  }
57
 
58
+ # Model cache with LRU
59
  pipelines = OrderedDict()
60
  last_used = {}
61
  lock = threading.Lock()
 
69
  torch.cuda.empty_cache()
70
  torch.cuda.ipc_collect()
71
 
72
+ # Load pipeline
73
  def load_pipeline(model_id):
74
 
75
  ner = pipeline(
 
82
 
83
  return ner
84
 
85
+ # Pipeline manager
86
  def get_pipeline(model_id):
87
 
88
  with lock:
 
111
 
112
  return ner
113
 
114
+ # Cleanup thread
115
  def cleanup_worker():
116
 
117
  while True:
 
122
 
123
  now = time.time()
124
 
125
+ remove = []
126
 
127
  for model_id in last_used:
128
 
129
  if now - last_used[model_id] > MODEL_IDLE_TIMEOUT:
130
+ remove.append(model_id)
131
 
132
+ for model_id in remove:
133
 
134
  del pipelines[model_id]
135
  del last_used[model_id]
136
 
137
+ if remove:
138
  clear_memory()
139
 
140
  threading.Thread(target=cleanup_worker, daemon=True).start()
141
 
142
+ # GPU inference API (Agent-compatible)
143
  @spaces.GPU
144
+ def predict(text, language):
145
 
146
  model_id = MODELS[language]
147
 
 
149
 
150
  results = ner(text)
151
 
152
+ return results
 
 
 
153
 
154
+ # CSS
 
 
 
 
 
 
 
 
155
  custom_css = """
156
  #blue-button {
157
  background-color: #00568b !important;
 
162
  background-color: #00488b !important;
163
  }
164
  """
165
+
166
+ # UI (Original Markdown preserved exactly)
167
  with gr.Blocks() as demo:
168
 
169
  gr.Markdown("# AWED-FiNER: Agents, Web applications, and Expert Detectors for Fine-grained Named Entity Recognition across 36 languages for 6.6 billion speakers")
170
 
171
  gr.Markdown("A tool for Fine-grained NER across 36 languages, ranging from four most spoken languages (English, Chinese, Hindi, & Spanish) to four extremely low-resource vulnerable languages (Bishnupriya, Bodo, Manipuri, & Mizo)")
172
 
173
+ gr.Markdown("The models used in this tool are based on the [SampurNER](https://huggingface.co/datasets/prachuryyaIITG/SampurNER), [MultiCoNERv2](https://huggingface.co/datasets/MultiCoNER/multiconer_v2), [CLASSER](https://huggingface.co/datasets/prachuryyaIITG/CLASSER), FiNE-MiBBiC, [FiNERVINER](https://huggingface.co/datasets/prachuryyaIITG/FiNERVINER) and [APTFiNER](https://huggingface.co/datasets/prachuryyaIITG/APTFiNER) research works.")
174
 
175
  gr.Markdown("To access the agentic tool, please check out: [AWED-FiNER tool](https://github.com/PrachuryyaKaushik/AWED-FiNER)")
176
 
177
+ lang_dropdown = gr.Dropdown(
178
+ choices=list(MODELS.keys()),
179
+ value="English",
180
+ label="1. Select Language"
181
+ )
 
 
182
 
183
  input_text = gr.Textbox(
184
  value="Jude Bellingham joined Real Madrid in 2023.",
 
187
  lines=3
188
  )
189
 
190
+ submit_btn = gr.Button("Analyze", elem_id="blue-button")
 
 
 
 
191
 
192
  output = gr.HighlightedText(
193
  label="3. Fine-grained NER Results",
 
195
  )
196
 
197
  submit_btn.click(
198
+ fn=predict,
199
  inputs=[input_text, lang_dropdown],
200
  outputs=output,
201
  api_name="predict"
 
220
  ["சச்சின் டெண்டுல்கர் சென்னையில் கிரிக்கெட் விளையாடினார்.", "Tamil"],
221
  ["فردوسی در توس به دنیا آمد.", "Farsi"],
222
  ["Dante Alighieri è nato a Firenze.", "Italian"],
223
+ ["રવિન્દ્રનাথ ટાગોરે કલકત્તામાં ગીતાંજલિ લખી હતી.", "Gujarati"],
224
  ["ಕೆ. ಶಿವರಾಮ ಕಾರಂತರು ಪುತ್ತೂರಿನಲ್ಲಿ ಜನಿಸಿದರು.", "Kannada"],
225
  ["Тарас Шевченко народився в Моринцях.", "Ukrainian"],
226
  ["भानुभक्त आचार्य तनहुँमा जन्मिएका हुन्।", "Nepali"],
 
231
  ],
232
  inputs=[input_text, lang_dropdown],
233
  outputs=output,
234
+ fn=predict,
235
  cache_examples=False
236
  )
237