Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,36 @@ import gradio as gr
|
|
| 2 |
import spaces
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
nlp_pipeline = pipeline(
|
| 8 |
"token-classification",
|
| 9 |
model=model_id,
|
| 10 |
-
aggregation_strategy=
|
| 11 |
ignore_labels=[]
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# The @spaces.GPU decorator dynamically requests GPU compute for inference
|
| 15 |
@spaces.GPU
|
| 16 |
def classify_text(text):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# Gradio Interface
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=classify_text,
|
| 26 |
-
inputs=gr.Textbox(placeholder="Enter
|
| 27 |
outputs="textbox",
|
| 28 |
title="Code-Switched Language Identification (ZeroGPU)",
|
| 29 |
description="XLM-RoBERTa Token Classification powered by Hugging Face ZeroGPU."
|
|
|
|
| 2 |
import spaces
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
model_id = "your-actual-username/code-switching-codesaviours-si26-hania"
|
| 6 |
+
|
| 7 |
+
# Set aggregation_strategy to None to stop merging words into single chunks
|
| 8 |
nlp_pipeline = pipeline(
|
| 9 |
"token-classification",
|
| 10 |
model=model_id,
|
| 11 |
+
aggregation_strategy=None,
|
| 12 |
ignore_labels=[]
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
@spaces.GPU
|
| 16 |
def classify_text(text):
|
| 17 |
+
# Split input by words to classify each token individually
|
| 18 |
+
words = text.split()
|
| 19 |
+
results = []
|
| 20 |
+
|
| 21 |
+
for word in words:
|
| 22 |
+
predictions = nlp_pipeline(word)
|
| 23 |
+
if predictions:
|
| 24 |
+
# Pick the top token prediction for each individual word
|
| 25 |
+
top_pred = predictions[0]
|
| 26 |
+
label = top_pred.get('entity', top_pred.get('entity_group', 'UNK'))
|
| 27 |
+
score = top_pred['score']
|
| 28 |
+
results.append(f"Word: '{word}' | Label: {label} | Score: {score:.2f}")
|
| 29 |
+
|
| 30 |
+
return "\n".join(results)
|
| 31 |
|
|
|
|
| 32 |
demo = gr.Interface(
|
| 33 |
fn=classify_text,
|
| 34 |
+
inputs=gr.Textbox(placeholder="Enter text..."),
|
| 35 |
outputs="textbox",
|
| 36 |
title="Code-Switched Language Identification (ZeroGPU)",
|
| 37 |
description="XLM-RoBERTa Token Classification powered by Hugging Face ZeroGPU."
|