Spaces:
Running
Running
File size: 4,420 Bytes
de1c759 76f85b9 1cd99d0 76f85b9 de1c759 bb88812 de1c759 bb88812 76f85b9 8f8dd51 de1c759 ebbe17f de1c759 1cd99d0 ebbe17f 1cd99d0 8f8dd51 de1c759 8f8dd51 76f85b9 1cd99d0 8f8dd51 76f85b9 bb88812 1cd99d0 bb88812 de1c759 8f8dd51 1387c21 ebbe17f 76f85b9 cc13458 de1c759 76f85b9 cc13458 76f85b9 2c4fc66 cc13458 76f85b9 de1c759 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# 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()
|