|
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] |
|
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() |
|
|