webnowa commited on
Commit
de74be6
·
verified ·
1 Parent(s): 209705f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -32
app.py CHANGED
@@ -1,11 +1,13 @@
1
  from fastapi import FastAPI
2
  from fastapi.middleware.cors import CORSMiddleware
3
- import uvicorn
4
  import uuid
 
 
5
 
6
  app = FastAPI()
7
 
8
- # CORS – pozwala frontendowi łączyć się z API
9
  app.add_middleware(
10
  CORSMiddleware,
11
  allow_origins=["*"],
@@ -13,43 +15,61 @@ app.add_middleware(
13
  allow_headers=["*"],
14
  )
15
 
16
- # Prosta baza statusów w pamięci
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  TASKS = {}
18
 
19
- @app.post("/generate")
20
- async def generate_video(data: dict):
21
- """
22
- Odbiera prompt, styl, czas trwania itd.
23
- Zwraca URL do wygenerowanego wideo.
24
- """
25
- task_id = str(uuid.uuid4())
26
 
27
- # Tu normalnie wywołałbyś model AI
28
- # Na razie zwracamy przykładowy URL
29
- video_url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/video-placeholder.mp4"
 
 
 
 
 
 
 
 
30
 
31
- TASKS[task_id] = {
32
- "status": "done",
33
- "video_url": video_url
34
- }
35
 
36
- return {
37
- "task_id": task_id,
38
- "video_url": video_url
39
- }
40
 
41
 
42
- @app.get("/status/{task_id}")
43
- async def get_status(task_id: str):
44
- """
45
- Zwraca status generowania.
46
- """
47
- if task_id not in TASKS:
48
- return {"status": "not_found"}
 
49
 
50
- return TASKS[task_id]
51
 
 
52
 
53
- # Uruchamianie lokalne (HuggingFace tego nie używa)
54
- if __name__ == "__main__":
55
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  from fastapi import FastAPI
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from huggingface_hub import InferenceClient
4
  import uuid
5
+ import asyncio
6
+ import os
7
 
8
  app = FastAPI()
9
 
10
+ # CORS
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
15
  allow_headers=["*"],
16
  )
17
 
18
+ HF_TOKEN = os.getenv("HF_TOKEN")
19
+
20
+ # MODELE – wszystkie darmowe
21
+ VIDEO_MODELS = {
22
+ "tiktok": "tencent/HunyuanVideo",
23
+ "social": "tencent/HunyuanVideo",
24
+ "cinematic": "Lightricks/LTX-Video-0.9.7-distilled",
25
+ "ads": "Lightricks/LTX-Video-0.9.7-distilled",
26
+ "music": "Wan-AI/Wan2.1-T2V-1.3B",
27
+ "experimental": "Wan-AI/Wan2.1-T2V-1.3B",
28
+ "avatar": "OpenTalker/SadTalker", # darmowy avatar
29
+ "image2video": "tencent/HunyuanVideo", # I2V też działa
30
+ }
31
+
32
+ # Prosta baza zadań
33
  TASKS = {}
34
 
35
+ async def generate_video_task(task_id, prompt, mode, image_url=None):
36
+ model_id = VIDEO_MODELS.get(mode, "tencent/HunyuanVideo")
37
+ client = InferenceClient(model_id, token=HF_TOKEN)
 
 
 
 
38
 
39
+ try:
40
+ if image_url:
41
+ # IMAGE → VIDEO
42
+ result = client.post(
43
+ json={"prompt": prompt, "image": image_url}
44
+ )
45
+ else:
46
+ # TEXT → VIDEO
47
+ result = client.post(
48
+ json={"prompt": prompt}
49
+ )
50
 
51
+ TASKS[task_id]["status"] = "done"
52
+ TASKS[task_id]["video_url"] = result
 
 
53
 
54
+ except Exception as e:
55
+ TASKS[task_id]["status"] = "error"
56
+ TASKS[task_id]["error"] = str(e)
 
57
 
58
 
59
+ @app.post("/generate")
60
+ async def generate(data: dict):
61
+ prompt = data.get("prompt", "")
62
+ mode = data.get("mode", "tiktok")
63
+ image_url = data.get("image_url")
64
+
65
+ task_id = str(uuid.uuid4())
66
+ TASKS[task_id] = {"status": "processing"}
67
 
68
+ asyncio.create_task(generate_video_task(task_id, prompt, mode, image_url))
69
 
70
+ return {"task_id": task_id}
71
 
72
+
73
+ @app.get("/status/{task_id}")
74
+ async def status(task_id: str):
75
+ return TASKS.get(task_id, {"status": "not_found"})