Spaces:
Sleeping
Sleeping
Commit
·
cd7c5fe
1
Parent(s):
579e033
updated app.py
Browse files- app.py +3 -3
- demo.ipynb +0 -134
app.py
CHANGED
|
@@ -27,7 +27,7 @@ def answer_question(image, question):
|
|
| 27 |
if image is None:
|
| 28 |
return "Error: Please upload an image."
|
| 29 |
|
| 30 |
-
# Convert NumPy array to PIL Image
|
| 31 |
try:
|
| 32 |
if isinstance(image, np.ndarray):
|
| 33 |
image = Image.fromarray(image)
|
|
@@ -49,14 +49,14 @@ def answer_question(image, question):
|
|
| 49 |
},
|
| 50 |
]
|
| 51 |
|
| 52 |
-
# Apply chat template
|
| 53 |
try:
|
| 54 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 55 |
inputs = processor(text=prompt, images=[image], return_tensors="pt").to(DEVICE)
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error: Failed to prepare inputs. {str(e)}"
|
| 58 |
|
| 59 |
-
# Generate
|
| 60 |
try:
|
| 61 |
outputs = model.generate(**inputs, max_new_tokens=400)
|
| 62 |
answer = processor.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 27 |
if image is None:
|
| 28 |
return "Error: Please upload an image."
|
| 29 |
|
| 30 |
+
# Convert NumPy array to PIL Image
|
| 31 |
try:
|
| 32 |
if isinstance(image, np.ndarray):
|
| 33 |
image = Image.fromarray(image)
|
|
|
|
| 49 |
},
|
| 50 |
]
|
| 51 |
|
| 52 |
+
# Apply chat template and prepare inputs
|
| 53 |
try:
|
| 54 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 55 |
inputs = processor(text=prompt, images=[image], return_tensors="pt").to(DEVICE)
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error: Failed to prepare inputs. {str(e)}"
|
| 58 |
|
| 59 |
+
# Generate answer
|
| 60 |
try:
|
| 61 |
outputs = model.generate(**inputs, max_new_tokens=400)
|
| 62 |
answer = processor.decode(outputs[0], skip_special_tokens=True)
|
demo.ipynb
DELETED
|
@@ -1,134 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "code",
|
| 5 |
-
"execution_count": 2,
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"outputs": [
|
| 8 |
-
{
|
| 9 |
-
"name": "stderr",
|
| 10 |
-
"output_type": "stream",
|
| 11 |
-
"text": [
|
| 12 |
-
"/home/kask/miniconda3/envs/innovatie-week/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 13 |
-
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 14 |
-
]
|
| 15 |
-
}
|
| 16 |
-
],
|
| 17 |
-
"source": [
|
| 18 |
-
"from transformers import AutoProcessor, AutoModelForVision2Seq\n",
|
| 19 |
-
"from transformers.image_utils import load_image\n",
|
| 20 |
-
"import numpy as np\n",
|
| 21 |
-
"import gradio as gr\n",
|
| 22 |
-
"import torch\n",
|
| 23 |
-
"from PIL import Image"
|
| 24 |
-
]
|
| 25 |
-
},
|
| 26 |
-
{
|
| 27 |
-
"cell_type": "code",
|
| 28 |
-
"execution_count": 3,
|
| 29 |
-
"metadata": {},
|
| 30 |
-
"outputs": [
|
| 31 |
-
{
|
| 32 |
-
"name": "stdout",
|
| 33 |
-
"output_type": "stream",
|
| 34 |
-
"text": [
|
| 35 |
-
"cpu\n"
|
| 36 |
-
]
|
| 37 |
-
}
|
| 38 |
-
],
|
| 39 |
-
"source": [
|
| 40 |
-
"# Set the device (GPU or CPU)\n",
|
| 41 |
-
"DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 42 |
-
"print(DEVICE)"
|
| 43 |
-
]
|
| 44 |
-
},
|
| 45 |
-
{
|
| 46 |
-
"cell_type": "code",
|
| 47 |
-
"execution_count": 4,
|
| 48 |
-
"metadata": {},
|
| 49 |
-
"outputs": [
|
| 50 |
-
{
|
| 51 |
-
"name": "stderr",
|
| 52 |
-
"output_type": "stream",
|
| 53 |
-
"text": [
|
| 54 |
-
"Some kwargs in processor config are unused and will not have any effect: image_seq_len. \n"
|
| 55 |
-
]
|
| 56 |
-
}
|
| 57 |
-
],
|
| 58 |
-
"source": [
|
| 59 |
-
"# Initialize processor and model\n",
|
| 60 |
-
"try:\n",
|
| 61 |
-
" processor = AutoProcessor.from_pretrained(\"HuggingFaceTB/SmolVLM-Instruct\")\n",
|
| 62 |
-
" model = AutoModelForVision2Seq.from_pretrained(\n",
|
| 63 |
-
" \"HuggingFaceTB/SmolVLM-Instruct\",\n",
|
| 64 |
-
" torch_dtype=torch.bfloat16,\n",
|
| 65 |
-
" _attn_implementation=\"flash_attention_2\" if DEVICE == \"cuda\" else \"eager\",).to(DEVICE)\n",
|
| 66 |
-
"except Exception as e:\n",
|
| 67 |
-
" print(f\"Error loading model or processor: {str(e)}\")\n",
|
| 68 |
-
" exit(1)"
|
| 69 |
-
]
|
| 70 |
-
},
|
| 71 |
-
{
|
| 72 |
-
"cell_type": "code",
|
| 73 |
-
"execution_count": null,
|
| 74 |
-
"metadata": {},
|
| 75 |
-
"outputs": [
|
| 76 |
-
{
|
| 77 |
-
"name": "stderr",
|
| 78 |
-
"output_type": "stream",
|
| 79 |
-
"text": [
|
| 80 |
-
"/home/kask/miniconda3/envs/innovatie-week/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 81 |
-
" from .autonotebook import tqdm as notebook_tqdm\n",
|
| 82 |
-
"Some kwargs in processor config are unused and will not have any effect: image_seq_len. \n"
|
| 83 |
-
]
|
| 84 |
-
},
|
| 85 |
-
{
|
| 86 |
-
"ename": "",
|
| 87 |
-
"evalue": "",
|
| 88 |
-
"output_type": "error",
|
| 89 |
-
"traceback": [
|
| 90 |
-
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
|
| 91 |
-
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
|
| 92 |
-
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
|
| 93 |
-
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
|
| 94 |
-
]
|
| 95 |
-
}
|
| 96 |
-
],
|
| 97 |
-
"source": [
|
| 98 |
-
"from transformers import AutoProcessor, AutoModelForVision2Seq\n",
|
| 99 |
-
"import tqdm as notebook_tqdm\n",
|
| 100 |
-
"\n",
|
| 101 |
-
"# Define the model name\n",
|
| 102 |
-
"model_name = \"HuggingFaceTB/SmolVLM-Instruct\"\n",
|
| 103 |
-
"\n",
|
| 104 |
-
"# Download and save the processor and model locally\n",
|
| 105 |
-
"processor = AutoProcessor.from_pretrained(model_name)\n",
|
| 106 |
-
"processor.save_pretrained(\"./local_model\")\n",
|
| 107 |
-
"\n",
|
| 108 |
-
"model = AutoModelForVision2Seq.from_pretrained(model_name)\n",
|
| 109 |
-
"model.save_pretrained(\"./local_model\")"
|
| 110 |
-
]
|
| 111 |
-
}
|
| 112 |
-
],
|
| 113 |
-
"metadata": {
|
| 114 |
-
"kernelspec": {
|
| 115 |
-
"display_name": "innovatie-week",
|
| 116 |
-
"language": "python",
|
| 117 |
-
"name": "python3"
|
| 118 |
-
},
|
| 119 |
-
"language_info": {
|
| 120 |
-
"codemirror_mode": {
|
| 121 |
-
"name": "ipython",
|
| 122 |
-
"version": 3
|
| 123 |
-
},
|
| 124 |
-
"file_extension": ".py",
|
| 125 |
-
"mimetype": "text/x-python",
|
| 126 |
-
"name": "python",
|
| 127 |
-
"nbconvert_exporter": "python",
|
| 128 |
-
"pygments_lexer": "ipython3",
|
| 129 |
-
"version": "3.12.1"
|
| 130 |
-
}
|
| 131 |
-
},
|
| 132 |
-
"nbformat": 4,
|
| 133 |
-
"nbformat_minor": 2
|
| 134 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|