File size: 977 Bytes
2181ce8 | 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
from pydantic import BaseModel
import os
# Загружаем секретный ключ из Hugging Face Secrets
API_KEY = os.getenv("API_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 != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API Key")
result = sentiment_model(request.text)
return {"result": result}
@app.get("/hello")
def hello_world():
return {"message": "Hello World"} |