Spaces:
Running
Running
| 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" | |
| ) | |
| 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 | |
| async def health_check(): | |
| return {"status": "ok", "model_initialized": hf_service.pipeline is not None} | |
| 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)}") |