Spaces:
Sleeping
Sleeping
| 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"} |