Commit ·
ff05d0c
1
Parent(s): cf3c4b2
update
Browse files- __pycache__/model.cpython-310.pyc +0 -0
- __pycache__/vlm_inference.cpython-310.pyc +0 -0
- app.py +69 -69
__pycache__/model.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/model.cpython-310.pyc and b/__pycache__/model.cpython-310.pyc differ
|
|
|
__pycache__/vlm_inference.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/vlm_inference.cpython-310.pyc and b/__pycache__/vlm_inference.cpython-310.pyc differ
|
|
|
app.py
CHANGED
|
@@ -10,44 +10,83 @@ from vlm_inference import (
|
|
| 10 |
)
|
| 11 |
|
| 12 |
# =====================================================
|
| 13 |
-
# Load
|
| 14 |
# =====================================================
|
|
|
|
| 15 |
model = load_vlm_model()
|
| 16 |
model.eval()
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
# =====================================================
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# =====================================================
|
| 22 |
@spaces.GPU
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
temperature,
|
| 27 |
top_p,
|
| 28 |
top_k,
|
| 29 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if image is None:
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
|
| 34 |
device = "cuda"
|
|
|
|
| 35 |
model_gpu = model.to(device)
|
| 36 |
|
| 37 |
-
|
| 38 |
image_tensor = image_processor(
|
| 39 |
images=image.convert("RGB"),
|
| 40 |
return_tensors="pt"
|
| 41 |
)["pixel_values"].to(device)
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
prompt = (
|
| 45 |
"<user>\n"
|
| 46 |
f"{text}\n"
|
| 47 |
"<assistant>\n"
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
for chunk in vlm_infer_stream(
|
| 52 |
model=model_gpu,
|
| 53 |
image_tensor=image_tensor,
|
|
@@ -57,72 +96,33 @@ def infer_once(
|
|
| 57 |
top_p=top_p if top_p > 0 else None,
|
| 58 |
top_k=top_k if top_k > 0 else None,
|
| 59 |
):
|
|
|
|
| 60 |
yield chunk
|
| 61 |
-
|
|
|
|
| 62 |
model_gpu.to("cpu")
|
| 63 |
torch.cuda.empty_cache()
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
# =====================================================
|
| 67 |
-
# UI logic (history is display-only)
|
| 68 |
-
# =====================================================
|
| 69 |
-
def submit(
|
| 70 |
-
image,
|
| 71 |
-
text,
|
| 72 |
-
history,
|
| 73 |
-
temperature,
|
| 74 |
-
top_p,
|
| 75 |
-
top_k,
|
| 76 |
-
):
|
| 77 |
-
history = history or []
|
| 78 |
-
history.append((text, ""))
|
| 79 |
-
|
| 80 |
-
def stream():
|
| 81 |
-
acc = ""
|
| 82 |
-
for chunk in infer_once(image, text, temperature, top_p, top_k):
|
| 83 |
-
acc += chunk
|
| 84 |
-
history[-1] = (text, acc)
|
| 85 |
-
yield history
|
| 86 |
-
|
| 87 |
-
return history, stream()
|
| 88 |
|
| 89 |
|
| 90 |
# =====================================================
|
| 91 |
-
#
|
| 92 |
# =====================================================
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
top_p = gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 107 |
-
top_k = gr.Slider(0, 200, value=0, step=1, label="Top-k")
|
| 108 |
-
|
| 109 |
-
submit_btn = gr.Button("Run")
|
| 110 |
-
|
| 111 |
-
with gr.Column(scale=1):
|
| 112 |
-
chatbot = gr.Chatbot(label="Output (history is display-only)")
|
| 113 |
-
state = gr.State([])
|
| 114 |
-
|
| 115 |
-
submit_btn.click(
|
| 116 |
-
fn=submit,
|
| 117 |
-
inputs=[
|
| 118 |
-
image_input,
|
| 119 |
-
text_input,
|
| 120 |
-
state,
|
| 121 |
-
temperature,
|
| 122 |
-
top_p,
|
| 123 |
-
top_k,
|
| 124 |
-
],
|
| 125 |
-
outputs=[chatbot, chatbot],
|
| 126 |
-
)
|
| 127 |
|
|
|
|
| 128 |
demo.launch()
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
# =====================================================
|
| 13 |
+
# Load VLM on CPU (ZeroGPU)
|
| 14 |
# =====================================================
|
| 15 |
+
print("[DEBUG] Loading VLM model on CPU...")
|
| 16 |
model = load_vlm_model()
|
| 17 |
model.eval()
|
| 18 |
+
print("[DEBUG] VLM model loaded.")
|
| 19 |
|
| 20 |
|
| 21 |
# =====================================================
|
| 22 |
+
# message parser (multimodal=True 仕様準拠)
|
| 23 |
+
# =====================================================
|
| 24 |
+
def parse_message(message: dict):
|
| 25 |
+
"""
|
| 26 |
+
message = {
|
| 27 |
+
"text": str,
|
| 28 |
+
"files": list # PIL.Image が入る
|
| 29 |
+
}
|
| 30 |
+
"""
|
| 31 |
+
print("[DEBUG] parse_message called")
|
| 32 |
+
print("[DEBUG] message type:", type(message))
|
| 33 |
+
print("[DEBUG] message content:", message)
|
| 34 |
+
|
| 35 |
+
text = message.get("text", "")
|
| 36 |
+
files = message.get("files", [])
|
| 37 |
+
|
| 38 |
+
print("[DEBUG] parsed text:", repr(text))
|
| 39 |
+
print("[DEBUG] parsed files:", files)
|
| 40 |
+
|
| 41 |
+
image = files[0] if files else None
|
| 42 |
+
print("[DEBUG] parsed image:", image)
|
| 43 |
+
|
| 44 |
+
return text, image
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# =====================================================
|
| 48 |
+
# GPU inference (single-turn, VLM only)
|
| 49 |
# =====================================================
|
| 50 |
@spaces.GPU
|
| 51 |
+
def chat_fn(
|
| 52 |
+
message,
|
| 53 |
+
history, # unused (single-turn)
|
| 54 |
temperature,
|
| 55 |
top_p,
|
| 56 |
top_k,
|
| 57 |
):
|
| 58 |
+
print("[DEBUG] chat_fn called")
|
| 59 |
+
print("[DEBUG] temperature:", temperature, "top_p:", top_p, "top_k:", top_k)
|
| 60 |
+
|
| 61 |
+
text, image = parse_message(message)
|
| 62 |
+
|
| 63 |
if image is None:
|
| 64 |
+
print("[DEBUG] image is None -> returning error message")
|
| 65 |
+
return "Image input is required."
|
| 66 |
|
| 67 |
device = "cuda"
|
| 68 |
+
print("[DEBUG] moving model to GPU")
|
| 69 |
model_gpu = model.to(device)
|
| 70 |
|
| 71 |
+
print("[DEBUG] preprocessing image")
|
| 72 |
image_tensor = image_processor(
|
| 73 |
images=image.convert("RGB"),
|
| 74 |
return_tensors="pt"
|
| 75 |
)["pixel_values"].to(device)
|
| 76 |
|
| 77 |
+
print("[DEBUG] image_tensor shape:", image_tensor.shape)
|
| 78 |
+
|
| 79 |
prompt = (
|
| 80 |
"<user>\n"
|
| 81 |
f"{text}\n"
|
| 82 |
"<assistant>\n"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
print("[DEBUG] prompt:")
|
| 86 |
+
print(prompt)
|
| 87 |
+
|
| 88 |
+
def stream():
|
| 89 |
+
print("[DEBUG] stream generator started")
|
| 90 |
for chunk in vlm_infer_stream(
|
| 91 |
model=model_gpu,
|
| 92 |
image_tensor=image_tensor,
|
|
|
|
| 96 |
top_p=top_p if top_p > 0 else None,
|
| 97 |
top_k=top_k if top_k > 0 else None,
|
| 98 |
):
|
| 99 |
+
print("[DEBUG] yield chunk:", repr(chunk))
|
| 100 |
yield chunk
|
| 101 |
+
|
| 102 |
+
print("[DEBUG] inference finished, cleaning up GPU")
|
| 103 |
model_gpu.to("cpu")
|
| 104 |
torch.cuda.empty_cache()
|
| 105 |
+
print("[DEBUG] GPU cleanup done")
|
| 106 |
|
| 107 |
+
return stream()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
# =====================================================
|
| 111 |
+
# UI (ChatInterface, multimodal)
|
| 112 |
# =====================================================
|
| 113 |
+
print("[DEBUG] Building Gradio UI")
|
| 114 |
+
|
| 115 |
+
demo = gr.ChatInterface(
|
| 116 |
+
fn=chat_fn,
|
| 117 |
+
multimodal=True,
|
| 118 |
+
title="EveryonesGPT Vision (VLM only)",
|
| 119 |
+
description="Single-turn Vision-Language Model demo (CLIP ViT-L/14)",
|
| 120 |
+
additional_inputs=[
|
| 121 |
+
gr.Slider(0.1, 2.0, value=0.5, step=0.05, label="Temperature"),
|
| 122 |
+
gr.Slider(0.0, 1.0, value=0.9, step=0.05, label="Top-p"),
|
| 123 |
+
gr.Slider(0, 200, value=0, step=1, label="Top-k"),
|
| 124 |
+
],
|
| 125 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
print("[DEBUG] Launching Gradio app")
|
| 128 |
demo.launch()
|