Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# --- Model Loading (Replace with your desired model) ---
|
| 5 |
+
# EXAMPLE: French to English. Choose a model appropriate for your languages!
|
| 6 |
+
model_name = "Helsinki-NLP/opus-mt-fr-en"
|
| 7 |
+
model_name = "Helsinki-NLP/opus-mt-de-en"
|
| 8 |
+
|
| 9 |
+
translator = pipeline("translation", model=model_name)
|
| 10 |
+
|
| 11 |
+
# --- Translation Function ---
|
| 12 |
+
def translate_text(text_to_translate):
|
| 13 |
+
try:
|
| 14 |
+
translated_text = translator(text_to_translate)[0]['translation_text']
|
| 15 |
+
return translated_text
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Translation Error: {e}" # Basic error handling
|
| 18 |
+
|
| 19 |
+
# --- Gradio Interface ---
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=translate_text,
|
| 22 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text to translate here..."), # Input textbox
|
| 23 |
+
outputs=gr.Textbox(lines=5, label="Translated Text"), # Output textbox
|
| 24 |
+
title="Old Text Translator",
|
| 25 |
+
description="Translate text using a Hugging Face model. (MVP)",
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# --- Launch the App ---
|
| 29 |
+
iface.launch()
|