File size: 899 Bytes
cd71737
 
345b2db
 
cd71737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import fasttext
from huggingface_hub import hf_hub_download
import gradio as gr

model_path = hf_hub_download(repo_id="facebook/fasttext-language-identification", filename="model.bin")
model = fasttext.load_model(model_path)

def predict(text, top):
    labels, probabilities = model.predict(text, k=top)
    cleaned_labels = [label.replace('__label__', '') for label in labels]
    result = dict(zip(cleaned_labels, probabilities))
    #result = sorted(result, key=lambda x: x[1], reverse=True)
    return result

demo = gr.Interface(
    fn=predict,
    inputs=[
      gr.Textbox(lines=1, placeholder="Text", label="Content"),
      gr.Number(value=5, info='number of predictions that should be returned', minimum=1, maximum=100,  label="Top"),
    ],
    title="Language Identification Demo",
    flagging_mode="never",
    outputs=gr.Label(label="Result"))
demo.launch(share=True, show_api=True)