File size: 916 Bytes
e873920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# app.py
import gradio as gr
from transformers import pipeline

# 1. Load a translation pipeline.
#    By default this is English → French; swap model for other language pairs.
translator = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr")

def translate(text: str) -> str:
    """
    Translate input English text into French.
    """
    # The pipeline returns a list of dicts; we grab the 'translation_text'
    result = translator(text, max_length=512)
    return result[0]["translation_text"]

# 2. Create the Gradio interface.
iface = gr.Interface(
    fn=translate,
    inputs=gr.Textbox(lines=5, label="Input Text (English)"),
    outputs=gr.Textbox(lines=5, label="Translated Text (French)"),
    title="HuggingFace Translation Service",
    description="Enter English text and get a French translation powered by a Hugging Face model."
)

if __name__ == "__main__":
    iface.launch()