/
vnss64657
/
SE_FastAPI_Example
Обзор
Документация
Войти
/
vnss64657
/
SE_FastAPI_Example
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
feature/fastapi-refactor
main.py
37 строк
1 KB
Axlifreeway
refactor: remove unsafe GET route and add response model
02 май 2026, 20:38
Верифицирован
02 май 2026, 20:38
d1d564f
Код
Авторство
О чём код?
import os from fastapi import FastAPI from transformers import pipeline from pydantic import BaseModel, Field MODEL_NAME = os.getenv("MODEL_NAME", "sentiment-analysis") APP_HOST = os.getenv("APP_HOST", "0.0.0.0") APP_PORT = int(os.getenv("APP_PORT", "8000")) class Item(BaseModel): text: str = Field( ..., min_length=1, max_length=5000, description="Текст для анализа тональности", examples=["Привет! Как дела?"] ) class PredictionResult(BaseModel): label: str score: float app = FastAPI( title="Sentiment Analysis API", description="API для анализа тональности текста", version="1.0.0", ) classifier = pipeline(MODEL_NAME) @app.get("/") def root(): return {"message": "FastAPI service started!"} @app.post("/predict/", response_model=list[PredictionResult]) def predict(item: Item): results = classifier(item.text) return [PredictionResult(label=r["label"], score=r["score"]) for r in results]