File size: 1,026 Bytes
0695957
 
 
ff79521
f2aeda2
e21b2f1
0695957
203a1a4
 
0695957
acfe761
0695957
 
 
 
e21b2f1
 
0695957
 
 
 
 
 
ff79521
 
0695957
 
 
ff79521
acfe761
 
0695957
ff79521
 
 
 
 
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
37
import os
from fastapi import FastAPI, Header, HTTPException
from transformers import pipeline
from pydantic import BaseModel
import gradio as gr
import torch

import os

# Загружаем секретный ключ из Hugging Face Secrets
API_KEY = os.getenv("API_KEY")

# Загружаем модель один раз при старте
sentiment_model = pipeline(
    "sentiment-analysis",
    model="cardiffnlp/twitter-roberta-base-sentiment-latest",
    device = 0
)

# 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"}