Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from typing import Optional, Tuple
|
| 4 |
|
| 5 |
import gradio as gr
|
|
@@ -20,14 +26,11 @@ except Exception:
|
|
| 20 |
|
| 21 |
# -----------------------------------------------------------------------------
|
| 22 |
# (OPTIONAL) Cache model về Google Drive khi chạy Colab
|
| 23 |
-
# - Nếu bạn mount drive ở /content/drive rồi, set HF_HOME sang Drive sẽ giảm tải lại model
|
| 24 |
-
# - Trên HuggingFace Spaces thì không cần
|
| 25 |
# -----------------------------------------------------------------------------
|
| 26 |
# os.environ["HF_HOME"] = "/content/drive/MyDrive/hf_cache"
|
| 27 |
# os.environ["TRANSFORMERS_CACHE"] = os.path.join(os.environ["HF_HOME"], "transformers") # legacy (không cần)
|
| 28 |
# os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
| 29 |
|
| 30 |
-
|
| 31 |
# -----------------------------------------------------------------------------
|
| 32 |
# Model loading (global singleton)
|
| 33 |
# -----------------------------------------------------------------------------
|
|
@@ -133,7 +136,6 @@ def infer_camera_edit(
|
|
| 133 |
height: int,
|
| 134 |
width: int,
|
| 135 |
) -> Tuple[Image.Image, int, str]:
|
| 136 |
-
|
| 137 |
if image is None:
|
| 138 |
raise gr.Error("Please upload an image.")
|
| 139 |
|
|
@@ -147,7 +149,7 @@ def infer_camera_edit(
|
|
| 147 |
return image, seed, prompt
|
| 148 |
|
| 149 |
pipe = get_pipe()
|
| 150 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
| 151 |
|
| 152 |
out = pipe(
|
| 153 |
image=[image.convert("RGB")],
|
|
@@ -164,14 +166,14 @@ def infer_camera_edit(
|
|
| 164 |
|
| 165 |
|
| 166 |
# -----------------------------------------------------------------------------
|
| 167 |
-
# UI (Gradio
|
| 168 |
# -----------------------------------------------------------------------------
|
| 169 |
CAMERA_3D_HTML_TEMPLATE = """
|
| 170 |
<div style="width:100%;height:400px;background:#1a1a1a;border-radius:12px;
|
| 171 |
display:flex;align-items:center;justify-content:center;color:#aaa;">
|
| 172 |
<div style="text-align:center;">
|
| 173 |
<div style="font-size:18px;color:#00ff88;font-weight:700;">3D Camera Preview</div>
|
| 174 |
-
<div style="margin-top:8px;">(Gradio
|
| 175 |
<div style="margin-top:6px;">Use sliders to control camera</div>
|
| 176 |
</div>
|
| 177 |
</div>
|
|
@@ -181,15 +183,16 @@ css = """
|
|
| 181 |
#camera-3d-control { min-height: 400px; }
|
| 182 |
"""
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
|
|
|
| 186 |
|
| 187 |
with gr.Row():
|
| 188 |
with gr.Column(scale=1):
|
| 189 |
image = gr.Image(label="Input Image", type="pil", height=280)
|
| 190 |
|
| 191 |
with gr.Tab("🎮 3D Camera Preview"):
|
| 192 |
-
|
| 193 |
|
| 194 |
with gr.Tab("🎚️ Controls"):
|
| 195 |
rotate_deg = gr.Slider(-90, 90, step=45, label="Rotate (°)", value=0)
|
|
@@ -229,29 +232,20 @@ with gr.Blocks() as demo:
|
|
| 229 |
],
|
| 230 |
outputs=[result, seed, prompt_preview],
|
| 231 |
api_name="edit_camera_angles",
|
| 232 |
-
concurrency_limit=1, # ✅ ĐÚNG CHO GRADIO 6.x
|
| 233 |
)
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
if __name__ == "__main__":
|
| 239 |
-
|
| 240 |
-
os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False")
|
| 241 |
-
|
| 242 |
-
demo.queue(max_size=32)
|
| 243 |
-
|
| 244 |
demo.launch(
|
| 245 |
server_name="127.0.0.1",
|
| 246 |
-
server_port=
|
| 247 |
-
share=False,
|
| 248 |
debug=True,
|
| 249 |
show_error=True,
|
| 250 |
-
|
| 251 |
)
|
| 252 |
-
|
| 253 |
-
|
| 254 |
while True:
|
| 255 |
time.sleep(3600)
|
| 256 |
|
| 257 |
-
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
+
import re
|
| 5 |
+
import shutil
|
| 6 |
+
import subprocess
|
| 7 |
+
import time
|
| 8 |
+
import traceback
|
| 9 |
from typing import Optional, Tuple
|
| 10 |
|
| 11 |
import gradio as gr
|
|
|
|
| 26 |
|
| 27 |
# -----------------------------------------------------------------------------
|
| 28 |
# (OPTIONAL) Cache model về Google Drive khi chạy Colab
|
|
|
|
|
|
|
| 29 |
# -----------------------------------------------------------------------------
|
| 30 |
# os.environ["HF_HOME"] = "/content/drive/MyDrive/hf_cache"
|
| 31 |
# os.environ["TRANSFORMERS_CACHE"] = os.path.join(os.environ["HF_HOME"], "transformers") # legacy (không cần)
|
| 32 |
# os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
|
| 33 |
|
|
|
|
| 34 |
# -----------------------------------------------------------------------------
|
| 35 |
# Model loading (global singleton)
|
| 36 |
# -----------------------------------------------------------------------------
|
|
|
|
| 136 |
height: int,
|
| 137 |
width: int,
|
| 138 |
) -> Tuple[Image.Image, int, str]:
|
|
|
|
| 139 |
if image is None:
|
| 140 |
raise gr.Error("Please upload an image.")
|
| 141 |
|
|
|
|
| 149 |
return image, seed, prompt
|
| 150 |
|
| 151 |
pipe = get_pipe()
|
| 152 |
+
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 153 |
|
| 154 |
out = pipe(
|
| 155 |
image=[image.convert("RGB")],
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
# -----------------------------------------------------------------------------
|
| 169 |
+
# UI (Gradio 6.3.0 compatible)
|
| 170 |
# -----------------------------------------------------------------------------
|
| 171 |
CAMERA_3D_HTML_TEMPLATE = """
|
| 172 |
<div style="width:100%;height:400px;background:#1a1a1a;border-radius:12px;
|
| 173 |
display:flex;align-items:center;justify-content:center;color:#aaa;">
|
| 174 |
<div style="text-align:center;">
|
| 175 |
<div style="font-size:18px;color:#00ff88;font-weight:700;">3D Camera Preview</div>
|
| 176 |
+
<div style="margin-top:8px;">(Gradio 6) JS ↔ Python sync removed</div>
|
| 177 |
<div style="margin-top:6px;">Use sliders to control camera</div>
|
| 178 |
</div>
|
| 179 |
</div>
|
|
|
|
| 183 |
#camera-3d-control { min-height: 400px; }
|
| 184 |
"""
|
| 185 |
|
| 186 |
+
|
| 187 |
+
with gr.Blocks(css=css) as demo:
|
| 188 |
+
gr.Markdown("## 🎬 Qwen Image Edit — Camera Angle Control (Gradio 6.3.0)")
|
| 189 |
|
| 190 |
with gr.Row():
|
| 191 |
with gr.Column(scale=1):
|
| 192 |
image = gr.Image(label="Input Image", type="pil", height=280)
|
| 193 |
|
| 194 |
with gr.Tab("🎮 3D Camera Preview"):
|
| 195 |
+
_ = gr.HTML(value=CAMERA_3D_HTML_TEMPLATE, elem_id="camera-3d-control")
|
| 196 |
|
| 197 |
with gr.Tab("🎚️ Controls"):
|
| 198 |
rotate_deg = gr.Slider(-90, 90, step=45, label="Rotate (°)", value=0)
|
|
|
|
| 232 |
],
|
| 233 |
outputs=[result, seed, prompt_preview],
|
| 234 |
api_name="edit_camera_angles",
|
|
|
|
| 235 |
)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
| 237 |
if __name__ == "__main__":
|
| 238 |
+
demo.queue(max_size=16)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
demo.launch(
|
| 240 |
server_name="127.0.0.1",
|
| 241 |
+
server_port=7861,
|
| 242 |
+
share=False, # <- quan trọng
|
| 243 |
debug=True,
|
| 244 |
show_error=True,
|
| 245 |
+
prevent_thread_lock=True, # để không block cell (nếu bạn chạy bằng python app.py)
|
| 246 |
)
|
| 247 |
+
# giữ process sống nếu chạy bằng python app.py
|
| 248 |
+
import time
|
| 249 |
while True:
|
| 250 |
time.sleep(3600)
|
| 251 |
|
|
|