File size: 1,185 Bytes
de65063 ec29ba4 de65063 ec29ba4 de65063 ec29ba4 de65063 ec29ba4 de65063 ec29ba4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import gradio as gr
import requests
import os
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
raise ValueError("HF_TOKEN is not set. Please add it as a secret in your Space settings.")
API_URL = "https://api-inference.huggingface.co/models/gabehubner/trained-distilbert-model"
headers = {"Authorization": f"Bearer {hf_token}"}
def classify_text(text):
response = requests.post(API_URL, headers=headers, json={"inputs": text})
if response.status_code != 200:
return f"Error: {response.text}"
results = response.json()
if isinstance(results, list) and results and isinstance(results[0], list) and results[0]:
prediction = results[0][0] # Get the first prediction from the inner list
return f"Label: {prediction['label']} (Confidence: {prediction['score']:.2f})"
else:
return f"Unexpected response format: {results}"
interface = gr.Interface(
fn=classify_text,
inputs=gr.Textbox(placeholder="Enter text here..."),
outputs="text",
title="Sentiment Classifier",
description="Enter text and see whether it's classified as positive or negative!",
)
if __name__ == "__main__":
interface.launch()
|