Spaces:
Runtime error
Runtime error
File size: 1,179 Bytes
1917b0b ce10f9a 5b4c169 07fca4f a3ae69c 4ffc5f1 ce10f9a 07fca4f d43b4cf 07fca4f ce10f9a 604d57b 1917b0b ce10f9a 7e23014 1917b0b 962bd20 1917b0b ce8a810 1917b0b ce8a810 1917b0b a3ae69c 94461c6 a3ae69c ce10f9a a3ae69c |
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 35 36 |
import torch
import os
import requests
import spaces
import gradio as gr
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}"}
@spaces.GPU
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def analyze_sentiment(text):
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>You're a sentiment analyzer. Your role is to evaluate the general feeling of the prompt. Answer only with 'positive' or 'negative'. Don't add any explanations. Here's the text to analyze (don't add any text) : {text}<|eot_id|><|start_header_id|>user<|end_header_id|>"
output = query({
"inputs": prompt,
})
# Assurez-vous de gérer correctement la sortie de l'API
if isinstance(output, list) and len(output) > 0:
return output[0].get('generated_text', 'Erreur: Réponse inattendue')
else:
return "Erreur: Réponse inattendue de l'API"
demo = gr.Interface(
fn = analyze_sentiment,
inputs=["text"],
outputs=["text"],
)
demo.launch() |