To create a Python chatbot using Gradio that displays an image gallery with each product's image, title, and link, you can follow the code snippet below. This code assumes that you have a list of product data with the image name, image title, and image URL.
import gradio
# Sample product data
products = [
{"name": "Product 1", "title": "Title 1", "image_url": "Image URL 1"},
{"name": "Product 2", "title": "Title 2", "image_url": "Image URL 2"},
{"name": "Product 3", "title": "Title 3", "image_url": "Image URL 3"},
{"name": "Product 4", "title": "Title 4", "image_url": "Image URL 4"},
{"name": "Product 5", "title": "Title 5", "image_url": "Image URL 5"}
]
# Function to create a clickable image with title and link
def create_image_card(product):
with gradio.HorizontalLayout():
with gradio.Image(source=product["image_url"], width=200) + gradio.Spacer(10)
gradio.Text(product["title"], size=18, weight="bold") + gradio.Spacer(10)
gradio.Button(f"Open in new tab ({product['title']})", url=product["image_url"])
# Create the chatbot interface
gradio.UI(
title="Image Gallery Chatbot",
components=[
gradio.Text("Click on the image to view the product details"),
gradio.Spacer(),
gradio.Text("Product 1", size=18, weight="bold"),
create_image_card(products),
gradio.Text("Product 2", size=18, weight="bold"),
create_image_card(products),
gradio.Text("Product 3", size=18, weight="bold"),
create_image_card(products),
gradio.Text("Product 4", size=18, weight="bold"),
create_image_card(products),
gradio.Text("Product 5", size=18, weight="bold"),
create_image_card(products),
]
)
This code creates a chatbot interface with an image gallery for each product. The create_image_card function takes a product dictionary as input and creates a horizontal layout with an image, title, and a link to the image. The chatbot interface uses the Gradio library to display the image gallery and handle user interactions.