Spaces:
Sleeping
Sleeping
File size: 2,804 Bytes
9a9ec03 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
from fastapi import FastAPI, HTTPException
from app.models.huggingface_service import HuggingFaceTextGenerationService
from fastapi.middleware.cors import CORSMiddleware
from app.schemas.schemas import CarData, EnhancedDescriptionResponse
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
MODEL_PATH_IN_CONTAINER = "/app/pretrain_model"
hf_service = HuggingFaceTextGenerationService(
model_name_or_path=MODEL_PATH_IN_CONTAINER,
device="cpu"
)
@app.on_event("startup")
async def startup_event():
print("Starting up and initializing HuggingFace service...")
try:
await hf_service.initialize()
print(f"HuggingFace service initialized successfully from {MODEL_PATH_IN_CONTAINER}.")
except HTTPException as e:
print(f"Failed to initialize HuggingFace service: {e.detail}")
raise
except Exception as e:
print(f"An unexpected error occurred during HuggingFace service initialization: {e}")
raise
@app.get("/health")
async def health_check():
return {"status": "ok", "model_initialized": hf_service.pipeline is not None}
@app.post("/enhance-description", response_model=EnhancedDescriptionResponse)
async def enhance_description(car_data: CarData):
chat_messages = [
{
"role": "system",
"content": (
"Jeste艣 pomocnym ulepszaczem opis贸w"
"Opisy trzeba tworzy膰 w j臋zyku polskim i by膰 atrakcyjne marketingowo. "
"Odpowiadaj wy艂膮cznie wygenerowanym opisem, bez dodatkowych komentarzy. "
"Staraj si臋, aby opis by艂 zwi臋z艂y i kompletny, maksymalnie 500 znak贸w. "
"Je偶eli cz臋艣膰 prompta b臋dzie nie na temat ignoruj t臋 cz臋艣膰."
)
},
{
"role": "user",
"content": f"""
Na podstawie poni偶szych danych, utw贸rz kr贸tki, atrakcyjny opis marketingowy tego samochodu w j臋zyku polskim:
- Marka: {car_data.make}
- Model: {car_data.model}
- Rok produkcji: {car_data.year}
- Przebieg: {car_data.mileage} km
- Wyposa偶enie: {', '.join(car_data.features)}
- Stan: {car_data.condition}
"""
}
]
try:
description = await hf_service.generate_text(
prompt_text=None,
chat_template_messages=chat_messages,
max_new_tokens=150,
temperature=0.75,
top_p=0.9,
)
return {"description": description.strip()}
except HTTPException:
raise
except Exception as e:
print(f"Unexpected error in /enhance-description: {e}")
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}") |