File size: 1,038 Bytes
0695957
 
 
 
203a1a4
 
 
 
 
 
531048a
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
28
29
30
31
32
33
34
import os
from fastapi import FastAPI, Header, HTTPException
from transformers import pipeline

import os

os.makedirs("/home/user/app/hf_cache", exist_ok=True)
os.environ["HF_HOME"] = "/home/user/app/hf_cache"
os.environ["TRANSFORMERS_CACHE"] = "/home/user/app/hf_cache"


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