Spaces:
Runtime error
Runtime error
File size: 948 Bytes
e815caf 7c8e64d ebfbe36 e815caf 7c8e64d e815caf 7c8e64d ddb6fa6 7c8e64d e815caf 7c8e64d e815caf 7c8e64d e815caf 7c8e64d | 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 | import os
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
from transformers import pipeline
#import torch
# Загружаем секретный ключ из Hugging Face Secrets
SECRET_KEY = os.getenv("SECRET_KEY")
# Загружаем модель один раз при старте
sentiment_model = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest"
)
# FastAPI приложение
app = FastAPI(title="Sentiment Analysis API")
# Модель запроса
class TextRequest(BaseModel):
text: str
# Эндпоинт для анализа
@app.post("/analyze")
def analyze_sentiment(request: TextRequest, x_api_key: str = Header(...)):
if x_api_key != SECRET_KEY:
raise HTTPException(status_code=401, detail="Invalid API Key")
result = sentiment_model(request.text)
return {"label": result[0]["label"], "score": result[0]["score"]} |