Spaces:
Runtime error
Runtime error
Download app.py from PrivateBlog/FastAPI: direct link, hf CLI and curl.
- Browser
- Download file 860 Bytes
-
https://huggingface.co/spaces/PrivateBlog/FastAPI/resolve/9cedf9ab32182445ae9fd209c6a1f4aa72d241c4/app.py
- Command line
-
hf download hf://spaces/PrivateBlog/FastAPI@9cedf9ab32182445ae9fd209c6a1f4aa72d241c4/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PrivateBlog/FastAPI/resolve/9cedf9ab32182445ae9fd209c6a1f4aa72d241c4/app.py
860 Bytes
| import os | |
| from fastapi import FastAPI, Header, HTTPException | |
| from transformers import pipeline | |
| # Загружаем секретный ключ из 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 | |
| # Эндпоинт для анализа | |
| 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} |