Pep1900 commited on
Commit
0695957
·
verified ·
1 Parent(s): 5cf2e7f

Create app.py

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