|
import os
|
|
|
|
import uvicorn
|
|
import torch
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.responses import JSONResponse, Response
|
|
from pydantic import BaseModel
|
|
from transformers import pipeline
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
title="Bielik Text Generation API",
|
|
description="API do generowania tekstu za pomoc膮 modelu Bielik-1.5B-v3.0-Instruct",
|
|
version="1.0.0"
|
|
)
|
|
|
|
|
|
MODEL_NAME = "speakleash/Bielik-1.5B-v3.0-Instruct"
|
|
generator = None
|
|
|
|
|
|
|
|
class GenerationRequest(BaseModel):
|
|
prompt: str
|
|
max_new_tokens: int = 50
|
|
temperature: float = 0.7
|
|
top_p: float = 0.9
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""
|
|
艁adowanie modelu podczas uruchamiania aplikacji.
|
|
To zajmie troch臋 czasu, ale dzieje si臋 tylko raz.
|
|
"""
|
|
global generator
|
|
|
|
print(f"艁adowanie modelu: {MODEL_NAME}...")
|
|
try:
|
|
|
|
|
|
|
|
generator = pipeline(
|
|
"text-generation",
|
|
model=MODEL_NAME,
|
|
|
|
)
|
|
print("Model za艂adowany pomy艣lnie!")
|
|
except Exception as e:
|
|
print(f"B艂膮d 艂adowania modelu: {e}")
|
|
|
|
|
|
generator = None
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""
|
|
G艂贸wny endpoint (health check).
|
|
"""
|
|
return {"message": "Bielik Text Generation API is running!"}
|
|
|
|
|
|
@app.post("/generate")
|
|
async def generate_text(request: GenerationRequest):
|
|
"""
|
|
Endpoint do generowania tekstu na podstawie promptu.
|
|
"""
|
|
if generator is None:
|
|
raise HTTPException(status_code=503, detail="Model nie zosta艂 za艂adowany lub wyst膮pi艂 b艂膮d.")
|
|
|
|
try:
|
|
generated_text = generator(
|
|
request.prompt,
|
|
max_new_tokens=request.max_new_tokens,
|
|
temperature=request.temperature,
|
|
top_p=request.top_p,
|
|
do_sample=True,
|
|
return_full_text=False
|
|
)
|
|
|
|
|
|
|
|
response_data = {"generated_text": generated_text[0]["generated_text"]}
|
|
return JSONResponse(
|
|
content=response_data,
|
|
media_type="application/json; charset=utf-8"
|
|
)
|
|
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"B艂膮d podczas generowania tekstu: {e}")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
|
|
|