Spaces:
Runtime error
Runtime error
Download app.py from PrivateBlog/FastAPI: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/PrivateBlog/FastAPI/resolve/f2aeda2a2f7f360d2f733c3f9fbcf4194f020660/app.py
- Command line
-
hf download hf://spaces/PrivateBlog/FastAPI@f2aeda2a2f7f360d2f733c3f9fbcf4194f020660/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PrivateBlog/FastAPI/resolve/f2aeda2a2f7f360d2f733c3f9fbcf4194f020660/app.py
1.03 kB
| 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 | |
| # Эндпоинт для анализа | |
| 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"} |