SmartDataAlex commited on
Commit
2181ce8
·
verified ·
1 Parent(s): afbbf6e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Header, HTTPException
3
+ from transformers import pipeline
4
+ from pydantic import BaseModel
5
+
6
+ import os
7
+
8
+ # Загружаем секретный ключ из Hugging Face Secrets
9
+ API_KEY = os.getenv("API_KEY")
10
+
11
+ # Загружаем модель один раз при старте
12
+ sentiment_model = pipeline(
13
+ "sentiment-analysis",
14
+ model="cardiffnlp/twitter-roberta-base-sentiment-latest"
15
+ )
16
+
17
+ # FastAPI приложение
18
+ app = FastAPI(title="Sentiment Analysis API")
19
+
20
+ # Модель запроса
21
+ class TextRequest(BaseModel):
22
+ text: str
23
+
24
+ # Эндпоинт для анализа
25
+ @app.post("/analyze")
26
+ def analyze_sentiment(request: TextRequest, x_api_key: str = Header(...)):
27
+ if x_api_key != API_KEY:
28
+ raise HTTPException(status_code=401, detail="Invalid API Key")
29
+ result = sentiment_model(request.text)
30
+ return {"result": result}
31
+
32
+ @app.get("/hello")
33
+ def hello_world():
34
+ return {"message": "Hello World"}