Spaces:
Running
Running
Commit Β·
aae9e98
1
Parent(s): ec827d2
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,16 +37,84 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# Language to ISO 639-3 codes (used for NLLB-200)
|
| 52 |
LANGUAGES = {
|
|
@@ -62,30 +130,25 @@ LANGUAGES = {
|
|
| 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 |
-
if response.status_code != 200:
|
| 71 |
-
print(f"[ERROR] API failed: {response.status_code} - {response.text}")
|
| 72 |
-
return {"error": f"Request failed with {response.status_code}"}
|
| 73 |
-
try:
|
| 74 |
-
return response.json()
|
| 75 |
-
except requests.exceptions.JSONDecodeError:
|
| 76 |
-
print(f"[ERROR] Failed to parse JSON: {response.text}")
|
| 77 |
-
return {"error": "Invalid JSON from API"}
|
| 78 |
|
| 79 |
|
| 80 |
def translate(input_text, language_label):
|
| 81 |
language_code = LANGUAGES[language_label]
|
| 82 |
formatted_input = f">>{language_code}<< {input_text}"
|
| 83 |
-
response = query({"inputs": formatted_input, "options": {"wait_for_model": True}})
|
| 84 |
-
if "error" in response:
|
| 85 |
-
return f"Error: {response['error']}"
|
| 86 |
-
return response[0]["translation_text"]
|
| 87 |
-
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
translator = gr.Interface(
|
| 90 |
fn=translate,
|
| 91 |
inputs=[
|
|
|
|
| 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 |
+
import os
|
| 108 |
+
from huggingface_hub import InferenceClient
|
| 109 |
import gradio as gr
|
| 110 |
from dotenv import load_dotenv
|
|
|
|
| 111 |
|
| 112 |
# Load environment variables
|
| 113 |
load_dotenv()
|
| 114 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 115 |
+
|
| 116 |
+
# Initialize InferenceClient with your token
|
| 117 |
+
client = InferenceClient(token=HF_TOKEN)
|
| 118 |
|
| 119 |
# Language to ISO 639-3 codes (used for NLLB-200)
|
| 120 |
LANGUAGES = {
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
MODEL_NAME = "facebook/nllb-200-distilled-600M"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
def translate(input_text, language_label):
|
| 136 |
language_code = LANGUAGES[language_label]
|
| 137 |
formatted_input = f">>{language_code}<< {input_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
try:
|
| 140 |
+
response = client.text_generation(
|
| 141 |
+
prompt=formatted_input,
|
| 142 |
+
model=MODEL_NAME,
|
| 143 |
+
max_new_tokens=500, # Adjust based on expected translation length
|
| 144 |
+
wait_for_model=True, # Avoids cold-start issues (Pro users skip queues)
|
| 145 |
+
)
|
| 146 |
+
return response.strip()
|
| 147 |
+
except Exception as e:
|
| 148 |
+
return f"Error: {str(e)}"
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# Gradio Interface
|
| 152 |
translator = gr.Interface(
|
| 153 |
fn=translate,
|
| 154 |
inputs=[
|