Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 2 |
-
#
|
| 3 |
# This space is created by SANJOG GHONGE for testing and learning purpose.
|
| 4 |
#
|
| 5 |
# If you want to remove this space or credits please contact me on my email id [ghongesanjog@gmail.com].
|
|
@@ -24,14 +24,25 @@
|
|
| 24 |
#
|
| 25 |
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 26 |
|
| 27 |
-
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
| 28 |
from qwen_vl_utils import process_vision_info
|
| 29 |
import gradio as gr
|
| 30 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Load the model and processor
|
| 33 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 34 |
-
"Qwen/QVQ-72B-Preview",
|
| 35 |
)
|
| 36 |
processor = AutoProcessor.from_pretrained("Qwen/QVQ-72B-Preview")
|
| 37 |
|
|
@@ -83,7 +94,7 @@ def process_image_and_question(image, question):
|
|
| 83 |
|
| 84 |
# Define the Gradio interface
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
-
gr.Markdown("# Sanjog Image and Question Answering\nProvide an image (JPG/PNG) and a related question to get an answer.")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column():
|
|
@@ -105,51 +116,3 @@ with gr.Blocks() as demo:
|
|
| 105 |
demo.launch()
|
| 106 |
|
| 107 |
|
| 108 |
-
# ------------------------------------------------------------------------------------------------------------------------------------
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# import gradio as gr
|
| 113 |
-
# from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 114 |
-
|
| 115 |
-
# # Load the processor and model
|
| 116 |
-
# model_name = "Qwen/QVQ-72B-Preview"
|
| 117 |
-
# processor = AutoProcessor.from_pretrained(model_name)
|
| 118 |
-
# model = AutoModelForImageTextToText.from_pretrained(model_name)
|
| 119 |
-
|
| 120 |
-
# # Define the prediction function
|
| 121 |
-
# def process_image_and_question(image, question):
|
| 122 |
-
# if image is None or not question:
|
| 123 |
-
# return "Please provide both an image and a question."
|
| 124 |
-
|
| 125 |
-
# # Process the inputs
|
| 126 |
-
# inputs = processor(images=image, text=question, return_tensors="pt")
|
| 127 |
-
|
| 128 |
-
# # Generate the output
|
| 129 |
-
# outputs = model.generate(**inputs)
|
| 130 |
-
# answer = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 131 |
-
|
| 132 |
-
# return answer
|
| 133 |
-
|
| 134 |
-
# # Define the Gradio interface
|
| 135 |
-
# with gr.Blocks() as demo:
|
| 136 |
-
# gr.Markdown("# Image and Question Answering\nProvide an image (JPG/PNG) and a related question to get an answer.")
|
| 137 |
-
|
| 138 |
-
# with gr.Row():
|
| 139 |
-
# with gr.Column():
|
| 140 |
-
# image_input = gr.Image(type="pil", label="Upload Image (JPG/PNG)")
|
| 141 |
-
# question_input = gr.Textbox(label="Enter your question")
|
| 142 |
-
|
| 143 |
-
# with gr.Column():
|
| 144 |
-
# output_box = gr.Textbox(label="Result", interactive=False)
|
| 145 |
-
|
| 146 |
-
# with gr.Row():
|
| 147 |
-
# clear_button = gr.Button("Clear")
|
| 148 |
-
# submit_button = gr.Button("Submit")
|
| 149 |
-
|
| 150 |
-
# # Define button functionality
|
| 151 |
-
# clear_button.click(lambda: (None, "", ""), inputs=[], outputs=[image_input, question_input, output_box])
|
| 152 |
-
# submit_button.click(process_image_and_question, inputs=[image_input, question_input], outputs=output_box)
|
| 153 |
-
|
| 154 |
-
# # Launch the interface
|
| 155 |
-
# demo.launch()
|
|
|
|
| 1 |
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 2 |
+
#
|
| 3 |
# This space is created by SANJOG GHONGE for testing and learning purpose.
|
| 4 |
#
|
| 5 |
# If you want to remove this space or credits please contact me on my email id [ghongesanjog@gmail.com].
|
|
|
|
| 24 |
#
|
| 25 |
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 26 |
|
| 27 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
|
| 28 |
from qwen_vl_utils import process_vision_info
|
| 29 |
import gradio as gr
|
| 30 |
from PIL import Image
|
| 31 |
+
import torch
|
| 32 |
+
print(torch.cuda.memory_summary())
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Create a configuration for quantization
|
| 36 |
+
quantization_config = BitsAndBytesConfig(
|
| 37 |
+
load_in_4bit=True, # Set to True for 4-bit quantization
|
| 38 |
+
bnb_4bit_compute_dtype="float16", # Use float16 for faster computations
|
| 39 |
+
bnb_4bit_use_double_quant=True, # Optional: Double quantization for memory savings
|
| 40 |
+
bnb_4bit_quant_type="nf4", # NormalFloat4 (nf4) is better for performance
|
| 41 |
+
)
|
| 42 |
|
| 43 |
# Load the model and processor
|
| 44 |
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 45 |
+
"Qwen/QVQ-72B-Preview", device_map="auto", quantization_config=quantization_config,
|
| 46 |
)
|
| 47 |
processor = AutoProcessor.from_pretrained("Qwen/QVQ-72B-Preview")
|
| 48 |
|
|
|
|
| 94 |
|
| 95 |
# Define the Gradio interface
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("# Sanjog Test : Image and Question Answering\nProvide an image (JPG/PNG) and a related question to get an answer.")
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column():
|
|
|
|
| 116 |
demo.launch()
|
| 117 |
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|