Update app.py
Browse files
app.py
CHANGED
|
@@ -10,36 +10,25 @@ all_langs = {iso[0]: (iso[1], iso[2], iso[3]) for iso in non_empty_isos} # {'Rom
|
|
| 10 |
iso1toall = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos} # {'ro': ('Romanian', 'rum', 'ron')}
|
| 11 |
DEFAULTS = None
|
| 12 |
|
| 13 |
-
|
| 14 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-mul", "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_nld",
|
| 15 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_fra_por_spa", "Helsinki-NLP/opus-mt-tc-bible-big-deu_eng_fra_por_spa-mul",
|
| 16 |
"Helsinki-NLP/opus-mt-tc-bible-big-roa-deu_eng_fra_por_spa", "Helsinki-NLP/opus-mt-tc-bible-big-deu_eng_fra_por_spa-roa", "Helsinki-NLP/opus-mt-tc-bible-big-roa-en",
|
| 17 |
-
"facebook/nllb-200-distilled-600M", "facebook/nllb-200-distilled-1.3B", "facebook/nllb-200-1.3B", "facebook/nllb-200-3.3B",
|
| 18 |
-
"facebook/mbart-large-50-many-to-many-mmt", "facebook/mbart-large-50-one-to-many-mmt", "facebook/mbart-large-50-many-to-one-mmt",
|
| 19 |
-
"facebook/hf-seamless-m4t-medium", "facebook/seamless-m4t-large", "facebook/seamless-m4t-v2-large",
|
| 20 |
-
"facebook/m2m100_418M", "facebook/m2m100_1.2B",
|
| 21 |
"alirezamsh/small100", "naist-nlp/mitre_466m", "naist-nlp/mitre_913m",
|
| 22 |
"bigscience/mt0-small", "bigscience/mt0-base", "bigscience/mt0-large", "bigscience/mt0-xl",
|
| 23 |
-
"bigscience/bloomz-560m", "bigscience/bloomz-1b1", "bigscience/bloomz-1b7", "bigscience/bloomz-3b",
|
| 24 |
-
"google/madlad400-3b-mt", "jbochi/madlad400-3b-mt",
|
| 25 |
-
"NiuTrans/LMT-60-0.6B", "NiuTrans/LMT-60-1.7B", "NiuTrans/LMT-60-4B",
|
| 26 |
-
"Lego-MT/Lego-MT", "BSC-LT/salamandraTA-2b-instruct",
|
| 27 |
"winninghealth/WiNGPT-Babel", "winninghealth/WiNGPT-Babel-2", "winninghealth/WiNGPT-Babel-2.1",
|
| 28 |
-
"Unbabel/Tower-Plus-2B", "utter-project/EuroLLM-1.7B", "utter-project/EuroLLM-1.7B-Instruct",
|
| 29 |
-
"yanolja/YanoljaNEXT-Rosetta-4B-2511", "yanolja/YanoljaNEXT-Rosetta-4B",
|
| 30 |
-
"google-t5/t5-small", "google-t5/t5-base", "google-t5/t5-large",
|
| 31 |
"google/flan-t5-small", "google/flan-t5-base", "google/flan-t5-large", "google/flan-t5-xl"]
|
| 32 |
|
| 33 |
def timer(func):
|
| 34 |
from time import time
|
| 35 |
def translate(input_text) -> tuple[str, str]:
|
| 36 |
start_time = time()
|
| 37 |
-
|
| 38 |
end_time = time()
|
| 39 |
execution_time = end_time - start_time
|
| 40 |
# print(f"Function {func.__name__!r} executed in {execution_time:.2f} seconds.")
|
| 41 |
-
|
| 42 |
-
return
|
| 43 |
return translate
|
| 44 |
|
| 45 |
@timer
|
|
@@ -53,7 +42,7 @@ def detect_language(input_text: str) -> tuple[str, str]:
|
|
| 53 |
Returns:
|
| 54 |
tuple:
|
| 55 |
detected_text(str): The input text translated to the selected target language
|
| 56 |
-
confidence(str):
|
| 57 |
|
| 58 |
Example:
|
| 59 |
>>> detect_language("Hello world")
|
|
@@ -67,16 +56,18 @@ def detect_language(input_text: str) -> tuple[str, str]:
|
|
| 67 |
return langcode, round(number=langecode_probabilities[0].prob * 100, ndigits=2)
|
| 68 |
|
| 69 |
with gr.Blocks() as interface:
|
| 70 |
-
gr.Markdown("###
|
| 71 |
input_text = gr.Textbox(label="Enter text to detect:", placeholder="Type your text here, maximum 512 tokens",
|
| 72 |
autofocus=True, submit_btn='Detect Language', max_length=512)
|
| 73 |
with gr.Row(variant="compact"):
|
| 74 |
detected_text = gr.Textbox(label="Translated text:", placeholder="Display field for translation", interactive=False, buttons=["copy"], lines=1)
|
| 75 |
-
confidence = gr.Textbox(label="Confidence:", placeholder="Display field for confidence score", interactive=False, lines=1)
|
|
|
|
|
|
|
| 76 |
input_text.submit(
|
| 77 |
fn=detect_language,
|
| 78 |
inputs=[input_text],
|
| 79 |
-
outputs=[detected_text, confidence]
|
| 80 |
)
|
| 81 |
if __name__ == "__main__":
|
| 82 |
interface.launch(mcp_server=True, footer_links=["api", "settings"])
|
|
|
|
| 10 |
iso1toall = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos} # {'ro': ('Romanian', 'rum', 'ron')}
|
| 11 |
DEFAULTS = None
|
| 12 |
|
| 13 |
+
libraries = ["langdetect", "QUICKMT", "Argos", "HPLT", "HPLT-OPUS", "Google",
|
| 14 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-mul", "Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_nld",
|
| 15 |
"Helsinki-NLP/opus-mt-tc-bible-big-mul-deu_eng_fra_por_spa", "Helsinki-NLP/opus-mt-tc-bible-big-deu_eng_fra_por_spa-mul",
|
| 16 |
"Helsinki-NLP/opus-mt-tc-bible-big-roa-deu_eng_fra_por_spa", "Helsinki-NLP/opus-mt-tc-bible-big-deu_eng_fra_por_spa-roa", "Helsinki-NLP/opus-mt-tc-bible-big-roa-en",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"alirezamsh/small100", "naist-nlp/mitre_466m", "naist-nlp/mitre_913m",
|
| 18 |
"bigscience/mt0-small", "bigscience/mt0-base", "bigscience/mt0-large", "bigscience/mt0-xl",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"winninghealth/WiNGPT-Babel", "winninghealth/WiNGPT-Babel-2", "winninghealth/WiNGPT-Babel-2.1",
|
|
|
|
|
|
|
|
|
|
| 20 |
"google/flan-t5-small", "google/flan-t5-base", "google/flan-t5-large", "google/flan-t5-xl"]
|
| 21 |
|
| 22 |
def timer(func):
|
| 23 |
from time import time
|
| 24 |
def translate(input_text) -> tuple[str, str]:
|
| 25 |
start_time = time()
|
| 26 |
+
detected_lang, confidence = func(input_text)
|
| 27 |
end_time = time()
|
| 28 |
execution_time = end_time - start_time
|
| 29 |
# print(f"Function {func.__name__!r} executed in {execution_time:.2f} seconds.")
|
| 30 |
+
execution_times = f'Executed in {execution_time:.2f} seconds!'
|
| 31 |
+
return detected_lang, confidence, execution_times
|
| 32 |
return translate
|
| 33 |
|
| 34 |
@timer
|
|
|
|
| 42 |
Returns:
|
| 43 |
tuple:
|
| 44 |
detected_text(str): The input text translated to the selected target language
|
| 45 |
+
confidence(str): The confidence score as float
|
| 46 |
|
| 47 |
Example:
|
| 48 |
>>> detect_language("Hello world")
|
|
|
|
| 56 |
return langcode, round(number=langecode_probabilities[0].prob * 100, ndigits=2)
|
| 57 |
|
| 58 |
with gr.Blocks() as interface:
|
| 59 |
+
gr.Markdown("### Language Detection with Gradio API and MCP Server")
|
| 60 |
input_text = gr.Textbox(label="Enter text to detect:", placeholder="Type your text here, maximum 512 tokens",
|
| 61 |
autofocus=True, submit_btn='Detect Language', max_length=512)
|
| 62 |
with gr.Row(variant="compact"):
|
| 63 |
detected_text = gr.Textbox(label="Translated text:", placeholder="Display field for translation", interactive=False, buttons=["copy"], lines=1)
|
| 64 |
+
confidence = gr.Textbox(label="Confidence:", placeholder="Display field for confidence score", interactive=False, buttons=["copy"], lines=1)
|
| 65 |
+
execution_time = gr.Textbox(label="Execution time:", placeholder="Display field for execution time", interactive=False, lines=1)
|
| 66 |
+
gr.CheckboxGroup(choices=["langdetect", "X", "XX"], label="Detection libraries", info="Detection libraries")
|
| 67 |
input_text.submit(
|
| 68 |
fn=detect_language,
|
| 69 |
inputs=[input_text],
|
| 70 |
+
outputs=[detected_text, confidence, execution_time]
|
| 71 |
)
|
| 72 |
if __name__ == "__main__":
|
| 73 |
interface.launch(mcp_server=True, footer_links=["api", "settings"])
|