Spaces:
Running
Running
Commit Β·
4450920
1
Parent(s): 1d35366
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,326 +1,353 @@
|
|
| 1 |
-
# import requests
|
| 2 |
-
# import gradio as gr
|
| 3 |
-
# from dotenv import load_dotenv
|
| 4 |
-
# import os
|
| 5 |
-
|
| 6 |
-
# # Load environment variables from .env file
|
| 7 |
-
# load_dotenv()
|
| 8 |
-
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
# model_name = "Helsinki-NLP/opus-mt-en-nso"
|
| 11 |
-
# API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 12 |
-
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# def query(payload):
|
| 16 |
-
# # HTTP POST Request
|
| 17 |
-
# response = requests.post(API_URL, headers=headers, json=payload)
|
| 18 |
-
# return response.json()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# def translate(input_text):
|
| 22 |
-
# # API Request:
|
| 23 |
-
# response = query({"inputs": input_text, "options": {"wait_for_model": True}})
|
| 24 |
-
|
| 25 |
-
# translation = response[0]["translation_text"]
|
| 26 |
-
|
| 27 |
-
# return translation
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# translator = gr.Interface(
|
| 31 |
-
# fn=translate,
|
| 32 |
-
# inputs=[gr.Textbox(label="Input Text", placeholder="Input Text To Be Translated")],
|
| 33 |
-
# outputs=gr.Textbox(label="Translation"),
|
| 34 |
-
# title="Translademia",
|
| 35 |
-
# )
|
| 36 |
-
|
| 37 |
-
# translator.launch()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# # The one we are going with
|
| 41 |
-
# import requests
|
| 42 |
-
# import gradio as gr
|
| 43 |
-
# from dotenv import load_dotenv
|
| 44 |
-
# import os
|
| 45 |
|
| 46 |
-
# # Load environment variables
|
| 47 |
-
# load_dotenv()
|
| 48 |
-
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 49 |
-
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 50 |
-
|
| 51 |
-
# # Language to ISO 639-3 codes (used for NLLB-200)
|
| 52 |
-
# LANGUAGES = {
|
| 53 |
-
# "English β Afrikaans": "afr",
|
| 54 |
-
# "English β Xhosa": "xho",
|
| 55 |
-
# "English β Zulu": "zul",
|
| 56 |
-
# "English β Sesotho": "sot",
|
| 57 |
-
# "English β Tswana": "tsn",
|
| 58 |
-
# "English β Northern Sotho": "nso",
|
| 59 |
-
# "English β Swati": "ssw",
|
| 60 |
-
# "English β Tsonga": "tso",
|
| 61 |
-
# "English β Venda": "ven",
|
| 62 |
-
# }
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
# API_URL = f"https://api-inference.huggingface.co/models/{
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
-
# def query(payload):
|
| 69 |
-
#
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# if response.status_code != 200:
|
| 72 |
-
# print(f"[ERROR] API failed: {response.status_code} - {response.text}")
|
| 73 |
-
# return {"error": f"Request failed with {response.status_code}"}
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
#
|
| 77 |
-
#
|
| 78 |
-
# print(f"[ERROR] Failed to parse JSON: {response.text}")
|
| 79 |
-
# return {"error": "Invalid JSON from API"}
|
| 80 |
|
|
|
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
# language_code = LANGUAGES[language_label]
|
| 84 |
-
# formatted_input = f">>{language_code}<< {input_text}"
|
| 85 |
|
| 86 |
-
# response = query({"inputs": formatted_input, "options": {"wait_for_model": True}})
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
#
|
| 92 |
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
#
|
| 96 |
-
#
|
| 97 |
-
#
|
| 98 |
-
#
|
| 99 |
-
# ],
|
| 100 |
-
# outputs=gr.Textbox(label="Translation"),
|
| 101 |
-
# title="Translademia",
|
| 102 |
-
# description="Translate English text to South African languages using Meta's NLLB-200 model.",
|
| 103 |
-
# )
|
| 104 |
-
|
| 105 |
-
# translator.launch()
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
import gradio as gr
|
| 113 |
-
from dotenv import load_dotenv
|
| 114 |
|
| 115 |
-
# Load env
|
| 116 |
-
load_dotenv()
|
| 117 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
"English β Xhosa": "xho",
|
| 126 |
-
"English β Zulu": "zul",
|
| 127 |
-
"English β Sesotho": "sot",
|
| 128 |
-
"English β Tswana": "tsn",
|
| 129 |
-
"English β Northern Sotho": "nso",
|
| 130 |
-
"English β Swati": "ssw",
|
| 131 |
-
"English β Tsonga": "tso",
|
| 132 |
-
"English β Venda": "ven",
|
| 133 |
-
}
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
|
| 138 |
-
def translate(input_text
|
| 139 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
-
formatted_input = f">>{lang_code}<< {input_text}"
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
prompt=formatted_input,
|
| 148 |
-
model=MODEL_NAME,
|
| 149 |
-
max_new_tokens=200,
|
| 150 |
-
)
|
| 151 |
-
return response.strip()
|
| 152 |
-
except Exception as e:
|
| 153 |
-
return f"Error: {str(e)}"
|
| 154 |
|
|
|
|
| 155 |
|
| 156 |
-
# Gradio UI
|
| 157 |
-
translator = gr.Interface(
|
| 158 |
-
fn=translate,
|
| 159 |
-
inputs=[
|
| 160 |
-
gr.Textbox(label="Input Text", placeholder="Type English text here..."),
|
| 161 |
-
gr.Dropdown(list(LANGUAGES.keys()), label="Target Language"),
|
| 162 |
-
],
|
| 163 |
-
outputs=gr.Textbox(label="Translation"),
|
| 164 |
-
title="NLLB-200 Translator",
|
| 165 |
-
description="Translate English to South African languages using Meta's NLLB model",
|
| 166 |
-
)
|
| 167 |
|
| 168 |
-
translator.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
|
|
|
| 170 |
|
| 171 |
-
# hate
|
| 172 |
|
|
|
|
| 173 |
|
| 174 |
-
# import
|
|
|
|
| 175 |
# import gradio as gr
|
| 176 |
# from dotenv import load_dotenv
|
| 177 |
-
# import os
|
| 178 |
|
| 179 |
-
# # Load
|
| 180 |
# load_dotenv()
|
| 181 |
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 182 |
-
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 183 |
-
|
| 184 |
-
# # NLLB model name
|
| 185 |
-
# MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 186 |
-
# API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 187 |
-
|
| 188 |
-
# # Define supported language pairs and NLLB codes
|
| 189 |
-
# LANGUAGE_PAIRS = {
|
| 190 |
-
# "English β Afrikaans": ("eng_Latn", "afr_Latn"),
|
| 191 |
-
# "English β Xhosa": ("eng_Latn", "xho_Latn"),
|
| 192 |
-
# "English β Zulu": ("eng_Latn", "zul_Latn"),
|
| 193 |
-
# "English β Sesotho": ("eng_Latn", "sot_Latn"),
|
| 194 |
-
# "English β Tswana": ("eng_Latn", "tsn_Latn"),
|
| 195 |
-
# "English β Northern Sotho": ("eng_Latn", "nso_Latn"),
|
| 196 |
-
# "English β Swati": ("eng_Latn", "ssw_Latn"),
|
| 197 |
-
# "English β Tsonga": ("eng_Latn", "tso_Latn"),
|
| 198 |
-
# "Afrikaans β English": ("afr_Latn", "eng_Latn"),
|
| 199 |
-
# "Xhosa β English": ("xho_Latn", "eng_Latn"),
|
| 200 |
-
# "Zulu β English": ("zul_Latn", "eng_Latn"),
|
| 201 |
-
# "Sesotho β English": ("sot_Latn", "eng_Latn"),
|
| 202 |
-
# "Tswana β English": ("tsn_Latn", "eng_Latn"),
|
| 203 |
-
# "Northern Sotho β English": ("nso_Latn", "eng_Latn"),
|
| 204 |
-
# "Swati β English": ("ssw_Latn", "eng_Latn"),
|
| 205 |
-
# "Tsonga β English": ("tso_Latn", "eng_Latn"),
|
| 206 |
-
# }
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
# src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 211 |
|
| 212 |
-
# payload = {
|
| 213 |
-
# "inputs": input_text,
|
| 214 |
-
# "parameters": {
|
| 215 |
-
# "src_lang": src_lang,
|
| 216 |
-
# "tgt_lang": tgt_lang,
|
| 217 |
-
# },
|
| 218 |
-
# "options": {"wait_for_model": True},
|
| 219 |
-
# }
|
| 220 |
|
| 221 |
-
#
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
#
|
| 224 |
-
#
|
| 225 |
|
| 226 |
# try:
|
| 227 |
-
#
|
| 228 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# except Exception as e:
|
| 230 |
-
# return f"
|
| 231 |
|
| 232 |
|
| 233 |
# # Gradio UI
|
| 234 |
# translator = gr.Interface(
|
| 235 |
# fn=translate,
|
| 236 |
# inputs=[
|
| 237 |
-
# gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 238 |
-
# gr.Dropdown(
|
| 239 |
# ],
|
| 240 |
# outputs=gr.Textbox(label="Translation"),
|
| 241 |
-
# title="
|
| 242 |
-
# description="Translate
|
| 243 |
# )
|
| 244 |
|
| 245 |
-
# translator.launch(
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
# ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 249 |
-
# Using Unesco API
|
| 250 |
-
|
| 251 |
-
# import requests
|
| 252 |
-
# import gradio as gr
|
| 253 |
-
# from dotenv import load_dotenv
|
| 254 |
-
# import os
|
| 255 |
-
|
| 256 |
-
# # Load Hugging Face token from .env
|
| 257 |
-
# load_dotenv()
|
| 258 |
-
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 259 |
-
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 260 |
-
|
| 261 |
-
# # NLLB model endpoint
|
| 262 |
-
# MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 263 |
-
# API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 264 |
-
|
| 265 |
-
# # Define supported language pairs and NLLB codes
|
| 266 |
-
# LANGUAGE_PAIRS = {
|
| 267 |
-
# "English β Afrikaans": ("eng_Latn", "afr_Latn"),
|
| 268 |
-
# "English β Xhosa": ("eng_Latn", "xho_Latn"),
|
| 269 |
-
# "English β Zulu": ("eng_Latn", "zul_Latn"),
|
| 270 |
-
# "English β Sesotho": ("eng_Latn", "sot_Latn"),
|
| 271 |
-
# "English β Tswana": ("eng_Latn", "tsn_Latn"),
|
| 272 |
-
# "English β Northern Sotho": ("eng_Latn", "nso_Latn"),
|
| 273 |
-
# "English β Swati": ("eng_Latn", "ssw_Latn"),
|
| 274 |
-
# "English β Tsonga": ("eng_Latn", "tso_Latn"),
|
| 275 |
-
# "Afrikaans β English": ("afr_Latn", "eng_Latn"),
|
| 276 |
-
# "Xhosa β English": ("xho_Latn", "eng_Latn"),
|
| 277 |
-
# "Zulu β English": ("zul_Latn", "eng_Latn"),
|
| 278 |
-
# "Sesotho β English": ("sot_Latn", "eng_Latn"),
|
| 279 |
-
# "Tswana β English": ("tsn_Latn", "eng_Latn"),
|
| 280 |
-
# "Northern Sotho β English": ("nso_Latn", "eng_Latn"),
|
| 281 |
-
# "Swati β English": ("ssw_Latn", "eng_Latn"),
|
| 282 |
-
# "Tsonga β English": ("tso_Latn", "eng_Latn"),
|
| 283 |
-
# }
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
# def translate(input_text, language_pair):
|
| 287 |
-
# if not input_text.strip():
|
| 288 |
-
# return "[ERROR] Please enter some text to translate."
|
| 289 |
-
|
| 290 |
-
# # Get source and target language codes
|
| 291 |
-
# src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 292 |
-
|
| 293 |
-
# # Prepend target language token to the input
|
| 294 |
-
# formatted_input = f">>{tgt_lang}<< {input_text.strip()}"
|
| 295 |
|
| 296 |
-
# # Send request to Hugging Face Inference API
|
| 297 |
-
# payload = {
|
| 298 |
-
# "inputs": formatted_input,
|
| 299 |
-
# "options": {"wait_for_model": True},
|
| 300 |
-
# }
|
| 301 |
|
| 302 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
# if response.status_code != 200:
|
| 305 |
-
# return f"[ERROR] {response.status_code}: {response.text}"
|
| 306 |
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
# return f"[ERROR] Failed to parse response: {e}"
|
| 312 |
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
-
|
| 315 |
-
# translator = gr.Interface(
|
| 316 |
-
# fn=translate,
|
| 317 |
-
# inputs=[
|
| 318 |
-
# gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 319 |
-
# gr.Dropdown(choices=list(LANGUAGE_PAIRS.keys()), label="Select Language Pair"),
|
| 320 |
-
# ],
|
| 321 |
-
# outputs=gr.Textbox(label="Translation"),
|
| 322 |
-
# title="Translademia (NLLB Edition)",
|
| 323 |
-
# description="Translate between English and South African languages using Meta's NLLB-200 multilingual model.",
|
| 324 |
-
# )
|
| 325 |
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # import requests
|
| 2 |
+
# # import gradio as gr
|
| 3 |
+
# # from dotenv import load_dotenv
|
| 4 |
+
# # import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# # # Load environment variables from .env file
|
| 7 |
+
# # load_dotenv()
|
| 8 |
+
# # HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# # model_name = "Helsinki-NLP/opus-mt-en-nso"
|
| 11 |
+
# # API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 12 |
+
# # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 13 |
|
| 14 |
|
| 15 |
+
# # def query(payload):
|
| 16 |
+
# # # HTTP POST Request
|
| 17 |
+
# # response = requests.post(API_URL, headers=headers, json=payload)
|
| 18 |
+
# # return response.json()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# # def translate(input_text):
|
| 22 |
+
# # # API Request:
|
| 23 |
+
# # response = query({"inputs": input_text, "options": {"wait_for_model": True}})
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# # translation = response[0]["translation_text"]
|
| 26 |
|
| 27 |
+
# # return translation
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
+
# # translator = gr.Interface(
|
| 31 |
+
# # fn=translate,
|
| 32 |
+
# # inputs=[gr.Textbox(label="Input Text", placeholder="Input Text To Be Translated")],
|
| 33 |
+
# # outputs=gr.Textbox(label="Translation"),
|
| 34 |
+
# # title="Translademia",
|
| 35 |
+
# # )
|
| 36 |
|
| 37 |
+
# # translator.launch()
|
| 38 |
|
| 39 |
|
| 40 |
+
# # # The one we are going with
|
| 41 |
+
# # import requests
|
| 42 |
+
# # import gradio as gr
|
| 43 |
+
# # from dotenv import load_dotenv
|
| 44 |
+
# # import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# # # Load environment variables
|
| 47 |
+
# # load_dotenv()
|
| 48 |
+
# # HF_TOKEN = os.getenv("HF_TOKEN")
|
| 49 |
+
# # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 50 |
|
| 51 |
+
# # # Language to ISO 639-3 codes (used for NLLB-200)
|
| 52 |
+
# # LANGUAGES = {
|
| 53 |
+
# # "English β Afrikaans": "afr",
|
| 54 |
+
# # "English β Xhosa": "xho",
|
| 55 |
+
# # "English β Zulu": "zul",
|
| 56 |
+
# # "English β Sesotho": "sot",
|
| 57 |
+
# # "English β Tswana": "tsn",
|
| 58 |
+
# # "English β Northern Sotho": "nso",
|
| 59 |
+
# # "English β Swati": "ssw",
|
| 60 |
+
# # "English β Tsonga": "tso",
|
| 61 |
+
# # "English β Venda": "ven",
|
| 62 |
+
# # }
|
| 63 |
|
| 64 |
+
# # MODEL_NAME = "facebook/nllb-200-distilled-600M"
|
| 65 |
+
# # API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# # def query(payload):
|
| 69 |
+
# # response = requests.post(API_URL, headers=headers, json=payload)
|
| 70 |
|
| 71 |
+
# # if response.status_code != 200:
|
| 72 |
+
# # print(f"[ERROR] API failed: {response.status_code} - {response.text}")
|
| 73 |
+
# # return {"error": f"Request failed with {response.status_code}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# # try:
|
| 76 |
+
# # return response.json()
|
| 77 |
+
# # except requests.exceptions.JSONDecodeError:
|
| 78 |
+
# # print(f"[ERROR] Failed to parse JSON: {response.text}")
|
| 79 |
+
# # return {"error": "Invalid JSON from API"}
|
| 80 |
|
| 81 |
|
| 82 |
+
# # def translate(input_text, language_label):
|
| 83 |
+
# # language_code = LANGUAGES[language_label]
|
| 84 |
+
# # formatted_input = f">>{language_code}<< {input_text}"
|
| 85 |
|
| 86 |
+
# # response = query({"inputs": formatted_input, "options": {"wait_for_model": True}})
|
|
|
|
| 87 |
|
| 88 |
+
# # if "error" in response:
|
| 89 |
+
# # return f"Error: {response['error']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# # return response[0]["translation_text"]
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# # translator = gr.Interface(
|
| 95 |
+
# # fn=translate,
|
| 96 |
+
# # inputs=[
|
| 97 |
+
# # gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 98 |
+
# # gr.Dropdown(list(LANGUAGES.keys()), label="Select Language Target"),
|
| 99 |
+
# # ],
|
| 100 |
+
# # outputs=gr.Textbox(label="Translation"),
|
| 101 |
+
# # title="Translademia",
|
| 102 |
+
# # description="Translate English text to South African languages using Meta's NLLB-200 model.",
|
| 103 |
+
# # )
|
| 104 |
|
| 105 |
+
# # translator.launch()
|
| 106 |
|
|
|
|
| 107 |
|
| 108 |
+
# # love
|
| 109 |
|
| 110 |
+
# import os
|
| 111 |
+
# from huggingface_hub import InferenceClient
|
| 112 |
# import gradio as gr
|
| 113 |
# from dotenv import load_dotenv
|
|
|
|
| 114 |
|
| 115 |
+
# # Load env
|
| 116 |
# load_dotenv()
|
| 117 |
# HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
+
# # Init client
|
| 120 |
+
# client = InferenceClient(token=HF_TOKEN)
|
| 121 |
+
|
| 122 |
+
# # Languages supported
|
| 123 |
+
# LANGUAGES = {
|
| 124 |
+
# "English β Afrikaans": "afr",
|
| 125 |
+
# "English β Xhosa": "xho",
|
| 126 |
+
# "English β Zulu": "zul",
|
| 127 |
+
# "English β Sesotho": "sot",
|
| 128 |
+
# "English β Tswana": "tsn",
|
| 129 |
+
# "English β Northern Sotho": "nso",
|
| 130 |
+
# "English β Swati": "ssw",
|
| 131 |
+
# "English β Tsonga": "tso",
|
| 132 |
+
# "English β Venda": "ven",
|
| 133 |
+
# }
|
| 134 |
|
| 135 |
+
# MODEL_NAME = "facebook/nllb-200-distilled-600M"
|
|
|
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
# def translate(input_text: str, language_label: str) -> str:
|
| 139 |
+
# if not input_text.strip():
|
| 140 |
+
# return "Error: Please enter text to translate."
|
| 141 |
|
| 142 |
+
# lang_code = LANGUAGES[language_label]
|
| 143 |
+
# formatted_input = f">>{lang_code}<< {input_text}"
|
| 144 |
|
| 145 |
# try:
|
| 146 |
+
# response = client.text_generation(
|
| 147 |
+
# prompt=formatted_input,
|
| 148 |
+
# model=MODEL_NAME,
|
| 149 |
+
# max_new_tokens=200,
|
| 150 |
+
# )
|
| 151 |
+
# return response.strip()
|
| 152 |
# except Exception as e:
|
| 153 |
+
# return f"Error: {str(e)}"
|
| 154 |
|
| 155 |
|
| 156 |
# # Gradio UI
|
| 157 |
# translator = gr.Interface(
|
| 158 |
# fn=translate,
|
| 159 |
# inputs=[
|
| 160 |
+
# gr.Textbox(label="Input Text", placeholder="Type English text here..."),
|
| 161 |
+
# gr.Dropdown(list(LANGUAGES.keys()), label="Target Language"),
|
| 162 |
# ],
|
| 163 |
# outputs=gr.Textbox(label="Translation"),
|
| 164 |
+
# title="NLLB-200 Translator",
|
| 165 |
+
# description="Translate English to South African languages using Meta's NLLB model",
|
| 166 |
# )
|
| 167 |
|
| 168 |
+
# translator.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# # hate
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# # import requests
|
| 175 |
+
# # import gradio as gr
|
| 176 |
+
# # from dotenv import load_dotenv
|
| 177 |
+
# # import os
|
| 178 |
+
|
| 179 |
+
# # # Load Hugging Face token from .env
|
| 180 |
+
# # load_dotenv()
|
| 181 |
+
# # HF_TOKEN = os.getenv("HF_TOKEN")
|
| 182 |
+
# # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 183 |
+
|
| 184 |
+
# # # NLLB model name
|
| 185 |
+
# # MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 186 |
+
# # API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 187 |
+
|
| 188 |
+
# # # Define supported language pairs and NLLB codes
|
| 189 |
+
# # LANGUAGE_PAIRS = {
|
| 190 |
+
# # "English β Afrikaans": ("eng_Latn", "afr_Latn"),
|
| 191 |
+
# # "English β Xhosa": ("eng_Latn", "xho_Latn"),
|
| 192 |
+
# # "English β Zulu": ("eng_Latn", "zul_Latn"),
|
| 193 |
+
# # "English β Sesotho": ("eng_Latn", "sot_Latn"),
|
| 194 |
+
# # "English β Tswana": ("eng_Latn", "tsn_Latn"),
|
| 195 |
+
# # "English β Northern Sotho": ("eng_Latn", "nso_Latn"),
|
| 196 |
+
# # "English β Swati": ("eng_Latn", "ssw_Latn"),
|
| 197 |
+
# # "English β Tsonga": ("eng_Latn", "tso_Latn"),
|
| 198 |
+
# # "Afrikaans β English": ("afr_Latn", "eng_Latn"),
|
| 199 |
+
# # "Xhosa β English": ("xho_Latn", "eng_Latn"),
|
| 200 |
+
# # "Zulu β English": ("zul_Latn", "eng_Latn"),
|
| 201 |
+
# # "Sesotho β English": ("sot_Latn", "eng_Latn"),
|
| 202 |
+
# # "Tswana β English": ("tsn_Latn", "eng_Latn"),
|
| 203 |
+
# # "Northern Sotho β English": ("nso_Latn", "eng_Latn"),
|
| 204 |
+
# # "Swati β English": ("ssw_Latn", "eng_Latn"),
|
| 205 |
+
# # "Tsonga β English": ("tso_Latn", "eng_Latn"),
|
| 206 |
+
# # }
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
# # def translate(input_text, language_pair):
|
| 210 |
+
# # src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 211 |
+
|
| 212 |
+
# # payload = {
|
| 213 |
+
# # "inputs": input_text,
|
| 214 |
+
# # "parameters": {
|
| 215 |
+
# # "src_lang": src_lang,
|
| 216 |
+
# # "tgt_lang": tgt_lang,
|
| 217 |
+
# # },
|
| 218 |
+
# # "options": {"wait_for_model": True},
|
| 219 |
+
# # }
|
| 220 |
+
|
| 221 |
+
# # response = requests.post(API_URL, headers=headers, json=payload)
|
| 222 |
+
|
| 223 |
+
# # if response.status_code != 200:
|
| 224 |
+
# # return f"[ERROR] {response.status_code}: {response.text}"
|
| 225 |
+
|
| 226 |
+
# # try:
|
| 227 |
+
# # output = response.json()
|
| 228 |
+
# # return output[0]["translation_text"]
|
| 229 |
+
# # except Exception as e:
|
| 230 |
+
# # return f"[ERROR] Failed to parse response: {e}"
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
# # # Gradio UI
|
| 234 |
+
# # translator = gr.Interface(
|
| 235 |
+
# # fn=translate,
|
| 236 |
+
# # inputs=[
|
| 237 |
+
# # gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 238 |
+
# # gr.Dropdown(choices=list(LANGUAGE_PAIRS.keys()), label="Select Language Pair"),
|
| 239 |
+
# # ],
|
| 240 |
+
# # outputs=gr.Textbox(label="Translation"),
|
| 241 |
+
# # title="Translademia (NLLB Edition)",
|
| 242 |
+
# # description="Translate between English and official South African languages using Meta's NLLB-200 model.",
|
| 243 |
+
# # )
|
| 244 |
+
|
| 245 |
+
# # translator.launch(share=True)
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
# # ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 249 |
+
# # Using Unesco API
|
| 250 |
+
|
| 251 |
+
# # import requests
|
| 252 |
+
# # import gradio as gr
|
| 253 |
+
# # from dotenv import load_dotenv
|
| 254 |
+
# # import os
|
| 255 |
+
|
| 256 |
+
# # # Load Hugging Face token from .env
|
| 257 |
+
# # load_dotenv()
|
| 258 |
+
# # HF_TOKEN = os.getenv("HF_TOKEN")
|
| 259 |
+
# # headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 260 |
+
|
| 261 |
+
# # # NLLB model endpoint
|
| 262 |
+
# # MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 263 |
+
# # API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 264 |
+
|
| 265 |
+
# # # Define supported language pairs and NLLB codes
|
| 266 |
+
# # LANGUAGE_PAIRS = {
|
| 267 |
+
# # "English β Afrikaans": ("eng_Latn", "afr_Latn"),
|
| 268 |
+
# # "English β Xhosa": ("eng_Latn", "xho_Latn"),
|
| 269 |
+
# # "English β Zulu": ("eng_Latn", "zul_Latn"),
|
| 270 |
+
# # "English β Sesotho": ("eng_Latn", "sot_Latn"),
|
| 271 |
+
# # "English β Tswana": ("eng_Latn", "tsn_Latn"),
|
| 272 |
+
# # "English β Northern Sotho": ("eng_Latn", "nso_Latn"),
|
| 273 |
+
# # "English β Swati": ("eng_Latn", "ssw_Latn"),
|
| 274 |
+
# # "English β Tsonga": ("eng_Latn", "tso_Latn"),
|
| 275 |
+
# # "Afrikaans β English": ("afr_Latn", "eng_Latn"),
|
| 276 |
+
# # "Xhosa β English": ("xho_Latn", "eng_Latn"),
|
| 277 |
+
# # "Zulu β English": ("zul_Latn", "eng_Latn"),
|
| 278 |
+
# # "Sesotho β English": ("sot_Latn", "eng_Latn"),
|
| 279 |
+
# # "Tswana β English": ("tsn_Latn", "eng_Latn"),
|
| 280 |
+
# # "Northern Sotho β English": ("nso_Latn", "eng_Latn"),
|
| 281 |
+
# # "Swati β English": ("ssw_Latn", "eng_Latn"),
|
| 282 |
+
# # "Tsonga β English": ("tso_Latn", "eng_Latn"),
|
| 283 |
+
# # }
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
# # def translate(input_text, language_pair):
|
| 287 |
+
# # if not input_text.strip():
|
| 288 |
+
# # return "[ERROR] Please enter some text to translate."
|
| 289 |
+
|
| 290 |
+
# # # Get source and target language codes
|
| 291 |
+
# # src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 292 |
+
|
| 293 |
+
# # # Prepend target language token to the input
|
| 294 |
+
# # formatted_input = f">>{tgt_lang}<< {input_text.strip()}"
|
| 295 |
+
|
| 296 |
+
# # # Send request to Hugging Face Inference API
|
| 297 |
+
# # payload = {
|
| 298 |
+
# # "inputs": formatted_input,
|
| 299 |
+
# # "options": {"wait_for_model": True},
|
| 300 |
+
# # }
|
| 301 |
+
|
| 302 |
+
# # response = requests.post(API_URL, headers=headers, json=payload)
|
| 303 |
+
|
| 304 |
+
# # if response.status_code != 200:
|
| 305 |
+
# # return f"[ERROR] {response.status_code}: {response.text}"
|
| 306 |
+
|
| 307 |
+
# # try:
|
| 308 |
+
# # output = response.json()
|
| 309 |
+
# # return output[0]["translation_text"]
|
| 310 |
+
# # except Exception as e:
|
| 311 |
+
# # return f"[ERROR] Failed to parse response: {e}"
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
# # # Gradio UI
|
| 315 |
+
# # translator = gr.Interface(
|
| 316 |
+
# # fn=translate,
|
| 317 |
+
# # inputs=[
|
| 318 |
+
# # gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 319 |
+
# # gr.Dropdown(choices=list(LANGUAGE_PAIRS.keys()), label="Select Language Pair"),
|
| 320 |
+
# # ],
|
| 321 |
+
# # outputs=gr.Textbox(label="Translation"),
|
| 322 |
+
# # title="Translademia (NLLB Edition)",
|
| 323 |
+
# # description="Translate between English and South African languages using Meta's NLLB-200 multilingual model.",
|
| 324 |
+
# # )
|
| 325 |
+
|
| 326 |
+
# # translator.launch(share=True)
|
| 327 |
|
|
|
|
|
|
|
| 328 |
|
| 329 |
+
import os
|
| 330 |
+
from huggingface_hub import InferenceClient
|
| 331 |
+
from dotenv import load_dotenv
|
| 332 |
+
import traceback
|
|
|
|
| 333 |
|
| 334 |
+
# Load token
|
| 335 |
+
load_dotenv()
|
| 336 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 337 |
|
| 338 |
+
client = InferenceClient(token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
+
try:
|
| 341 |
+
input_text = "Good morning, how are you?"
|
| 342 |
+
lang_code = "zul"
|
| 343 |
+
prompt = f">>{lang_code}<< {input_text}"
|
| 344 |
+
|
| 345 |
+
result = client.text_generation(
|
| 346 |
+
prompt=prompt,
|
| 347 |
+
model="facebook/nllb-200-distilled-600M",
|
| 348 |
+
max_new_tokens=200,
|
| 349 |
+
timeout=60, # add this
|
| 350 |
+
)
|
| 351 |
+
print("β
Result:", result)
|
| 352 |
+
except Exception as e:
|
| 353 |
+
print("β Error:\n", traceback.format_exc())
|