alex-abb's picture
Update app.py
f946cbe verified
raw
history blame
1.33 kB
import requests
import gradio as gr
import os
api_token = os.environ.get("TOKEN")
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-llama-3-8B-Instruct"
headers = {"Authorization": f"Bearer {api_token}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
if response.status_code == 200:
return response.json()
else:
raise Exception(f"Request failed with status code {response.status_code}: {response.text}")
def detect_sentiment(message):
prompt = f"Détecte le sentiment de ce message. Réponds par 'positif' ou 'négatif' :\nMessage : \"{message}\""
response = query({"inputs": prompt})
generated_texts = response.get('generated_text', [])
if generated_texts:
sentiment = generated_texts[0].strip().lower()
return sentiment
else:
raise Exception("No generated_text found in API response")
def sentiment_analysis_interface(message):
sentiment = detect_sentiment(message)
return sentiment
# Créer l'interface Gradio
iface = gr.Interface(
fn=sentiment_analysis_interface,
inputs="text",
outputs="text",
title="Détection de Sentiment",
description="Entrez un message pour détecter si le sentiment est positif ou négatif.",
)
# Lancer l'interface
iface.launch()