Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "alexneakameni/language_detection"
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Get label mapping
|
| 12 |
+
id2label = model.config.id2label
|
| 13 |
+
|
| 14 |
+
def predict_language(text, top_k=5):
|
| 15 |
+
"""Predicts the top-k languages for the given text."""
|
| 16 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512).to(device)
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
logits = model(**inputs).logits
|
| 19 |
+
|
| 20 |
+
probs = torch.nn.functional.softmax(logits, dim=-1).squeeze()
|
| 21 |
+
top_probs, top_indices = torch.topk(probs, top_k)
|
| 22 |
+
|
| 23 |
+
results = [f"{id2label[str(idx.item())]}: {prob:.4f}" for prob, idx in zip(top_probs, top_indices)]
|
| 24 |
+
return "\n".join(results)
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=predict_language,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Textbox(label="Enter text", placeholder="Type a sentence here..."),
|
| 31 |
+
gr.Slider(1, 10, value=5, step=1, label="Top-k Languages")
|
| 32 |
+
],
|
| 33 |
+
outputs=gr.Textbox(label="Predicted Languages"),
|
| 34 |
+
title="🌍 Language Detection",
|
| 35 |
+
description="Detects the language of a given text using a fine-tuned BERT model. Returns the top-k most probable languages."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|