Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,24 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
API_URL = "https://api-inference.huggingface.co/models/gabehubner/trained-distilbert-model"
|
| 6 |
-
headers = {"Authorization": f"Bearer {
|
| 7 |
|
| 8 |
def classify_text(text):
|
| 9 |
response = requests.post(API_URL, headers=headers, json={"inputs": text})
|
| 10 |
if response.status_code != 200:
|
| 11 |
return f"Error: {response.text}"
|
| 12 |
-
|
| 13 |
results = response.json()
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
interface = gr.Interface(
|
| 17 |
fn=classify_text,
|
|
@@ -22,4 +30,4 @@ interface = gr.Interface(
|
|
| 22 |
)
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
| 25 |
-
interface.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 6 |
+
if hf_token is None:
|
| 7 |
+
raise ValueError("HF_TOKEN is not set. Please add it as a secret in your Space settings.")
|
| 8 |
+
|
| 9 |
API_URL = "https://api-inference.huggingface.co/models/gabehubner/trained-distilbert-model"
|
| 10 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 11 |
|
| 12 |
def classify_text(text):
|
| 13 |
response = requests.post(API_URL, headers=headers, json={"inputs": text})
|
| 14 |
if response.status_code != 200:
|
| 15 |
return f"Error: {response.text}"
|
| 16 |
+
|
| 17 |
results = response.json()
|
| 18 |
+
if isinstance(results, list) and results and isinstance(results[0], list) and results[0]:
|
| 19 |
+
prediction = results[0][0] # Get the first prediction from the inner list
|
| 20 |
+
return f"Label: {prediction['label']} (Confidence: {prediction['score']:.2f})"
|
| 21 |
+
else:
|
| 22 |
+
return f"Unexpected response format: {results}"
|
| 23 |
|
| 24 |
interface = gr.Interface(
|
| 25 |
fn=classify_text,
|
|
|
|
| 30 |
)
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
+
interface.launch()
|