Spaces:
Sleeping
Sleeping
Commit
Β·
2f1eb40
1
Parent(s):
1748350
Update app.py
Browse files
app.py
CHANGED
|
@@ -105,6 +105,83 @@
|
|
| 105 |
# translator.launch()
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
import requests
|
| 109 |
import gradio as gr
|
| 110 |
from dotenv import load_dotenv
|
|
@@ -115,7 +192,7 @@ load_dotenv()
|
|
| 115 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 116 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 117 |
|
| 118 |
-
# NLLB model
|
| 119 |
MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 120 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 121 |
|
|
@@ -141,14 +218,18 @@ LANGUAGE_PAIRS = {
|
|
| 141 |
|
| 142 |
|
| 143 |
def translate(input_text, language_pair):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
payload = {
|
| 147 |
-
"inputs":
|
| 148 |
-
"parameters": {
|
| 149 |
-
"src_lang": src_lang,
|
| 150 |
-
"tgt_lang": tgt_lang,
|
| 151 |
-
},
|
| 152 |
"options": {"wait_for_model": True},
|
| 153 |
}
|
| 154 |
|
|
@@ -173,7 +254,7 @@ translator = gr.Interface(
|
|
| 173 |
],
|
| 174 |
outputs=gr.Textbox(label="Translation"),
|
| 175 |
title="Translademia (NLLB Edition)",
|
| 176 |
-
description="Translate between English and
|
| 177 |
)
|
| 178 |
|
| 179 |
translator.launch(share=True)
|
|
|
|
| 105 |
# translator.launch()
|
| 106 |
|
| 107 |
|
| 108 |
+
# import requests
|
| 109 |
+
# import gradio as gr
|
| 110 |
+
# from dotenv import load_dotenv
|
| 111 |
+
# import os
|
| 112 |
+
|
| 113 |
+
# # Load Hugging Face token from .env
|
| 114 |
+
# load_dotenv()
|
| 115 |
+
# HF_TOKEN = os.getenv("HF_TOKEN")
|
| 116 |
+
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 117 |
+
|
| 118 |
+
# # NLLB model name
|
| 119 |
+
# MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 120 |
+
# API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 121 |
+
|
| 122 |
+
# # Define supported language pairs and NLLB codes
|
| 123 |
+
# LANGUAGE_PAIRS = {
|
| 124 |
+
# "English β Afrikaans": ("eng_Latn", "afr_Latn"),
|
| 125 |
+
# "English β Xhosa": ("eng_Latn", "xho_Latn"),
|
| 126 |
+
# "English β Zulu": ("eng_Latn", "zul_Latn"),
|
| 127 |
+
# "English β Sesotho": ("eng_Latn", "sot_Latn"),
|
| 128 |
+
# "English β Tswana": ("eng_Latn", "tsn_Latn"),
|
| 129 |
+
# "English β Northern Sotho": ("eng_Latn", "nso_Latn"),
|
| 130 |
+
# "English β Swati": ("eng_Latn", "ssw_Latn"),
|
| 131 |
+
# "English β Tsonga": ("eng_Latn", "tso_Latn"),
|
| 132 |
+
# "Afrikaans β English": ("afr_Latn", "eng_Latn"),
|
| 133 |
+
# "Xhosa β English": ("xho_Latn", "eng_Latn"),
|
| 134 |
+
# "Zulu β English": ("zul_Latn", "eng_Latn"),
|
| 135 |
+
# "Sesotho β English": ("sot_Latn", "eng_Latn"),
|
| 136 |
+
# "Tswana β English": ("tsn_Latn", "eng_Latn"),
|
| 137 |
+
# "Northern Sotho β English": ("nso_Latn", "eng_Latn"),
|
| 138 |
+
# "Swati β English": ("ssw_Latn", "eng_Latn"),
|
| 139 |
+
# "Tsonga β English": ("tso_Latn", "eng_Latn"),
|
| 140 |
+
# }
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# def translate(input_text, language_pair):
|
| 144 |
+
# src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 145 |
+
|
| 146 |
+
# payload = {
|
| 147 |
+
# "inputs": input_text,
|
| 148 |
+
# "parameters": {
|
| 149 |
+
# "src_lang": src_lang,
|
| 150 |
+
# "tgt_lang": tgt_lang,
|
| 151 |
+
# },
|
| 152 |
+
# "options": {"wait_for_model": True},
|
| 153 |
+
# }
|
| 154 |
+
|
| 155 |
+
# response = requests.post(API_URL, headers=headers, json=payload)
|
| 156 |
+
|
| 157 |
+
# if response.status_code != 200:
|
| 158 |
+
# return f"[ERROR] {response.status_code}: {response.text}"
|
| 159 |
+
|
| 160 |
+
# try:
|
| 161 |
+
# output = response.json()
|
| 162 |
+
# return output[0]["translation_text"]
|
| 163 |
+
# except Exception as e:
|
| 164 |
+
# return f"[ERROR] Failed to parse response: {e}"
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# # Gradio UI
|
| 168 |
+
# translator = gr.Interface(
|
| 169 |
+
# fn=translate,
|
| 170 |
+
# inputs=[
|
| 171 |
+
# gr.Textbox(label="Input Text", placeholder="Type text here..."),
|
| 172 |
+
# gr.Dropdown(choices=list(LANGUAGE_PAIRS.keys()), label="Select Language Pair"),
|
| 173 |
+
# ],
|
| 174 |
+
# outputs=gr.Textbox(label="Translation"),
|
| 175 |
+
# title="Translademia (NLLB Edition)",
|
| 176 |
+
# description="Translate between English and official South African languages using Meta's NLLB-200 model.",
|
| 177 |
+
# )
|
| 178 |
+
|
| 179 |
+
# translator.launch(share=True)
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
# ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
| 183 |
+
# Using Unesco API
|
| 184 |
+
|
| 185 |
import requests
|
| 186 |
import gradio as gr
|
| 187 |
from dotenv import load_dotenv
|
|
|
|
| 192 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 193 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 194 |
|
| 195 |
+
# NLLB model endpoint
|
| 196 |
MODEL_NAME = "facebook/nllb-200-3.3B"
|
| 197 |
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 198 |
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
def translate(input_text, language_pair):
|
| 221 |
+
if not input_text.strip():
|
| 222 |
+
return "[ERROR] Please enter some text to translate."
|
| 223 |
+
|
| 224 |
+
# Get source and target language codes
|
| 225 |
src_lang, tgt_lang = LANGUAGE_PAIRS[language_pair]
|
| 226 |
|
| 227 |
+
# Prepend target language token to the input
|
| 228 |
+
formatted_input = f">>{tgt_lang}<< {input_text.strip()}"
|
| 229 |
+
|
| 230 |
+
# Send request to Hugging Face Inference API
|
| 231 |
payload = {
|
| 232 |
+
"inputs": formatted_input,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
"options": {"wait_for_model": True},
|
| 234 |
}
|
| 235 |
|
|
|
|
| 254 |
],
|
| 255 |
outputs=gr.Textbox(label="Translation"),
|
| 256 |
title="Translademia (NLLB Edition)",
|
| 257 |
+
description="Translate between English and South African languages using Meta's NLLB-200 multilingual model.",
|
| 258 |
)
|
| 259 |
|
| 260 |
translator.launch(share=True)
|