Spaces:
Sleeping
Sleeping
File size: 3,861 Bytes
b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 b776e78 a5eb2b8 | 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import os
import streamlit as st
from typing import List
# Load Hugging Face Marian model (Helsinki-NLP) locally or via API
@st.cache_resource
def load_tokenizer_model(model_name: str):
try:
from transformers import MarianTokenizer, MarianMTModel
except Exception:
st.error("transformers library not installed. Run: pip install transformers sentencepiece")
raise
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
return tokenizer, model
def translate_local(texts: List[str], tokenizer, model) -> List[str]:
batch = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
generated = model.generate(**batch, max_length=512)
return tokenizer.batch_decode(generated, skip_special_tokens=True)
def translate_via_inference_api(text: str, model_name: str, hf_token: str) -> str:
import requests
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
headers = {"Authorization": f"Bearer {hf_token}"}
payload = {"inputs": text}
res = requests.post(API_URL, headers=headers, json=payload, timeout=60)
res.raise_for_status()
data = res.json()
if isinstance(data, dict) and data.get('error'):
raise RuntimeError(data['error'])
if isinstance(data, list) and len(data) > 0:
return data[0].get('translation_text') or data[0].get('generated_text') or str(data[0])
return str(data)
# ---------------- Streamlit UI ----------------
st.set_page_config(page_title="English β Urdu Translator", layout="centered")
st.title("π¬π§ β π΅π° English β Urdu Translator")
st.write("Streamlit app that translates English text to Urdu using Hugging Face models.")
model_name = st.text_input("Model name", value="Helsinki-NLP/opus-mt-en-ur")
use_api = st.checkbox("Use Hugging Face Inference API instead of local model")
hf_token_env = os.environ.get("HF_API_TOKEN", "")
hf_token_input = st.text_input("Hugging Face API token (optional; required only for Inference API)", value=hf_token_env, type="password")
text_in = st.text_area("Input English text", height=200)
# Handle Streamlit version differences
try:
col1, col2 = st.columns([1, 1])
except AttributeError:
col1, col2 = st.beta_columns([1, 1])
with col1:
translate_btn = st.button("Translate")
with col2:
clear_btn = st.button("Clear")
if clear_btn:
st.rerun()
if translate_btn:
if not text_in.strip():
st.warning("Please enter some English text to translate.")
else:
with st.spinner("Translating..."):
try:
if use_api:
hf_token = hf_token_input.strip()
if not hf_token:
st.error("Hugging Face API token required when using Inference API. Set as HF_API_TOKEN env var or paste it here.")
else:
result = translate_via_inference_api(text_in, model_name, hf_token)
st.subheader("Urdu translation")
st.write(result)
else:
tokenizer, model = load_tokenizer_model(model_name)
outputs = translate_local([text_in], tokenizer, model)
st.subheader("Urdu translation")
st.write(outputs[0])
except Exception as e:
st.error(f"Translation failed: {e}")
st.markdown("---")
st.write("**Notes:**")
st.write("- Local model download may take some time on first run and requires internet.\n- To use the Hugging Face Inference API, enable the checkbox and provide your API token from https://huggingface.co/settings/tokens.\n- Install dependencies: `pip install streamlit transformers sentencepiece requests`.")
st.caption("Built with β€οΈ β modify freely for your needs.") |