import spaces import marqo import requests import io from PIL import Image import gradio as gr import os from dotenv import load_dotenv load_dotenv() # Initialize Marqo client (for local deployment) # mq = marqo.Client("http://localhost:8882", api_key=None) # Initialize Marqo client (for Marqo Cloud deployment) api_key = os.getenv("MARQO_API_KEY") mq = marqo.Client("https://api.marqo.ai", api_key=api_key) def search_marqo(query, themes, negatives): # Build query weights query_weights = {query: 1.0} if themes: query_weights[themes] = 0.75 if negatives: query_weights[negatives] = -1.1 # Perform search with Marqo res = mq.index("marqo-ecommerce-b").search(query_weights, limit=10) # limit to top 10 results # Prepare results products = [] for hit in res['hits']: image_url = hit.get('image_url') title = hit.get('title', 'No Title') description = hit.get('description', 'No Description') price = hit.get('price', 'N/A') score = hit['_score'] # Fetch the image from the URL response = requests.get(image_url) image = Image.open(io.BytesIO(response.content)) # Append product details for Gradio display product_info = f'{title}\n{description}\nPrice: {price}\nScore: {score:.4f}' products.append((image, product_info)) return products # Function to clear inputs and results def clear_inputs(): return "", "", [], [] # Gradio Blocks Interface for Custom Layout with gr.Blocks(css=".orange-button { background-color: orange; color: black; }") as interface: gr.Markdown("