Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,26 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForTokenClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# Cargar el modelo y el tokenizador
|
6 |
+
model_name = "EmergentMethods/gliner_medium_news-v2.1"
|
7 |
+
model = AutoModelForTokenClassification.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
+
def predict(text):
|
11 |
+
inputs = tokenizer(text, return_tensors="pt")
|
12 |
+
|
13 |
+
# Realizar la inferencia
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = model(**inputs)
|
16 |
+
|
17 |
+
logits = outputs.logits
|
18 |
+
predictions = torch.argmax(logits, dim=2)
|
19 |
+
|
20 |
+
id2label = model.config.id2label
|
21 |
+
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
22 |
+
entities = [{"token": token, "label": id2label[prediction.item()]} for token, prediction in zip(tokens, predictions[0])]
|
23 |
+
return entities
|
24 |
+
|
25 |
+
demo = gr.Interface(fn=predict, inputs="text", outputs="json")
|
26 |
demo.launch()
|