Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from utils import generate_response
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def describe_image(image):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Define chatbot interaction function
|
| 11 |
def chat(user_input, chat_history, image):
|
|
@@ -13,7 +28,7 @@ def chat(user_input, chat_history, image):
|
|
| 13 |
# Generate text response
|
| 14 |
response = generate_response(user_input)
|
| 15 |
|
| 16 |
-
# If an image is uploaded, describe it
|
| 17 |
if image is not None:
|
| 18 |
image_description = describe_image(image)
|
| 19 |
response += f"\n\n[Image Description]: {image_description}"
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from utils import generate_response
|
| 3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Load the BLIP image captioning model and processor
|
| 8 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 9 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 10 |
+
|
| 11 |
+
# Function to generate description for an image using BLIP
|
| 12 |
def describe_image(image):
|
| 13 |
+
try:
|
| 14 |
+
# Convert the image to the format the model expects
|
| 15 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
# Generate a caption for the image
|
| 18 |
+
out = model.generate(**inputs)
|
| 19 |
+
description = processor.decode(out[0], skip_special_tokens=True)
|
| 20 |
+
|
| 21 |
+
return description
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error describing the image: {e}"
|
| 24 |
|
| 25 |
# Define chatbot interaction function
|
| 26 |
def chat(user_input, chat_history, image):
|
|
|
|
| 28 |
# Generate text response
|
| 29 |
response = generate_response(user_input)
|
| 30 |
|
| 31 |
+
# If an image is uploaded, describe it using the image captioning model
|
| 32 |
if image is not None:
|
| 33 |
image_description = describe_image(image)
|
| 34 |
response += f"\n\n[Image Description]: {image_description}"
|