Update comfy_engine.py
Browse files- comfy_engine.py +26 -4
comfy_engine.py
CHANGED
|
@@ -2,6 +2,7 @@ import json, uuid, os, asyncio, time
|
|
| 2 |
import httpx
|
| 3 |
import websockets
|
| 4 |
from urllib.parse import urlencode
|
|
|
|
| 5 |
|
| 6 |
# AMD GPU सर्वर का IP
|
| 7 |
COMFY_HOST = os.getenv("COMFY_HOST", "134.199.132.159")
|
|
@@ -17,6 +18,25 @@ RESOLUTIONS = {
|
|
| 17 |
"720p": {"16:9": (1280, 720), "9:16": (720, 1280), "1:1": (768, 768)}
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def inject_params(req: dict) -> dict:
|
| 21 |
p = json.loads(json.dumps(WORKFLOW_TEMPLATE))
|
| 22 |
|
|
@@ -39,7 +59,6 @@ def inject_params(req: dict) -> dict:
|
|
| 39 |
p["78"]["inputs"]["steps"] = 4
|
| 40 |
p["78"]["inputs"]["cfg"] = 5.0
|
| 41 |
|
| 42 |
-
# नोट: सीड (Seed) को हमने फिक्स रखा है जैसा कि आपने चाहा था (Consistency के लिए)
|
| 43 |
return p
|
| 44 |
|
| 45 |
def extract_video_url(history: dict, token: str) -> str:
|
|
@@ -72,14 +91,14 @@ async def generate_video_stream(req: dict):
|
|
| 72 |
start_t = time.time()
|
| 73 |
progress_fake = 0
|
| 74 |
|
| 75 |
-
# WebSocket के ज़रिए प्रोग्रेस ट्रैक करना
|
| 76 |
ws_url = f"ws://{COMFY_HOST}/ws?clientId={client_id}&token={token}"
|
| 77 |
try:
|
| 78 |
async with websockets.connect(ws_url, ping_interval=20) as ws:
|
| 79 |
while True:
|
| 80 |
msg_raw = await ws.recv()
|
| 81 |
|
| 82 |
-
# बाइनरी डेटा (Bytes) हैंडलिंग -
|
| 83 |
if isinstance(msg_raw, (bytes, bytearray)):
|
| 84 |
if progress_fake < 95 and (time.time() - start_t) > 2:
|
| 85 |
progress_fake = min(95, progress_fake + 1)
|
|
@@ -106,7 +125,10 @@ async def generate_video_stream(req: dict):
|
|
| 106 |
|
| 107 |
try:
|
| 108 |
v_url = extract_video_url(history, token)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
yield f"data: {json.dumps({'progress': 100, 'status': 'done', 'url': v_url})}\n\n"
|
| 110 |
except:
|
| 111 |
yield f"data: {json.dumps({'error': 'File extraction failed'})}\n\n"
|
| 112 |
-
|
|
|
|
| 2 |
import httpx
|
| 3 |
import websockets
|
| 4 |
from urllib.parse import urlencode
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# AMD GPU सर्वर का IP
|
| 8 |
COMFY_HOST = os.getenv("COMFY_HOST", "134.199.132.159")
|
|
|
|
| 18 |
"720p": {"16:9": (1280, 720), "9:16": (720, 1280), "1:1": (768, 768)}
|
| 19 |
}
|
| 20 |
|
| 21 |
+
def save_to_history(prompt, video_url):
|
| 22 |
+
history_file = "history.json"
|
| 23 |
+
new_entry = {
|
| 24 |
+
"id": str(uuid.uuid4())[:8],
|
| 25 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 26 |
+
"prompt": prompt,
|
| 27 |
+
"url": video_url
|
| 28 |
+
}
|
| 29 |
+
data = []
|
| 30 |
+
if os.path.exists(history_file):
|
| 31 |
+
try:
|
| 32 |
+
with open(history_file, "r", encoding="utf-8") as f:
|
| 33 |
+
data = json.load(f)
|
| 34 |
+
except: pass
|
| 35 |
+
|
| 36 |
+
data.insert(0, new_entry) # नया वीडियो सबसे ऊपर
|
| 37 |
+
with open(history_file, "w", encoding="utf-8") as f:
|
| 38 |
+
json.dump(data[:50], f, indent=4) # सिर्फ लेटेस्ट 50 वीडियोस सेव करेंगे
|
| 39 |
+
|
| 40 |
def inject_params(req: dict) -> dict:
|
| 41 |
p = json.loads(json.dumps(WORKFLOW_TEMPLATE))
|
| 42 |
|
|
|
|
| 59 |
p["78"]["inputs"]["steps"] = 4
|
| 60 |
p["78"]["inputs"]["cfg"] = 5.0
|
| 61 |
|
|
|
|
| 62 |
return p
|
| 63 |
|
| 64 |
def extract_video_url(history: dict, token: str) -> str:
|
|
|
|
| 91 |
start_t = time.time()
|
| 92 |
progress_fake = 0
|
| 93 |
|
| 94 |
+
# WebSocket के ज़रिए प्रोग्रेस ट्रैक करना
|
| 95 |
ws_url = f"ws://{COMFY_HOST}/ws?clientId={client_id}&token={token}"
|
| 96 |
try:
|
| 97 |
async with websockets.connect(ws_url, ping_interval=20) as ws:
|
| 98 |
while True:
|
| 99 |
msg_raw = await ws.recv()
|
| 100 |
|
| 101 |
+
# बाइनरी डेटा (Bytes) हैंडलिंग - हैक
|
| 102 |
if isinstance(msg_raw, (bytes, bytearray)):
|
| 103 |
if progress_fake < 95 and (time.time() - start_t) > 2:
|
| 104 |
progress_fake = min(95, progress_fake + 1)
|
|
|
|
| 125 |
|
| 126 |
try:
|
| 127 |
v_url = extract_video_url(history, token)
|
| 128 |
+
|
| 129 |
+
# 🚀 MAGIC FIX: यहाँ वीडियो हिस्ट्री में सेव हो रहा है!
|
| 130 |
+
save_to_history(req.get("prompt", ""), v_url)
|
| 131 |
+
|
| 132 |
yield f"data: {json.dumps({'progress': 100, 'status': 'done', 'url': v_url})}\n\n"
|
| 133 |
except:
|
| 134 |
yield f"data: {json.dumps({'error': 'File extraction failed'})}\n\n"
|
|
|