Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import requests | |
import spaces | |
# Récupérer le token depuis les secrets | |
api_token = os.environ.get("HF_API_TOKEN") | |
API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english" | |
headers = {"Authorization": f"Bearer {api_token}"} | |
def analyze_sentiment(text): | |
payload = {"inputs": text} | |
try: | |
response = requests.post(API_URL, headers=headers, json=payload) | |
response.raise_for_status() # Lève une exception pour les codes d'erreur HTTP | |
result = response.json() | |
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list): | |
sentiment_scores = result[0] | |
sentiment = "heureux" if sentiment_scores[1] > sentiment_scores[0] else "malheureux" | |
return sentiment | |
else: | |
return "Erreur: Format de réponse inattendu" | |
except requests.RequestException as e: | |
return f"Erreur de requête: {str(e)}" | |
except Exception as e: | |
return f"Erreur inattendue: {str(e)}" | |
def gradio_interface(input_text): | |
return analyze_sentiment(input_text) | |
iface = gr.Interface( | |
fn=gradio_interface, | |
inputs=gr.Textbox(lines=3, placeholder="Entrez votre texte ici..."), | |
outputs=gr.Label(num_top_classes=1), | |
title="Analyseur de Sentiment", | |
description="Entrez un texte pour déterminer si le sentiment est 'heureux' ou 'malheureux'." | |
) | |
iface.launch() |