Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load the translation pipeline using the NLLB-200 model
|
| 5 |
+
pipe = pipeline("translation", model="facebook/nllb-200-3.3B")
|
| 6 |
+
|
| 7 |
+
# Function to translate text
|
| 8 |
+
def translate_text(text, source_lang, target_lang):
|
| 9 |
+
# Translate the input text
|
| 10 |
+
translation = pipe(text, src_lang=source_lang, tgt_lang=target_lang)
|
| 11 |
+
return translation[0]['translation_text']
|
| 12 |
+
|
| 13 |
+
# Set up Gradio interface
|
| 14 |
+
with gr.Blocks() as demo:
|
| 15 |
+
gr.Markdown("# Text Translation using NLLB-200 (facebook/nllb-200-3.3B)")
|
| 16 |
+
|
| 17 |
+
# Input for user's text
|
| 18 |
+
text_input = gr.Textbox(label="Enter Text", placeholder="Type the text you want to translate")
|
| 19 |
+
|
| 20 |
+
# Input for source language
|
| 21 |
+
source_lang = gr.Textbox(label="Source Language", placeholder="e.g., 'eng' for English", value="eng")
|
| 22 |
+
|
| 23 |
+
# Input for target language
|
| 24 |
+
target_lang = gr.Textbox(label="Target Language", placeholder="e.g., 'fra' for French", value="fra")
|
| 25 |
+
|
| 26 |
+
# Output for displaying the translation
|
| 27 |
+
output_text = gr.Textbox(label="Translated Text")
|
| 28 |
+
|
| 29 |
+
# Button to trigger translation
|
| 30 |
+
translate_button = gr.Button("Translate")
|
| 31 |
+
|
| 32 |
+
# Link button click with translation function
|
| 33 |
+
translate_button.click(fn=translate_text, inputs=[text_input, source_lang, target_lang], outputs=output_text)
|
| 34 |
+
|
| 35 |
+
# Launch the Gradio app
|
| 36 |
+
demo.launch()
|