# Using Public space/ import requests import gradio as gr # Language dropdown: Display label → (from_code, to_code) LANGUAGES = { "English → Afrikaans": ("en", "af"), "English → Xhosa": ("en", "xh"), "English → Zulu": ("en", "zu"), "English → Sesotho": ("en", "st"), "English → Northern Sotho (Sepedi)": ("en", "nso"), "English → Tsonga": ("en", "ts"), "English → Shona": ("en", "sn"), "English → Yoruba": ("en", "yo"), "English → Swahili": ("en", "sw"), } API_URL = "https://sepioo-facebook-translation.hf.space/translate" def translate(input_text, language_label): from_lang, to_lang = LANGUAGES[language_label] payload = { "from_language": from_lang, "to_language": to_lang, "input_text": input_text, } response = requests.post(API_URL, json=payload) if response.status_code != 200: return f"Error {response.status_code}: {response.text}" try: data = response.json() if "translate" in data: return data["translate"] elif "detail" in data: return f"Validation error: {data['detail']}" else: return "Unexpected response format." except Exception as e: return f"Error parsing response: {e}" # Gradio UI translator = gr.Interface( fn=translate, inputs=[ gr.Textbox(label="Input Text", placeholder="Type English text here..."), gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"), ], outputs=gr.Textbox(label="Translation"), title="Translademia", description="Translate English to South African languages", ) translator.launch() # import requests # import gradio as gr # from dotenv import load_dotenv # from transformers import MBart50TokenizerFast # import os # # Load environment variables # load_dotenv() # HF_TOKEN = os.getenv("HF_TOKEN") # headers = {"Authorization": f"Bearer {HF_TOKEN}"} # # Correct mBART-50 language codes # LANGUAGES = { # "English → Afrikaans": "af_ZA", # "English → Xhosa": "xh_ZA", # "English → Zulu": "zu_ZA", # "English → Sesotho": "st_ZA", # Southern Sotho # "English → Tswana": "tn_ZA", # # The following are *not officially* supported by mBART-50 and may raise errors # # You can remove them if not working # # "English → Northern Sotho": "nso_ZA", # # "English → Swati": "ss_ZA", # # "English → Tsonga": "ts_ZA", # # "English → Venda": "ve_ZA", # } # MODEL_NAME = "facebook/mbart-large-50-many-to-many-mmt" # API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}" # # Load tokenizer to get language token IDs # tokenizer = MBart50TokenizerFast.from_pretrained(MODEL_NAME) # def get_token_id(lang_code): # """Return the forced_bos_token_id for the target language.""" # return tokenizer.lang_code_to_id[lang_code] # def query(payload): # """Send the translation request to the Hugging Face API.""" # response = requests.post(API_URL, headers=headers, json=payload) # if response.status_code != 200: # print(f"[ERROR] API failed: {response.status_code} - {response.text}") # return {"error": f"Request failed with {response.status_code}"} # try: # return response.json() # except requests.exceptions.JSONDecodeError: # print(f"[ERROR] Failed to parse JSON: {response.text}") # return {"error": "Invalid JSON from API"} # def translate(input_text, language_label): # """Main translation function.""" # target_lang_code = LANGUAGES[language_label] # token_id = get_token_id(target_lang_code) # payload = { # "inputs": input_text, # "parameters": {"forced_bos_token_id": token_id}, # "options": {"wait_for_model": True}, # } # response = query(payload) # if "error" in response: # return f"Error: {response['error']}" # return response[0]["translation_text"] # # Gradio UI # translator = gr.Interface( # fn=translate, # inputs=[ # gr.Textbox(label="Input Text", placeholder="Type text here..."), # gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"), # ], # outputs=gr.Textbox(label="Translation"), # title="Translademia", # description="Translate English text to South African languages using Meta's mBART-50 model.", # ) # translator.launch()