Spaces:
Running
Running
Commit
Β·
de1c759
1
Parent(s):
ebbe17f
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,136 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from transformers import MBart50TokenizerFast
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
load_dotenv()
|
| 9 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 11 |
-
|
| 12 |
-
# Correct mBART-50 language codes
|
| 13 |
LANGUAGES = {
|
| 14 |
-
"English β Afrikaans": "
|
| 15 |
-
"English β Xhosa": "
|
| 16 |
-
"English β Zulu": "
|
| 17 |
-
"English β Sesotho": "
|
| 18 |
-
"English β Tswana": "
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# "English β Tsonga": "ts_ZA",
|
| 24 |
-
# "English β Venda": "ve_ZA",
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
| 28 |
-
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 29 |
-
|
| 30 |
-
# Load tokenizer to get language token IDs
|
| 31 |
-
tokenizer = MBart50TokenizerFast.from_pretrained(MODEL_NAME)
|
| 32 |
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
return tokenizer.lang_code_to_id[lang_code]
|
| 37 |
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
"""Send the translation request to the Hugging Face API."""
|
| 41 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 42 |
|
| 43 |
if response.status_code != 200:
|
| 44 |
-
|
| 45 |
-
return {"error": f"Request failed with {response.status_code}"}
|
| 46 |
|
| 47 |
try:
|
| 48 |
-
return response.json()
|
| 49 |
-
except
|
| 50 |
-
|
| 51 |
-
return {"error": "Invalid JSON from API"}
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def translate(input_text, language_label):
|
| 55 |
-
"""Main translation function."""
|
| 56 |
-
target_lang_code = LANGUAGES[language_label]
|
| 57 |
-
token_id = get_token_id(target_lang_code)
|
| 58 |
-
|
| 59 |
-
payload = {
|
| 60 |
-
"inputs": input_text,
|
| 61 |
-
"parameters": {"forced_bos_token_id": token_id},
|
| 62 |
-
"options": {"wait_for_model": True},
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
response = query(payload)
|
| 66 |
-
|
| 67 |
-
if "error" in response:
|
| 68 |
-
return f"Error: {response['error']}"
|
| 69 |
-
|
| 70 |
-
return response[0]["translation_text"]
|
| 71 |
|
| 72 |
|
| 73 |
# Gradio UI
|
| 74 |
translator = gr.Interface(
|
| 75 |
fn=translate,
|
| 76 |
inputs=[
|
| 77 |
-
gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 78 |
gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
|
| 79 |
],
|
| 80 |
outputs=gr.Textbox(label="Translation"),
|
| 81 |
-
title="Translademia",
|
| 82 |
-
description="Translate English
|
| 83 |
)
|
| 84 |
|
| 85 |
translator.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using Public space/
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Supported target languages and their ISO 639-1 codes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
LANGUAGES = {
|
| 7 |
+
"English β Afrikaans": ("en", "af"),
|
| 8 |
+
"English β Xhosa": ("en", "xh"),
|
| 9 |
+
"English β Zulu": ("en", "zu"),
|
| 10 |
+
"English β Sesotho": ("en", "st"),
|
| 11 |
+
"English β Tswana": ("en", "tn"),
|
| 12 |
+
"English β Northern Sotho": ("en", "nso"),
|
| 13 |
+
"English β Swati": ("en", "ss"),
|
| 14 |
+
"English β Tsonga": ("en", "ts"),
|
| 15 |
+
"English β Venda": ("en", "ve"),
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
API_URL = "https://sepioo-facebook-translation.hf.space/translate"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
+
def translate(input_text, language_label):
|
| 22 |
+
source_lang, target_lang = LANGUAGES[language_label]
|
|
|
|
| 23 |
|
| 24 |
+
payload = {"source": source_lang, "target": target_lang, "text": input_text}
|
| 25 |
|
| 26 |
+
response = requests.post(API_URL, json=payload)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if response.status_code != 200:
|
| 29 |
+
return f"Error: {response.status_code} - {response.text}"
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
return response.json()["translation"]
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error parsing response: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# Gradio UI
|
| 38 |
translator = gr.Interface(
|
| 39 |
fn=translate,
|
| 40 |
inputs=[
|
| 41 |
+
gr.Textbox(label="Input Text", placeholder="Type English text here..."),
|
| 42 |
gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
|
| 43 |
],
|
| 44 |
outputs=gr.Textbox(label="Translation"),
|
| 45 |
+
title="Translademia (Community-Powered)",
|
| 46 |
+
description="Translate English to South African languages using a public Hugging Face Space backend (no token needed).",
|
| 47 |
)
|
| 48 |
|
| 49 |
translator.launch()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# import requests
|
| 53 |
+
# import gradio as gr
|
| 54 |
+
# from dotenv import load_dotenv
|
| 55 |
+
# from transformers import MBart50TokenizerFast
|
| 56 |
+
# import os
|
| 57 |
+
|
| 58 |
+
# # Load environment variables
|
| 59 |
+
# load_dotenv()
|
| 60 |
+
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 61 |
+
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 62 |
+
|
| 63 |
+
# # Correct mBART-50 language codes
|
| 64 |
+
# LANGUAGES = {
|
| 65 |
+
# "English β Afrikaans": "af_ZA",
|
| 66 |
+
# "English β Xhosa": "xh_ZA",
|
| 67 |
+
# "English β Zulu": "zu_ZA",
|
| 68 |
+
# "English β Sesotho": "st_ZA", # Southern Sotho
|
| 69 |
+
# "English β Tswana": "tn_ZA",
|
| 70 |
+
# # The following are *not officially* supported by mBART-50 and may raise errors
|
| 71 |
+
# # You can remove them if not working
|
| 72 |
+
# # "English β Northern Sotho": "nso_ZA",
|
| 73 |
+
# # "English β Swati": "ss_ZA",
|
| 74 |
+
# # "English β Tsonga": "ts_ZA",
|
| 75 |
+
# # "English β Venda": "ve_ZA",
|
| 76 |
+
# }
|
| 77 |
+
|
| 78 |
+
# MODEL_NAME = "facebook/mbart-large-50-many-to-many-mmt"
|
| 79 |
+
# API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 80 |
+
|
| 81 |
+
# # Load tokenizer to get language token IDs
|
| 82 |
+
# tokenizer = MBart50TokenizerFast.from_pretrained(MODEL_NAME)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# def get_token_id(lang_code):
|
| 86 |
+
# """Return the forced_bos_token_id for the target language."""
|
| 87 |
+
# return tokenizer.lang_code_to_id[lang_code]
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# def query(payload):
|
| 91 |
+
# """Send the translation request to the Hugging Face API."""
|
| 92 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
| 93 |
+
|
| 94 |
+
# if response.status_code != 200:
|
| 95 |
+
# print(f"[ERROR] API failed: {response.status_code} - {response.text}")
|
| 96 |
+
# return {"error": f"Request failed with {response.status_code}"}
|
| 97 |
+
|
| 98 |
+
# try:
|
| 99 |
+
# return response.json()
|
| 100 |
+
# except requests.exceptions.JSONDecodeError:
|
| 101 |
+
# print(f"[ERROR] Failed to parse JSON: {response.text}")
|
| 102 |
+
# return {"error": "Invalid JSON from API"}
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# def translate(input_text, language_label):
|
| 106 |
+
# """Main translation function."""
|
| 107 |
+
# target_lang_code = LANGUAGES[language_label]
|
| 108 |
+
# token_id = get_token_id(target_lang_code)
|
| 109 |
+
|
| 110 |
+
# payload = {
|
| 111 |
+
# "inputs": input_text,
|
| 112 |
+
# "parameters": {"forced_bos_token_id": token_id},
|
| 113 |
+
# "options": {"wait_for_model": True},
|
| 114 |
+
# }
|
| 115 |
+
|
| 116 |
+
# response = query(payload)
|
| 117 |
+
|
| 118 |
+
# if "error" in response:
|
| 119 |
+
# return f"Error: {response['error']}"
|
| 120 |
+
|
| 121 |
+
# return response[0]["translation_text"]
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# # Gradio UI
|
| 125 |
+
# translator = gr.Interface(
|
| 126 |
+
# fn=translate,
|
| 127 |
+
# inputs=[
|
| 128 |
+
# gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 129 |
+
# gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
|
| 130 |
+
# ],
|
| 131 |
+
# outputs=gr.Textbox(label="Translation"),
|
| 132 |
+
# title="Translademia",
|
| 133 |
+
# description="Translate English text to South African languages using Meta's mBART-50 model.",
|
| 134 |
+
# )
|
| 135 |
+
|
| 136 |
+
# translator.launch()
|