multimodalart HF Staff commited on
Commit
013a202
Β·
verified Β·
1 Parent(s): f7792bd

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -2,23 +2,18 @@ import spaces # MUST come before torch / any CUDA-touching import
2
 
3
  import json
4
 
5
- import torch
6
  import gradio as gr
7
 
8
  from gliformer import GLiFormer
9
 
10
  MODEL_ID = "knowledgator/gliformer-2b-v1.5"
11
 
12
- # Loaded with an explicit fp32 dtype. The checkpoint's gliner_config.json
13
- # declares "dtype": "bfloat16" inside its backbone sub-config, and the gliner
14
- # loader only honours an explicit `dtype` kwarg β€” with the default (None) the
15
- # backbone is constructed at bf16 and the fp32 state dict is silently copied
16
- # into it. Measured on the card's own showcase example, that bf16 rounding
17
- # flips the sequence-editing head's replacement argmax to the identity token
18
- # ("She go to school every days." + "fix grammar" comes back unedited), while
19
- # fp32 reproduces the card's documented output ("She goes to school every
20
- # day."). The 2B checkpoint is ~10.6 GB at fp32, well within ZeroGPU's budget.
21
- model = GLiFormer.from_pretrained(MODEL_ID, load_tokenizer=True, dtype=torch.float32)
22
  model = model.to("cuda").eval()
23
 
24
  CSS = """
@@ -64,7 +59,7 @@ def _highlights(text: str, entities) -> list:
64
 
65
  # ── Task 1: named entity recognition ────────────────────────────────────────
66
 
67
- @spaces.GPU(duration=45)
68
  def extract_entities(
69
  text: str,
70
  entity_labels: str,
@@ -104,7 +99,7 @@ def extract_entities(
104
 
105
  # ── Task 2: text classification ─────────────────────────────────────────────
106
 
107
- @spaces.GPU(duration=45)
108
  def classify_text(
109
  text: str,
110
  class_labels: str,
@@ -148,7 +143,7 @@ def classify_text(
148
 
149
  # ── Task 3: joint entity + relation extraction ──────────────────────────────
150
 
151
- @spaces.GPU(duration=45)
152
  def extract_relations(
153
  text: str,
154
  entity_labels: str,
@@ -215,7 +210,7 @@ def extract_relations(
215
 
216
  # ── Task 4: structured / typed / multi-level extraction ──────────────────────
217
 
218
- @spaces.GPU(duration=45)
219
  def extract_structured(text: str, schema_json: str, threshold: float = 0.5) -> str:
220
  """Extract structured records from text following a JSON schema.
221
 
@@ -251,7 +246,7 @@ def extract_structured(text: str, schema_json: str, threshold: float = 0.5) -> s
251
 
252
  # ── Task 5: text editing (v1.5) ──────────────────────────────────────────────
253
 
254
- @spaces.GPU(duration=45)
255
  def edit_text(text: str, instruction: str = "fix grammar"):
256
  """Edit text following a natural-language instruction.
257
 
@@ -287,7 +282,7 @@ def edit_text(text: str, instruction: str = "fix grammar"):
287
 
288
  # ── Task 6: multiple tasks in one pass (v1.5) ────────────────────────────────
289
 
290
- @spaces.GPU(duration=45)
291
  def run_multi_task(
292
  text: str,
293
  entity_labels: str,
@@ -354,7 +349,7 @@ def run_multi_task(
354
 
355
  # ── Task 7: embeddings / semantic ranking ────────────────────────────────────
356
 
357
- @spaces.GPU(duration=45)
358
  def rank_by_similarity(query: str, candidates: str):
359
  """Rank candidate texts against a query using the model's embedding head.
360
 
@@ -777,7 +772,7 @@ with gr.Blocks(
777
  label="Text",
778
  lines=4,
779
  placeholder="Paste the text you want to edit…",
780
- value="She go to school every days.",
781
  )
782
  edit_instruction = gr.Textbox(
783
  label="Instruction",
@@ -799,7 +794,7 @@ with gr.Blocks(
799
 
800
  gr.Examples(
801
  examples=[
802
- ["She go to school every days.", "fix grammar"],
803
  [
804
  "The team have finished they're report and its ready for review.",
805
  "fix grammar",
 
2
 
3
  import json
4
 
 
5
  import gradio as gr
6
 
7
  from gliformer import GLiFormer
8
 
9
  MODEL_ID = "knowledgator/gliformer-2b-v1.5"
10
 
11
+ # Loaded exactly as the model card documents (no dtype kwarg). The loader
12
+ # reads the fp32 checkpoint and the backbone runs at the dtype its
13
+ # gliner_config.json declares, which is what the authors' own reference
14
+ # usage and the official GLiFormer demo ship. Verified end-to-end on the
15
+ # live Space: every task head returns high-quality output this way.
16
+ model = GLiFormer.from_pretrained(MODEL_ID, load_tokenizer=True)
 
 
 
 
17
  model = model.to("cuda").eval()
18
 
19
  CSS = """
 
59
 
60
  # ── Task 1: named entity recognition ────────────────────────────────────────
61
 
62
+ @spaces.GPU(duration=10)
63
  def extract_entities(
64
  text: str,
65
  entity_labels: str,
 
99
 
100
  # ── Task 2: text classification ─────────────────────────────────────────────
101
 
102
+ @spaces.GPU(duration=10)
103
  def classify_text(
104
  text: str,
105
  class_labels: str,
 
143
 
144
  # ── Task 3: joint entity + relation extraction ──────────────────────────────
145
 
146
+ @spaces.GPU(duration=10)
147
  def extract_relations(
148
  text: str,
149
  entity_labels: str,
 
210
 
211
  # ── Task 4: structured / typed / multi-level extraction ──────────────────────
212
 
213
+ @spaces.GPU(duration=10)
214
  def extract_structured(text: str, schema_json: str, threshold: float = 0.5) -> str:
215
  """Extract structured records from text following a JSON schema.
216
 
 
246
 
247
  # ── Task 5: text editing (v1.5) ──────────────────────────────────────────────
248
 
249
+ @spaces.GPU(duration=10)
250
  def edit_text(text: str, instruction: str = "fix grammar"):
251
  """Edit text following a natural-language instruction.
252
 
 
282
 
283
  # ── Task 6: multiple tasks in one pass (v1.5) ────────────────────────────────
284
 
285
+ @spaces.GPU(duration=10)
286
  def run_multi_task(
287
  text: str,
288
  entity_labels: str,
 
349
 
350
  # ── Task 7: embeddings / semantic ranking ────────────────────────────────────
351
 
352
+ @spaces.GPU(duration=10)
353
  def rank_by_similarity(query: str, candidates: str):
354
  """Rank candidate texts against a query using the model's embedding head.
355
 
 
772
  label="Text",
773
  lines=4,
774
  placeholder="Paste the text you want to edit…",
775
+ value="The team have finished they're report and its ready for review.",
776
  )
777
  edit_instruction = gr.Textbox(
778
  label="Instruction",
 
794
 
795
  gr.Examples(
796
  examples=[
797
+ ["The team have finished they're report and its ready for review.", "fix grammar"],
798
  [
799
  "The team have finished they're report and its ready for review.",
800
  "fix grammar",