Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import threading
|
|
| 5 |
import requests
|
| 6 |
import subprocess
|
| 7 |
import base64
|
| 8 |
-
from flask import Flask, render_template, request, jsonify, send_from_directory, abort
|
| 9 |
import queue
|
| 10 |
from groq import Groq
|
| 11 |
import itertools
|
|
@@ -140,16 +140,44 @@ def distill_prompt_for_short_video(enhanced_prompt, api_key, model_name):
|
|
| 140 |
print(f"⚠️ Groq Distiller failed: {e}. Falling back to original enhanced prompt.")
|
| 141 |
return enhanced_prompt, None
|
| 142 |
|
| 143 |
-
#
|
| 144 |
def generate_clip(prompt, idx, api_key, bytez_model):
|
| 145 |
from bytez import Bytez
|
| 146 |
sdk = Bytez(api_key)
|
| 147 |
model = sdk.model(bytez_model)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
if err:
|
|
|
|
| 153 |
return None, f"Model Error (Key ...{api_key[-4:]}): {err}"
|
| 154 |
|
| 155 |
filename = f"clip_{idx}_{uuid.uuid4().hex}.mp4"
|
|
@@ -162,24 +190,12 @@ def generate_clip(prompt, idx, api_key, bytez_model):
|
|
| 162 |
r.raise_for_status()
|
| 163 |
with open(filepath, "wb") as f: f.write(r.content)
|
| 164 |
else:
|
| 165 |
-
|
|
|
|
| 166 |
except Exception as e:
|
| 167 |
return None, f"Failed to save or download the generated clip: {e}"
|
| 168 |
|
| 169 |
return filepath, None
|
| 170 |
-
filepath = os.path.join(OUTPUT_FOLDER, filename)
|
| 171 |
-
try:
|
| 172 |
-
if isinstance(out, bytes):
|
| 173 |
-
with open(filepath, "wb") as f: f.write(out)
|
| 174 |
-
elif isinstance(out, str) and out.startswith('http'):
|
| 175 |
-
r = requests.get(out, timeout=300)
|
| 176 |
-
r.raise_for_status()
|
| 177 |
-
with open(filepath, "wb") as f: f.write(r.content)
|
| 178 |
-
else:
|
| 179 |
-
return None, f"Unexpected output type from model: {type(out)}"
|
| 180 |
-
except Exception as e:
|
| 181 |
-
return None, f"Failed to save or download the generated clip: {e}"
|
| 182 |
-
return filepath, None
|
| 183 |
|
| 184 |
def process_and_merge_clips(clip_files):
|
| 185 |
if not clip_files: return None
|
|
@@ -244,7 +260,6 @@ def generate_video_job(prompt, num_clips, bytez_model):
|
|
| 244 |
# ---------- FLASK ROUTES ----------
|
| 245 |
@app.route("/", methods=["GET"])
|
| 246 |
def home():
|
| 247 |
-
# अब यह 'templates' फोल्डर से 'index.html' फाइल को प्रस्तुत करेगा।
|
| 248 |
return render_template("index.html")
|
| 249 |
|
| 250 |
@app.route("/start", methods=["POST"])
|
|
|
|
| 5 |
import requests
|
| 6 |
import subprocess
|
| 7 |
import base64
|
| 8 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory, abort
|
| 9 |
import queue
|
| 10 |
from groq import Groq
|
| 11 |
import itertools
|
|
|
|
| 140 |
print(f"⚠️ Groq Distiller failed: {e}. Falling back to original enhanced prompt.")
|
| 141 |
return enhanced_prompt, None
|
| 142 |
|
| 143 |
+
# /// --- यह नया, सही और सुरक्षित फंक्शन है --- ///
|
| 144 |
def generate_clip(prompt, idx, api_key, bytez_model):
|
| 145 |
from bytez import Bytez
|
| 146 |
sdk = Bytez(api_key)
|
| 147 |
model = sdk.model(bytez_model)
|
| 148 |
|
| 149 |
+
out = None
|
| 150 |
+
err = None
|
| 151 |
|
| 152 |
+
try:
|
| 153 |
+
# हम model.run() से आने वाले सभी आउटपुट को एक ही वेरिएबल में पकड़ेंगे
|
| 154 |
+
result = model.run(prompt)
|
| 155 |
+
|
| 156 |
+
# हम यह देखने के लिए प्रिंट करेंगे कि असल में क्या आ रहा है (डीबगिंग के लिए)
|
| 157 |
+
print(f"DEBUG: model.run() returned a '{type(result)}'. Full result: {result}")
|
| 158 |
+
|
| 159 |
+
# अब हम सुरक्षित रूप से जांच करेंगे कि क्या result एक ट्यूपल (tuple) है
|
| 160 |
+
if isinstance(result, tuple) and len(result) >= 2:
|
| 161 |
+
# यदि यह एक ट्यूपल है, तो हम पहले दो मानों को out और err में डालेंगे
|
| 162 |
+
out = result[0]
|
| 163 |
+
err = result[1]
|
| 164 |
+
elif isinstance(result, tuple) and len(result) == 1:
|
| 165 |
+
# यदि केवल एक ही मान है, तो हम मानते हैं कि यह आउटपुट है
|
| 166 |
+
out = result[0]
|
| 167 |
+
err = None
|
| 168 |
+
else:
|
| 169 |
+
# यदि यह ट्यूपल नहीं है, तो हम मानते हैं कि यह सफल आउटपुट है
|
| 170 |
+
out = result
|
| 171 |
+
err = None
|
| 172 |
+
|
| 173 |
+
except Exception as e:
|
| 174 |
+
# यदि model.run() खुद ही क्रैश हो जाए (जो नहीं होना चाहिए, लेकिन सुरक्षा के लिए)
|
| 175 |
+
print(f"🛑 CRITICAL ERROR during model.run() call: {e}")
|
| 176 |
+
return None, str(e)
|
| 177 |
+
|
| 178 |
+
# --- बाकी का लॉजिक वही रहेगा ---
|
| 179 |
if err:
|
| 180 |
+
# अगर मॉडल ने कोई एरर लौटाया है (जैसे सेफ्टी फिल्टर)
|
| 181 |
return None, f"Model Error (Key ...{api_key[-4:]}): {err}"
|
| 182 |
|
| 183 |
filename = f"clip_{idx}_{uuid.uuid4().hex}.mp4"
|
|
|
|
| 190 |
r.raise_for_status()
|
| 191 |
with open(filepath, "wb") as f: f.write(r.content)
|
| 192 |
else:
|
| 193 |
+
# यदि 'out' में कुछ भी अपेक्षित नहीं है (जैसे None), तो हम यहाँ एक स्पष्ट एरर देंगे
|
| 194 |
+
return None, f"Unexpected or empty output from model. Output type: {type(out)}"
|
| 195 |
except Exception as e:
|
| 196 |
return None, f"Failed to save or download the generated clip: {e}"
|
| 197 |
|
| 198 |
return filepath, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
def process_and_merge_clips(clip_files):
|
| 201 |
if not clip_files: return None
|
|
|
|
| 260 |
# ---------- FLASK ROUTES ----------
|
| 261 |
@app.route("/", methods=["GET"])
|
| 262 |
def home():
|
|
|
|
| 263 |
return render_template("index.html")
|
| 264 |
|
| 265 |
@app.route("/start", methods=["POST"])
|