Commit
·
cbe84b9
1
Parent(s):
f712aee
sss
Browse files
app.py
CHANGED
|
@@ -1,18 +1,15 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
|
| 6 |
def process_images(images, prompt):
|
| 7 |
"""
|
| 8 |
-
Process multiple images
|
| 9 |
-
|
| 10 |
-
Args:
|
| 11 |
-
images: List of uploaded images
|
| 12 |
-
prompt: User-provided prompt
|
| 13 |
-
|
| 14 |
-
Returns:
|
| 15 |
-
List of generated descriptions
|
| 16 |
"""
|
| 17 |
if not images:
|
| 18 |
return "Please upload at least one image."
|
|
@@ -24,17 +21,31 @@ def process_images(images, prompt):
|
|
| 24 |
continue
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
response = qwen_model(
|
| 30 |
-
prompt, # Text prompt
|
| 31 |
-
image # Image file
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
results.append(f"Image {i+1}: ❌ Error - {str(e)}")
|
| 40 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import base64
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Initialize the inference client
|
| 8 |
+
client = InferenceClient()
|
| 9 |
|
| 10 |
def process_images(images, prompt):
|
| 11 |
"""
|
| 12 |
+
Process multiple images using Hugging Face Hub Inference Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
if not images:
|
| 15 |
return "Please upload at least one image."
|
|
|
|
| 21 |
continue
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# Convert numpy array to PIL Image
|
| 25 |
+
pil_image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Encode image to base64
|
| 28 |
+
buffered = io.BytesIO()
|
| 29 |
+
pil_image.save(buffered, format="JPEG")
|
| 30 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 31 |
+
base64_image = f"data:image/jpeg;base64,{img_str}"
|
| 32 |
+
|
| 33 |
+
# Use the inference client to generate response
|
| 34 |
+
response = client.post(
|
| 35 |
+
"Qwen/Qwen2-VL-7B-Instruct",
|
| 36 |
+
inputs={
|
| 37 |
+
"text": prompt,
|
| 38 |
+
"image": base64_image
|
| 39 |
+
}
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# Extract the response
|
| 43 |
+
if response and len(response) > 0:
|
| 44 |
+
description = response[0]
|
| 45 |
+
results.append(f"Image {i+1}: {description}")
|
| 46 |
+
else:
|
| 47 |
+
results.append(f"Image {i+1}: ❌ No response from model")
|
| 48 |
+
|
| 49 |
except Exception as e:
|
| 50 |
results.append(f"Image {i+1}: ❌ Error - {str(e)}")
|
| 51 |
|