Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +18 -5
- app.py +71 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,25 @@
|
|
| 1 |
---
|
| 2 |
title: Instant Translator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.0.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Instant Translator
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "6.0.2"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
## π Instant Translator
|
| 14 |
+
|
| 15 |
+
Translate text between languages using MarianMT via the HuggingFace Inference API.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- 6 language pairs (EN β FR/ES/DE)
|
| 20 |
+
- Real-time translation
|
| 21 |
+
- No model downloads - uses API
|
| 22 |
+
|
| 23 |
+
## Setup
|
| 24 |
+
|
| 25 |
+
Add your `HF_TOKEN` as a Secret in Space Settings.
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Get token from environment (set in HF Space secrets)
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 7 |
+
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
| 8 |
+
|
| 9 |
+
# Language pairs with their MarianMT models
|
| 10 |
+
LANGUAGE_PAIRS = {
|
| 11 |
+
"English β French": "Helsinki-NLP/opus-mt-en-fr",
|
| 12 |
+
"English β Spanish": "Helsinki-NLP/opus-mt-en-es",
|
| 13 |
+
"English β German": "Helsinki-NLP/opus-mt-en-de",
|
| 14 |
+
"French β English": "Helsinki-NLP/opus-mt-fr-en",
|
| 15 |
+
"Spanish β English": "Helsinki-NLP/opus-mt-es-en",
|
| 16 |
+
"German β English": "Helsinki-NLP/opus-mt-de-en",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def translate(text: str, language_pair: str) -> str:
|
| 21 |
+
"""Translate text using selected language pair."""
|
| 22 |
+
if not text.strip():
|
| 23 |
+
return "π Enter text to translate!"
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
model = LANGUAGE_PAIRS[language_pair]
|
| 27 |
+
result = client.translation(text, model=model)
|
| 28 |
+
return result.translation_text
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"β Error: {e}"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
with gr.Blocks(title="Instant Translator") as demo:
|
| 34 |
+
gr.Markdown("# π Instant Translator\nTranslate text between languages instantly!")
|
| 35 |
+
|
| 36 |
+
with gr.Row(equal_height=True):
|
| 37 |
+
with gr.Column():
|
| 38 |
+
input_text = gr.Textbox(
|
| 39 |
+
label="Source text",
|
| 40 |
+
placeholder="Hello, how are you today?",
|
| 41 |
+
lines=4,
|
| 42 |
+
autofocus=True,
|
| 43 |
+
)
|
| 44 |
+
language_pair = gr.Dropdown(
|
| 45 |
+
choices=list(LANGUAGE_PAIRS.keys()),
|
| 46 |
+
value="English β French",
|
| 47 |
+
label="Language pair",
|
| 48 |
+
)
|
| 49 |
+
btn = gr.Button("Translate π", variant="primary")
|
| 50 |
+
|
| 51 |
+
with gr.Column():
|
| 52 |
+
output_text = gr.Textbox(
|
| 53 |
+
label="Translation",
|
| 54 |
+
lines=4,
|
| 55 |
+
interactive=False,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
btn.click(translate, inputs=[input_text, language_pair], outputs=output_text)
|
| 59 |
+
input_text.submit(translate, inputs=[input_text, language_pair], outputs=output_text)
|
| 60 |
+
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=[
|
| 63 |
+
["Hello, how are you today?", "English β French"],
|
| 64 |
+
["Machine learning is fascinating.", "English β Spanish"],
|
| 65 |
+
["I love programming with Python.", "English β German"],
|
| 66 |
+
],
|
| 67 |
+
inputs=[input_text, language_pair],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
demo.queue()
|
| 71 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0.0
|
| 2 |
+
huggingface_hub>=0.23.0
|
| 3 |
+
|