Spaces:
Sleeping
Sleeping
Commit ·
d24d0fc
1
Parent(s): 68cf2ab
dhhhd
Browse files- .gradio/flagged/dataset3.csv +1 -0
- app.py +25 -9
.gradio/flagged/dataset3.csv
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
Input Text,Translation,timestamp
|
| 2 |
Thobela,,2025-07-27 21:39:41.511509
|
|
|
|
|
|
| 1 |
Input Text,Translation,timestamp
|
| 2 |
Thobela,,2025-07-27 21:39:41.511509
|
| 3 |
+
Hello,,2025-07-28 00:30:59.744082
|
app.py
CHANGED
|
@@ -53,30 +53,46 @@ import gradio as gr
|
|
| 53 |
from dotenv import load_dotenv
|
| 54 |
import os
|
| 55 |
|
| 56 |
-
# Load
|
| 57 |
load_dotenv()
|
| 58 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 59 |
|
| 60 |
model_name = "Helsinki-NLP/opus-mt-en-nso"
|
| 61 |
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 62 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 63 |
|
| 64 |
|
| 65 |
def query(payload):
|
| 66 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def translate(input_text):
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
response = query({"inputs": input_text, "options": {"wait_for_model": True}})
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
translator = gr.Interface(
|
|
|
|
| 53 |
from dotenv import load_dotenv
|
| 54 |
import os
|
| 55 |
|
| 56 |
+
# Load env
|
| 57 |
load_dotenv()
|
| 58 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 59 |
|
| 60 |
model_name = "Helsinki-NLP/opus-mt-en-nso"
|
| 61 |
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 62 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}", "Accept": "application/json"}
|
| 63 |
|
| 64 |
|
| 65 |
def query(payload):
|
| 66 |
response = requests.post(API_URL, headers=headers, json=payload)
|
| 67 |
+
|
| 68 |
+
if response.status_code == 200:
|
| 69 |
+
try:
|
| 70 |
+
return response.json()
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return {"error": f"JSON decode error: {str(e)}"}
|
| 73 |
+
else:
|
| 74 |
+
# Show error message from HF API or HTTP code
|
| 75 |
+
try:
|
| 76 |
+
err = response.json()
|
| 77 |
+
except:
|
| 78 |
+
err = response.text
|
| 79 |
+
return {"error": f"HTTP {response.status_code}: {err}"}
|
| 80 |
|
| 81 |
|
| 82 |
def translate(input_text):
|
| 83 |
+
if not input_text.strip():
|
| 84 |
+
return "Please enter text to translate."
|
| 85 |
+
|
| 86 |
response = query({"inputs": input_text, "options": {"wait_for_model": True}})
|
| 87 |
|
| 88 |
+
if "error" in response:
|
| 89 |
+
return f"Error: {response['error']}"
|
| 90 |
|
| 91 |
+
try:
|
| 92 |
+
translation = response[0]["translation_text"]
|
| 93 |
+
return translation
|
| 94 |
+
except (KeyError, IndexError):
|
| 95 |
+
return f"Unexpected response format: {response}"
|
| 96 |
|
| 97 |
|
| 98 |
translator = gr.Interface(
|