Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Hugging Face API Token (Set in Hugging Face Spaces)
|
| 6 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 7 |
|
| 8 |
# Hugging Face model endpoints
|
|
@@ -17,10 +17,22 @@ def get_toxicity_prediction(text, language):
|
|
| 17 |
return "Error: Model not found for the selected language"
|
| 18 |
|
| 19 |
url = MODEL_ENDPOINTS[language]
|
| 20 |
-
headers = {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
if response.status_code == 200:
|
| 26 |
predictions = response.json()[0] # Extract first prediction result
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Hugging Face API Token (Set this in Hugging Face Spaces)
|
| 6 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 7 |
|
| 8 |
# Hugging Face model endpoints
|
|
|
|
| 17 |
return "Error: Model not found for the selected language"
|
| 18 |
|
| 19 |
url = MODEL_ENDPOINTS[language]
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {HF_API_TOKEN}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
payload = {
|
| 26 |
+
"inputs": text,
|
| 27 |
+
"parameters": {
|
| 28 |
+
"return_all_scores": True
|
| 29 |
+
},
|
| 30 |
+
"options": {
|
| 31 |
+
"wait_for_model": True # Ensures the model loads if it's inactive
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 36 |
|
| 37 |
if response.status_code == 200:
|
| 38 |
predictions = response.json()[0] # Extract first prediction result
|