balaji4991512 commited on
Commit
9185bc5
·
verified ·
1 Parent(s): 187cb31

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ import pycountry
5
+
6
+ # Load a language‐detection model with bfloat16 precision
7
+ language_detector = pipeline(
8
+ "text-classification",
9
+ model="papluca/xlm-roberta-base-language-detection",
10
+ torch_dtype=torch.bfloat16
11
+ )
12
+
13
+ def detect_language(text: str) -> str:
14
+ """
15
+ Detects the language of the given text and returns
16
+ both the full language name and its ISO code with confidence.
17
+ """
18
+ result = language_detector(text)[0]
19
+ code = result["label"] # e.g. "en", "ta", "fr"
20
+ score = result["score"]
21
+
22
+ # Map ISO code to full language name using pycountry
23
+ try:
24
+ lang = pycountry.languages.get(alpha_2=code).name
25
+ except:
26
+ lang = code.upper()
27
+
28
+ return f"{lang} ({code}) — {score:.2f}"
29
+
30
+ # Build Gradio interface
31
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
32
+ gr.Markdown(
33
+ """
34
+ # 🌐 Text Language Detector
35
+ Type or paste text below to detect its language (name + code + confidence).
36
+ """
37
+ )
38
+
39
+ with gr.Row():
40
+ text_input = gr.Textbox(
41
+ label="📝 Input Text",
42
+ placeholder="Type or paste text here...",
43
+ lines=4,
44
+ show_copy_button=True
45
+ )
46
+ lang_output = gr.Textbox(
47
+ label="✅ Detected Language",
48
+ placeholder="Full language name, ISO code, and confidence will appear here",
49
+ lines=1,
50
+ interactive=False
51
+ )
52
+
53
+ detect_btn = gr.Button("🔍 Detect Language")
54
+ detect_btn.click(fn=detect_language, inputs=text_input, outputs=lang_output)
55
+
56
+ gr.Markdown(
57
+ """
58
+ ---
59
+ Built with 🤗 Transformers (`papluca/xlm-roberta-base-language-detection`),
60
+ `pycountry` for language names, and 🚀 Gradio
61
+ """
62
+ )
63
+
64
+ demo.launch()