Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import io
|
|
| 3 |
import time
|
| 4 |
import sys
|
| 5 |
import requests
|
|
|
|
| 6 |
from PIL import Image, ImageSequence
|
| 7 |
import gradio as gr
|
| 8 |
|
|
@@ -12,9 +13,55 @@ try:
|
|
| 12 |
except Exception as e:
|
| 13 |
raise RuntimeError("llama-cpp-python import failed: " + str(e))
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if not os.path.exists(MODEL_PATH):
|
| 17 |
-
raise FileNotFoundError(f"Model not found at {MODEL_PATH}
|
| 18 |
|
| 19 |
def download_bytes(url: str, timeout: int = 30) -> bytes:
|
| 20 |
with requests.get(url, stream=True, timeout=timeout) as r:
|
|
@@ -35,6 +82,7 @@ def make_prompt_for_image(image_path: str, user_prompt: str = "Describe the imag
|
|
| 35 |
|
| 36 |
# Initialize model (low-resource options)
|
| 37 |
print("Loading GGUF model (this can take 30–120s)...", file=sys.stderr)
|
|
|
|
| 38 |
llm = Llama(model_path=MODEL_PATH, n_ctx=2048, n_threads=2)
|
| 39 |
|
| 40 |
def generate_caption_from_url(url: str, prompt: str = "Describe the image."):
|
|
@@ -84,7 +132,7 @@ iface = gr.Interface(
|
|
| 84 |
gr.Textbox(label="Prompt (optional)", value="Describe the image."),
|
| 85 |
],
|
| 86 |
outputs=gr.Textbox(label="Generated caption"),
|
| 87 |
-
title="JoyCaption GGUF (
|
| 88 |
description="Runs a quantized JoyCaption GGUF locally via llama.cpp (no external API).",
|
| 89 |
)
|
| 90 |
|
|
|
|
| 3 |
import time
|
| 4 |
import sys
|
| 5 |
import requests
|
| 6 |
+
import subprocess
|
| 7 |
from PIL import Image, ImageSequence
|
| 8 |
import gradio as gr
|
| 9 |
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
raise RuntimeError("llama-cpp-python import failed: " + str(e))
|
| 15 |
|
| 16 |
+
# Ensure model is present: try Q4_K_M, fall back to Q4_K_S
|
| 17 |
+
def download_with_curl(url: str, path: str) -> bool:
|
| 18 |
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 19 |
+
try:
|
| 20 |
+
subprocess.check_call(["curl", "-L", "-C", "-", "-o", path, url])
|
| 21 |
+
return True
|
| 22 |
+
except Exception:
|
| 23 |
+
if os.path.exists(path):
|
| 24 |
+
try:
|
| 25 |
+
os.remove(path)
|
| 26 |
+
except Exception:
|
| 27 |
+
pass
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
def ensure_model() -> str:
|
| 31 |
+
target = os.path.join("model", "llama-joycaption-q4_k_m.gguf")
|
| 32 |
+
alt = os.path.join("model", "llama-joycaption-q4_k_s.gguf")
|
| 33 |
+
if os.path.exists(target):
|
| 34 |
+
return target
|
| 35 |
+
# URLs (update if upstream changes)
|
| 36 |
+
url_m = "https://huggingface.co/mradermacher/llama-joycaption-beta-one-hf-llava-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_m.gguf"
|
| 37 |
+
url_s = "https://huggingface.co/mradermacher/llama-joycaption-beta-one-hf-llava-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_s.gguf"
|
| 38 |
+
sys.stderr.write("Model not found locally, attempting download (this can be several GB)...\n")
|
| 39 |
+
# Try M first
|
| 40 |
+
if download_with_curl(url_m, target):
|
| 41 |
+
sys.stderr.write("Downloaded Q4_K_M model.\n")
|
| 42 |
+
return target
|
| 43 |
+
# Try S
|
| 44 |
+
if download_with_curl(url_s, alt):
|
| 45 |
+
# create symlink so code expecting the M filename still works
|
| 46 |
+
try:
|
| 47 |
+
if os.path.exists(target):
|
| 48 |
+
os.remove(target)
|
| 49 |
+
os.symlink(os.path.basename(alt), target)
|
| 50 |
+
except Exception:
|
| 51 |
+
# fallback: copy file
|
| 52 |
+
try:
|
| 53 |
+
import shutil
|
| 54 |
+
shutil.copyfile(alt, target)
|
| 55 |
+
except Exception:
|
| 56 |
+
pass
|
| 57 |
+
sys.stderr.write("Downloaded Q4_K_S model and linked as Q4_K_M filename.\n")
|
| 58 |
+
return target
|
| 59 |
+
raise FileNotFoundError("Failed to download both Q4_K_M and Q4_K_S GGUF models. Check logs and URLs.")
|
| 60 |
+
|
| 61 |
+
# Ensure model is available before loading llama.cpp
|
| 62 |
+
MODEL_PATH = ensure_model()
|
| 63 |
if not os.path.exists(MODEL_PATH):
|
| 64 |
+
raise FileNotFoundError(f"Model not found at {MODEL_PATH} after download attempt.")
|
| 65 |
|
| 66 |
def download_bytes(url: str, timeout: int = 30) -> bytes:
|
| 67 |
with requests.get(url, stream=True, timeout=timeout) as r:
|
|
|
|
| 82 |
|
| 83 |
# Initialize model (low-resource options)
|
| 84 |
print("Loading GGUF model (this can take 30–120s)...", file=sys.stderr)
|
| 85 |
+
# Adjust n_threads as appropriate for the Space CPU available
|
| 86 |
llm = Llama(model_path=MODEL_PATH, n_ctx=2048, n_threads=2)
|
| 87 |
|
| 88 |
def generate_caption_from_url(url: str, prompt: str = "Describe the image."):
|
|
|
|
| 132 |
gr.Textbox(label="Prompt (optional)", value="Describe the image."),
|
| 133 |
],
|
| 134 |
outputs=gr.Textbox(label="Generated caption"),
|
| 135 |
+
title="JoyCaption GGUF (Q4_K)",
|
| 136 |
description="Runs a quantized JoyCaption GGUF locally via llama.cpp (no external API).",
|
| 137 |
)
|
| 138 |
|