File size: 1,292 Bytes
de6e05c
 
 
 
 
487660b
 
 
de6e05c
 
487660b
de6e05c
 
 
487660b
08b4bcb
487660b
 
 
08b4bcb
 
 
 
de6e05c
 
 
 
 
08b4bcb
de6e05c
08b4bcb
de6e05c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load model and tokenizer from Hugging Face
model_name = "iro-malta07/distilbert-base-german-lang-level-class"  # replace with your model path
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Use id2label from the config
id2label = model.config.id2label

# Inference function
def classify_text(text):
    if not text.endswith("."):
        text += "."
    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
    # Create a dictionary of labels and their corresponding probabilities
    confidences = {id2label[i]: float(probs[i]) for i in range(len(probs))}
    return confidences

# Gradio interface
demo = gr.Interface(
    fn=classify_text,
    inputs=gr.Textbox(lines=4, placeholder="Schreibe etwas auf Deutsch..."),
    outputs=gr.Label(num_top_classes=4),
    title="German Language Level Classifier",
    description="Enter German text and get the predicted CEFR level (A1 to C2). 🚧 Work in progress. 🚧"
)

# Launch app
demo.launch()