Download app.py from SmartDataAlex/get_sentiment_fastapi: direct link, hf CLI and curl.
- Browser
- Download file 977 Bytes
-
https://huggingface.co/spaces/SmartDataAlex/get_sentiment_fastapi/resolve/main/app.py
- Command line
-
hf download hf://spaces/SmartDataAlex/get_sentiment_fastapi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SmartDataAlex/get_sentiment_fastapi/resolve/main/app.py
977 Bytes
| 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 | |
| # Эндпоинт для анализа | |
| 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} | |
| def hello_world(): | |
| return {"message": "Hello World"} |