Hug0endob commited on
Commit
e4bf697
·
verified ·
1 Parent(s): e76c937

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -39
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
- # 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.")
@@ -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 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."):
 
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."):