Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,8 @@ import os
|
|
| 2 |
import io
|
| 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,52 +13,89 @@ try:
|
|
| 13 |
except Exception as e:
|
| 14 |
raise RuntimeError("llama-cpp-python import failed: " + str(e))
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 24 |
-
|
| 25 |
os.remove(path)
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return False
|
| 29 |
|
| 30 |
def ensure_model() -> str:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|
|
@@ -82,7 +119,7 @@ def make_prompt_for_image(image_path: str, user_prompt: str = "Describe the imag
|
|
| 82 |
|
| 83 |
# Initialize model (low-resource options)
|
| 84 |
print("Loading GGUF model (this can take 30–120s)...", file=sys.stderr)
|
| 85 |
-
# Adjust n_threads
|
| 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."):
|
|
|
|
| 2 |
import io
|
| 3 |
import time
|
| 4 |
import sys
|
|
|
|
| 5 |
import subprocess
|
| 6 |
+
import requests
|
| 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 |
+
MODEL_DIR = "model"
|
| 17 |
+
EXPECTED_TARGET = os.path.join(MODEL_DIR, "llama-joycaption-q4_k_m.gguf")
|
| 18 |
+
|
| 19 |
+
# Candidate direct-download URLs (try in order)
|
| 20 |
+
CANDIDATES = [
|
| 21 |
+
# Jasaga7818 copy (often a direct GGUF)
|
| 22 |
+
("https://huggingface.co/Jasaga7818/llama-joycaption-beta-one-hf-llava-Q4_K_M-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_m.gguf",
|
| 23 |
+
EXPECTED_TARGET),
|
| 24 |
+
# mradermacher (alternate host)
|
| 25 |
+
("https://huggingface.co/mradermacher/llama-joycaption-beta-one-hf-llava-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_m.gguf",
|
| 26 |
+
EXPECTED_TARGET),
|
| 27 |
+
# Fallback to Q4_K_S (Jasaga)
|
| 28 |
+
("https://huggingface.co/Jasaga7818/llama-joycaption-beta-one-hf-llava-Q4_K_M-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_s.gguf",
|
| 29 |
+
os.path.join(MODEL_DIR, "llama-joycaption-q4_k_s.gguf")),
|
| 30 |
+
("https://huggingface.co/mradermacher/llama-joycaption-beta-one-hf-llava-GGUF/resolve/main/llama-joycaption-beta-one-hf-llava-q4_k_s.gguf",
|
| 31 |
+
os.path.join(MODEL_DIR, "llama-joycaption-q4_k_s.gguf")),
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
def download_curl(url: str, path: str) -> bool:
|
| 35 |
os.makedirs(os.path.dirname(path), exist_ok=True)
|
| 36 |
try:
|
| 37 |
+
# Use curl for resume support and progress in logs
|
| 38 |
subprocess.check_call(["curl", "-L", "-C", "-", "-o", path, url])
|
| 39 |
return True
|
| 40 |
except Exception:
|
| 41 |
+
try:
|
| 42 |
+
if os.path.exists(path):
|
| 43 |
os.remove(path)
|
| 44 |
+
except Exception:
|
| 45 |
+
pass
|
| 46 |
+
return False
|
| 47 |
+
|
| 48 |
+
def is_valid_gguf(path: str) -> bool:
|
| 49 |
+
# GGUF files start with "GGUF" in ASCII at offset 0 (0x47 0x47 0x55 0x46).
|
| 50 |
+
# Some converted uploads may be HTML pages or redirects; check header.
|
| 51 |
+
try:
|
| 52 |
+
with open(path, "rb") as f:
|
| 53 |
+
head = f.read(8)
|
| 54 |
+
return head.startswith(b"GGUF")
|
| 55 |
+
except Exception:
|
| 56 |
return False
|
| 57 |
|
| 58 |
def ensure_model() -> str:
|
| 59 |
+
# If already present (and valid), use it.
|
| 60 |
+
if os.path.exists(EXPECTED_TARGET) and is_valid_gguf(EXPECTED_TARGET):
|
| 61 |
+
sys.stderr.write(f"Model already present and valid at {EXPECTED_TARGET}\n")
|
| 62 |
+
return EXPECTED_TARGET
|
| 63 |
+
|
| 64 |
+
sys.stderr.write("Model not found locally or invalid, attempting download (several GB)...\n")
|
| 65 |
+
for url, dest in CANDIDATES:
|
| 66 |
+
sys.stderr.write(f"Attempting download: {url} -> {dest}\n")
|
| 67 |
+
if download_curl(url, dest):
|
| 68 |
+
sys.stderr.write(f"Downloaded candidate to {dest}; verifying header...\n")
|
| 69 |
+
if is_valid_gguf(dest):
|
| 70 |
+
# If candidate wasn't the expected filename, create symlink so rest of code can use EXPECTED_TARGET.
|
| 71 |
+
if os.path.abspath(dest) != os.path.abspath(EXPECTED_TARGET):
|
| 72 |
+
try:
|
| 73 |
+
if os.path.exists(EXPECTED_TARGET):
|
| 74 |
+
os.remove(EXPECTED_TARGET)
|
| 75 |
+
os.symlink(os.path.basename(dest), EXPECTED_TARGET)
|
| 76 |
+
sys.stderr.write(f"Created symlink {EXPECTED_TARGET} -> {os.path.basename(dest)}\n")
|
| 77 |
+
except Exception:
|
| 78 |
+
# fallback: copy
|
| 79 |
+
try:
|
| 80 |
+
import shutil
|
| 81 |
+
shutil.copyfile(dest, EXPECTED_TARGET)
|
| 82 |
+
sys.stderr.write(f"Copied {dest} to {EXPECTED_TARGET}\n")
|
| 83 |
+
except Exception:
|
| 84 |
+
sys.stderr.write("Warning: failed to symlink or copy candidate to expected filename.\n")
|
| 85 |
+
sys.stderr.write("Model verified as GGUF and ready.\n")
|
| 86 |
+
return EXPECTED_TARGET
|
| 87 |
+
else:
|
| 88 |
+
sys.stderr.write("Downloaded file is not a valid GGUF (header mismatch). Removing and trying next.\n")
|
| 89 |
+
try:
|
| 90 |
+
os.remove(dest)
|
| 91 |
+
except Exception:
|
| 92 |
+
pass
|
| 93 |
+
else:
|
| 94 |
+
sys.stderr.write("Download failed for candidate; trying next.\n")
|
| 95 |
+
|
| 96 |
+
raise FileNotFoundError("Failed to download a valid GGUF model from candidates. Check URLs and repo availability.")
|
| 97 |
+
|
| 98 |
+
# Ensure model exists and is a GGUF before importing/initializing Llama
|
| 99 |
MODEL_PATH = ensure_model()
|
| 100 |
if not os.path.exists(MODEL_PATH):
|
| 101 |
raise FileNotFoundError(f"Model not found at {MODEL_PATH} after download attempt.")
|
|
|
|
| 119 |
|
| 120 |
# Initialize model (low-resource options)
|
| 121 |
print("Loading GGUF model (this can take 30–120s)...", file=sys.stderr)
|
| 122 |
+
# Adjust n_threads for the Space CPU; increase if you know you have more cores available.
|
| 123 |
llm = Llama(model_path=MODEL_PATH, n_ctx=2048, n_threads=2)
|
| 124 |
|
| 125 |
def generate_caption_from_url(url: str, prompt: str = "Describe the image."):
|