Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the translation pipeline
|
5 |
+
pipe = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
6 |
+
|
7 |
+
# Define the translation function
|
8 |
+
def translate_text(text, source_language, target_language, max_length=200):
|
9 |
+
# Define source and target languages (e.g., 'eng_Latn' -> 'fra_Latn')
|
10 |
+
language_pair = f"{source_language}-{target_language}"
|
11 |
+
response = pipe(
|
12 |
+
text,
|
13 |
+
src_lang=source_language,
|
14 |
+
tgt_lang=target_language,
|
15 |
+
max_length=max_length
|
16 |
+
)
|
17 |
+
return response[0]['translation_text']
|
18 |
+
|
19 |
+
# Create the Gradio app interface
|
20 |
+
with gr.Blocks() as app:
|
21 |
+
gr.Markdown("## Translation App")
|
22 |
+
gr.Markdown(
|
23 |
+
"Enter text in the source language, and the model will translate it to the target language. "
|
24 |
+
"This app uses the `facebook/nllb-200-distilled-600M` model."
|
25 |
+
)
|
26 |
+
|
27 |
+
with gr.Row():
|
28 |
+
input_text = gr.Textbox(
|
29 |
+
label="Input Text",
|
30 |
+
placeholder="Type the text to translate...",
|
31 |
+
lines=4
|
32 |
+
)
|
33 |
+
output_translation = gr.Textbox(label="Translated Text", lines=4)
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
source_language = gr.Textbox(
|
37 |
+
label="Source Language Code",
|
38 |
+
placeholder="e.g., eng_Latn (for English)"
|
39 |
+
)
|
40 |
+
target_language = gr.Textbox(
|
41 |
+
label="Target Language Code",
|
42 |
+
placeholder="e.g., fra_Latn (for French)"
|
43 |
+
)
|
44 |
+
|
45 |
+
max_length = gr.Slider(
|
46 |
+
label="Max Length",
|
47 |
+
minimum=50,
|
48 |
+
maximum=500,
|
49 |
+
step=50,
|
50 |
+
value=200
|
51 |
+
)
|
52 |
+
|
53 |
+
submit_button = gr.Button("Translate")
|
54 |
+
|
55 |
+
submit_button.click(
|
56 |
+
fn=translate_text,
|
57 |
+
inputs=[input_text, source_language, target_language, max_length],
|
58 |
+
outputs=output_translation
|
59 |
+
)
|
60 |
+
|
61 |
+
# Launch the app
|
62 |
+
if __name__ == "__main__":
|
63 |
+
app.launch()
|