import gradio as gr import pandas as pd import numpy as np from typing import List, Dict import os # Create static directory if it doesn't exist if not os.path.exists('static'): os.makedirs('static') # Move thumbs directory into static if it exists if os.path.exists('thumbs'): import shutil if not os.path.exists('static/thumbs'): shutil.move('thumbs', 'static/') else: # If static/thumbs already exists, merge the contents for file in os.listdir('thumbs'): shutil.move(os.path.join('thumbs', file), 'static/thumbs/') os.rmdir('thumbs') class CSVViewer: def __init__(self, csv_path: str): self.df = pd.read_csv(csv_path) self.current_page = 0 self.rows_per_page = 1 self.total_pages = len(self.df) def get_page_data(self, page_num: int) -> Dict: if page_num < 0 or page_num >= self.total_pages: return None row = self.df.iloc[page_num] # Get product information product_info = { 'Web link': str(row['Web link']), 'Product Code': str(row['Product Code']), 'Product Name': str(row['Product Name']) } # Separate original and AI-predicted columns original_data = {} ai_data = {} for col in self.df.columns: if 'Score' in col or 'Distance' in col or col == '': continue # Skip score and distance columns if col in ['Web link', 'Product Code', 'Product Name']: continue # Skip these columns as they're shown at the top if col in ["Length", "Style", "Sleeve Length", "Neckline", "Pattern", "Fabric", "Features", "Closure", "Body Fit", "Occasion", "Season"]: original_data[col] = str(row[col]) if col.endswith('_AI'): ai_data[col] = str(row[col]) return { 'product_info': product_info, 'original_data': original_data, 'ai_data': ai_data, 'page_num': page_num + 1, 'total_pages': self.total_pages } def create_page_view(page_num: int): viewer = CSVViewer('attr.csv') page_data = viewer.get_page_data(page_num) if not page_data: return "Invalid page number" # Get product image product_code = page_data['product_info']['Product Code'] # Find matching image file image_files = [f for f in os.listdir('static/thumbs') if f.startswith(product_code)] if image_files: # Use Gradio's static file serving image_path = f"/static/thumbs/{image_files[0]}" print(f"Found image: {image_path}") else: image_path = None # Create HTML content html = f"

Page {page_data['page_num']} of {page_data['total_pages']}

" html += "
" # Product info section html += "
" if image_path: html += f"Product Image" for key, value in page_data['product_info'].items(): html += f"

{key}: {value}

" html += "
" html += "
" # Comparison section html += "
" html += "

Comparison

" html += "" html += "" # Add comparison rows for key, orig_value in page_data['original_data'].items(): # Find matching AI prediction ai_key = key + '_AI' ai_value = page_data['ai_data'].get(ai_key, '') # Skip if AI value is empty if not ai_value.strip(): continue # Clean up values by removing brackets and quotes orig_value = str(orig_value).strip('[]').strip('"').strip() ai_value = str(ai_value).strip('[]').strip('"').strip() # Handle array values try: # Try to parse as arrays orig_items = [item.strip() for item in orig_value.split(',') if item.strip()] ai_items = [item.strip() for item in ai_value.split(',') if item.strip()] # Check if all original items are in AI items if all(item.lower() in [x.lower() for x in ai_items] for item in orig_items): if len(ai_items) > len(orig_items): status = "✓ All and more" status_color = "#4CAF50" row_color = "#e8f5e9" else: status = "✓ Match" status_color = "#4CAF50" row_color = "#e8f5e9" else: status = "✗ Mismatch" status_color = "#f44336" row_color = "#ffebee" # Format the values nicely orig_display = ', '.join(orig_items) ai_display = ', '.join(ai_items) except: # Handle non-array values if orig_value.lower() == ai_value.lower(): status = "✓ Match" status_color = "#4CAF50" row_color = "#e8f5e9" orig_display = orig_value ai_display = ai_value else: status = "✗ Mismatch" status_color = "#f44336" row_color = "#ffebee" orig_display = orig_value ai_display = ai_value # Add row with comparison html += f"" html += f"" html += f"" html += f"" html += f"" html += "" html += "
AttributeOriginalAI PredictionStatus
{key}{orig_display}{ai_display}{status}
" # Original data section html += "
" html += "

Original Data

" html += "" for key, value in page_data['original_data'].items(): html += f"" html += "
{key}{value}
" # AI data section html += "
" html += "

AI Predicted Data

" html += "" for key, value in page_data['ai_data'].items(): html += f"" html += "
{key}{value}
" html += "
" return html # Create Gradio interface demo = gr.Blocks() with demo: gr.Markdown("# CSV Data Viewer") gr.Markdown("Navigate through CSV data pages to compare original and AI-predicted values") page_num = gr.Slider(0, 100, step=1, label="Page Number") html_output = gr.HTML() page_num.change(create_page_view, [page_num], [html_output]) # Launch Gradio interface demo.launch(server_name="0.0.0.0", server_port=7860, share=True)