webnowa commited on
Commit
c33959f
·
verified ·
1 Parent(s): 6fa4f94

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +79 -0
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # main.py
2
+ # FastAPI API dla tomasz-svd (Etap 5/6)
3
+
4
+ from fastapi import FastAPI
5
+ from pydantic import BaseModel
6
+ from typing import Optional, List
7
+
8
+ from core import (
9
+ analyze_domain,
10
+ generate_script,
11
+ generate_music,
12
+ convert_to_mp3,
13
+ generate_video_from_b64,
14
+ )
15
+
16
+ app = FastAPI(title="tomasz-svd API")
17
+
18
+ class GenerateAdRequest(BaseModel):
19
+ domain: str
20
+ style: str = "energetyczny TikTok"
21
+ length: int = 15
22
+
23
+ class GenerateAdResponse(BaseModel):
24
+ status: str
25
+ message: Optional[str] = None
26
+ video_path: Optional[str] = None
27
+
28
+ @app.get("/health")
29
+ def health():
30
+ return {"status": "ok"}
31
+
32
+ @app.post("/analyze")
33
+ def api_analyze(domain: str):
34
+ return analyze_domain(domain)
35
+
36
+ @app.post("/script")
37
+ def api_script(domain: str, brand_prompt: str, brand_text: str, length: int = 15, style: str = "energetyczny TikTok"):
38
+ return generate_script(brand_prompt, domain, brand_text, length, style)
39
+
40
+ @app.post("/audio")
41
+ def api_audio(prompt: str = "energetic modern ad music", duration: int = 15):
42
+ wav = generate_music(prompt, duration)
43
+ mp3 = convert_to_mp3(wav)
44
+ return {"status": "ok", "audio_path": mp3}
45
+
46
+ class VideoRequest(BaseModel):
47
+ images_b64: List[str]
48
+ script_json: str
49
+ audio_path: str
50
+
51
+ @app.post("/video")
52
+ def api_video(req: VideoRequest):
53
+ video = generate_video_from_b64(req.images_b64, req.script_json, req.audio_path)
54
+ if not video:
55
+ return {"status": "error", "message": "Nie udało się wygenerować wideo."}
56
+ return {"status": "ok", "video_path": video}
57
+
58
+ @app.post("/generate_ad", response_model=GenerateAdResponse)
59
+ def api_generate_ad(req: GenerateAdRequest):
60
+ analyzed = analyze_domain(req.domain)
61
+ if "error" in analyzed:
62
+ return GenerateAdResponse(status="error", message=analyzed["error"])
63
+
64
+ script = generate_script(
65
+ analyzed["prompt"],
66
+ analyzed["domain"],
67
+ analyzed["text_snippet"],
68
+ req.length,
69
+ req.style
70
+ )
71
+
72
+ wav = generate_music("energetic modern ad music", req.length)
73
+ mp3 = convert_to_mp3(wav)
74
+
75
+ video = generate_video_from_b64(analyzed["images"], script, mp3)
76
+ if not video:
77
+ return GenerateAdResponse(status="error", message="Nie udało się wygenerować wideo.")
78
+
79
+ return GenerateAdResponse(status="ok", video_path=video)