Spaces:
Sleeping
Sleeping
vidhanm
commited on
Commit
·
b0a4224
1
Parent(s):
7d56bcc
updated parameters name in call_generate_script
Browse files
app.py
CHANGED
|
@@ -22,109 +22,108 @@ MODEL_REPO_ID = "lusxvr/nanoVLM-222M"
|
|
| 22 |
print(f"DEBUG: Using generate.py script at: {GENERATE_SCRIPT_PATH}")
|
| 23 |
print(f"DEBUG: Using model repo ID: {MODEL_REPO_ID}")
|
| 24 |
|
|
|
|
|
|
|
| 25 |
def call_generate_script(image_path: str, prompt_text: str) -> str:
|
| 26 |
print(f"\n--- DEBUG (call_generate_script) ---")
|
| 27 |
print(f"Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
| 28 |
print(f"Calling with image_path='{image_path}', prompt='{prompt_text}'")
|
| 29 |
|
| 30 |
-
# Arguments for nanoVLM's generate.py
|
| 31 |
-
# Using low max_new_tokens for CPU testing.
|
| 32 |
cmd_args = [
|
| 33 |
-
"python", "-u", GENERATE_SCRIPT_PATH,
|
| 34 |
"--hf_model", MODEL_REPO_ID,
|
| 35 |
-
"--image_path", image_path, #
|
| 36 |
"--prompt", prompt_text,
|
| 37 |
-
"--num_samples", "1", #
|
| 38 |
-
"--max_new_tokens", "30", #
|
| 39 |
-
"--device", "cpu" #
|
| 40 |
-
# Optional args for generate.py:
|
| 41 |
# "--temperature", "0.7",
|
| 42 |
-
# "--top_k", "
|
| 43 |
]
|
| 44 |
|
| 45 |
print(f"Executing command: {' '.join(cmd_args)}")
|
| 46 |
|
| 47 |
-
# Realistic timeout for the subprocess. HF Spaces free tier usually times out requests around 60s.
|
| 48 |
-
# Set this shorter to catch issues within app.py.
|
| 49 |
SCRIPT_TIMEOUT_SECONDS = 55
|
| 50 |
start_time = time.time()
|
| 51 |
|
| 52 |
-
|
| 53 |
try:
|
| 54 |
process = subprocess.run(
|
| 55 |
cmd_args,
|
| 56 |
capture_output=True,
|
| 57 |
text=True,
|
| 58 |
-
check=False,
|
| 59 |
timeout=SCRIPT_TIMEOUT_SECONDS
|
| 60 |
)
|
| 61 |
-
process_details = f"PID {process.pid if hasattr(process, 'pid') else 'N/A'}"
|
| 62 |
|
| 63 |
duration = time.time() - start_time
|
| 64 |
-
print(f"Subprocess ({
|
| 65 |
print(f"generate.py RETURN CODE: {process.returncode}")
|
| 66 |
|
| 67 |
stdout = process.stdout.strip() if process.stdout else "[No STDOUT from generate.py]"
|
| 68 |
stderr = process.stderr.strip() if process.stderr else "[No STDERR from generate.py]"
|
| 69 |
|
| 70 |
-
print(f"---------- generate.py STDOUT ({
|
| 71 |
if stderr or process.returncode != 0:
|
| 72 |
-
print(f"---------- generate.py STDERR ({
|
| 73 |
|
| 74 |
if process.returncode != 0:
|
| 75 |
error_message = f"Error: Generation script failed (code {process.returncode})."
|
| 76 |
-
if "
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
# --- Parse the output from nanoVLM's generate.py ---
|
| 81 |
-
#
|
| 82 |
-
# Outputs:
|
| 83 |
# > Sample 1: <generated text>
|
| 84 |
output_lines = stdout.splitlines()
|
| 85 |
-
generated_text = "[No parsable output from generate.py]"
|
| 86 |
|
| 87 |
found_output_line = False
|
| 88 |
for line_idx, line in enumerate(output_lines):
|
| 89 |
stripped_line = line.strip()
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
print(f"Parsed generated text: '{generated_text}'")
|
| 101 |
-
break
|
| 102 |
|
| 103 |
if not found_output_line:
|
| 104 |
-
print(f"Could not find 'Sample 1'
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
print(f"Returning parsed text: '{generated_text}'")
|
| 110 |
return generated_text
|
| 111 |
|
| 112 |
except subprocess.TimeoutExpired as e:
|
| 113 |
duration = time.time() - start_time
|
| 114 |
-
print(f"ERROR: generate.py ({
|
| 115 |
-
stdout_on_timeout = e.stdout.strip() if e.stdout else "[No STDOUT on timeout]"
|
| 116 |
-
stderr_on_timeout = e.stderr.strip() if e.stderr else "[No STDERR on timeout]"
|
| 117 |
print(f"STDOUT on timeout:\n{stdout_on_timeout}")
|
| 118 |
print(f"STDERR on timeout:\n{stderr_on_timeout}")
|
| 119 |
return f"Error: Generation script timed out after {SCRIPT_TIMEOUT_SECONDS}s. Model loading and generation may be too slow for CPU."
|
| 120 |
except Exception as e:
|
| 121 |
duration = time.time() - start_time
|
| 122 |
-
print(f"ERROR: An unexpected error occurred ({
|
| 123 |
import traceback; traceback.print_exc()
|
| 124 |
return f"Unexpected error calling script: {str(e)}"
|
| 125 |
finally:
|
| 126 |
print(f"--- END (call_generate_script) ---")
|
| 127 |
|
|
|
|
|
|
|
| 128 |
|
| 129 |
def gradio_interface_fn(image_input_pil: Optional[PILImage.Image], prompt_input_str: Optional[str]) -> str:
|
| 130 |
print(f"\nDEBUG (gradio_interface_fn): Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
| 22 |
print(f"DEBUG: Using generate.py script at: {GENERATE_SCRIPT_PATH}")
|
| 23 |
print(f"DEBUG: Using model repo ID: {MODEL_REPO_ID}")
|
| 24 |
|
| 25 |
+
# In app.py
|
| 26 |
+
|
| 27 |
def call_generate_script(image_path: str, prompt_text: str) -> str:
|
| 28 |
print(f"\n--- DEBUG (call_generate_script) ---")
|
| 29 |
print(f"Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
| 30 |
print(f"Calling with image_path='{image_path}', prompt='{prompt_text}'")
|
| 31 |
|
| 32 |
+
# Arguments for nanoVLM's generate.py, VERIFIED against its source code
|
|
|
|
| 33 |
cmd_args = [
|
| 34 |
+
"python", "-u", GENERATE_SCRIPT_PATH,
|
| 35 |
"--hf_model", MODEL_REPO_ID,
|
| 36 |
+
"--image_path", image_path, # VERIFIED: script expects --image_path
|
| 37 |
"--prompt", prompt_text,
|
| 38 |
+
"--num_samples", "1", # VERIFIED: script expects --num_samples
|
| 39 |
+
"--max_new_tokens", "30", # This was correct
|
| 40 |
+
"--device", "cpu" # VERIFIED: script expects --device
|
| 41 |
+
# Optional args for generate.py that you can add if needed:
|
| 42 |
# "--temperature", "0.7",
|
| 43 |
+
# "--top_k", "200" # Default is 200 in script
|
| 44 |
]
|
| 45 |
|
| 46 |
print(f"Executing command: {' '.join(cmd_args)}")
|
| 47 |
|
|
|
|
|
|
|
| 48 |
SCRIPT_TIMEOUT_SECONDS = 55
|
| 49 |
start_time = time.time()
|
| 50 |
|
| 51 |
+
process_identifier = "generate.py_process"
|
| 52 |
try:
|
| 53 |
process = subprocess.run(
|
| 54 |
cmd_args,
|
| 55 |
capture_output=True,
|
| 56 |
text=True,
|
| 57 |
+
check=False,
|
| 58 |
timeout=SCRIPT_TIMEOUT_SECONDS
|
| 59 |
)
|
|
|
|
| 60 |
|
| 61 |
duration = time.time() - start_time
|
| 62 |
+
print(f"Subprocess ({process_identifier}) finished in {duration:.2f} seconds.")
|
| 63 |
print(f"generate.py RETURN CODE: {process.returncode}")
|
| 64 |
|
| 65 |
stdout = process.stdout.strip() if process.stdout else "[No STDOUT from generate.py]"
|
| 66 |
stderr = process.stderr.strip() if process.stderr else "[No STDERR from generate.py]"
|
| 67 |
|
| 68 |
+
print(f"---------- generate.py STDOUT ({process_identifier}) START ----------\n{stdout}\n---------- generate.py STDOUT ({process_identifier}) END ----------")
|
| 69 |
if stderr or process.returncode != 0:
|
| 70 |
+
print(f"---------- generate.py STDERR ({process_identifier}) START ----------\n{stderr}\n---------- generate.py STDERR ({process_identifier}) END ----------")
|
| 71 |
|
| 72 |
if process.returncode != 0:
|
| 73 |
error_message = f"Error: Generation script failed (code {process.returncode})."
|
| 74 |
+
if "unrecognized arguments" in stderr:
|
| 75 |
+
error_message += " Argument mismatch with script."
|
| 76 |
+
print(error_message)
|
| 77 |
+
return error_message + f" STDERR Snippet: {stderr[:300]}" # Show more stderr
|
| 78 |
|
| 79 |
# --- Parse the output from nanoVLM's generate.py ---
|
| 80 |
+
# The original nanoVLM generate.py prints:
|
|
|
|
| 81 |
# > Sample 1: <generated text>
|
| 82 |
output_lines = stdout.splitlines()
|
| 83 |
+
generated_text = "[No parsable output from generate.py]"
|
| 84 |
|
| 85 |
found_output_line = False
|
| 86 |
for line_idx, line in enumerate(output_lines):
|
| 87 |
stripped_line = line.strip()
|
| 88 |
+
# The actual generate.py from nanoVLM prints "> Sample 1:"
|
| 89 |
+
prefix_to_remove = None
|
| 90 |
+
if stripped_line.startswith("> Sample 1:"):
|
| 91 |
+
prefix_to_remove = "> Sample 1:"
|
| 92 |
+
|
| 93 |
+
if prefix_to_remove:
|
| 94 |
+
generated_text = stripped_line.replace(prefix_to_remove, "", 1).strip()
|
| 95 |
+
found_output_line = True
|
| 96 |
+
print(f"Parsed generated text: '{generated_text}'")
|
| 97 |
+
break
|
|
|
|
|
|
|
| 98 |
|
| 99 |
if not found_output_line:
|
| 100 |
+
print(f"Could not find '> Sample 1:' line in generate.py output. Raw STDOUT was:\n{stdout}")
|
| 101 |
+
if stdout:
|
| 102 |
+
generated_text = f"[Parsing failed] STDOUT: {stdout[:500]}"
|
| 103 |
+
else:
|
| 104 |
+
generated_text = "[Parsing failed, no STDOUT from script]"
|
| 105 |
|
| 106 |
print(f"Returning parsed text: '{generated_text}'")
|
| 107 |
return generated_text
|
| 108 |
|
| 109 |
except subprocess.TimeoutExpired as e:
|
| 110 |
duration = time.time() - start_time
|
| 111 |
+
print(f"ERROR: generate.py ({process_identifier}) timed out after {duration:.2f} seconds (limit: {SCRIPT_TIMEOUT_SECONDS}s).")
|
| 112 |
+
stdout_on_timeout = e.stdout.strip() if hasattr(e, 'stdout') and e.stdout else "[No STDOUT on timeout]"
|
| 113 |
+
stderr_on_timeout = e.stderr.strip() if hasattr(e, 'stderr') and e.stderr else "[No STDERR on timeout]"
|
| 114 |
print(f"STDOUT on timeout:\n{stdout_on_timeout}")
|
| 115 |
print(f"STDERR on timeout:\n{stderr_on_timeout}")
|
| 116 |
return f"Error: Generation script timed out after {SCRIPT_TIMEOUT_SECONDS}s. Model loading and generation may be too slow for CPU."
|
| 117 |
except Exception as e:
|
| 118 |
duration = time.time() - start_time
|
| 119 |
+
print(f"ERROR: An unexpected error occurred ({process_identifier}) after {duration:.2f}s: {type(e).__name__} - {e}")
|
| 120 |
import traceback; traceback.print_exc()
|
| 121 |
return f"Unexpected error calling script: {str(e)}"
|
| 122 |
finally:
|
| 123 |
print(f"--- END (call_generate_script) ---")
|
| 124 |
|
| 125 |
+
# The rest of your app.py (gradio_interface_fn, Gradio Interface Definition, __main__ block)
|
| 126 |
+
# should remain the same.
|
| 127 |
|
| 128 |
def gradio_interface_fn(image_input_pil: Optional[PILImage.Image], prompt_input_str: Optional[str]) -> str:
|
| 129 |
print(f"\nDEBUG (gradio_interface_fn): Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
|