Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
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
|
| 13 |
-
#
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
|
| 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=
|
| 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=
|
| 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=
|
| 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=
|
| 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=
|
| 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=
|
| 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=
|
| 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="
|
| 781 |
)
|
| 782 |
edit_instruction = gr.Textbox(
|
| 783 |
label="Instruction",
|
|
@@ -799,7 +794,7 @@ with gr.Blocks(
|
|
| 799 |
|
| 800 |
gr.Examples(
|
| 801 |
examples=[
|
| 802 |
-
["
|
| 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",
|