File size: 860 Bytes
0695957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from fastapi import FastAPI, Header, HTTPException
from transformers import pipeline

# Загружаем секретный ключ из 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: str, 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 {"result": result}