Chat_bot_Rag/utils/display.py

41 lines
1.3 KiB
Python

from PIL import Image
from io import BytesIO
import base64
def base64_to_image(base64_data):
"""Convert base64 image data to PIL Image"""
try:
if not base64_data:
return None
image_bytes = base64.b64decode(base64_data)
return Image.open(BytesIO(image_bytes))
except Exception as e:
print(f"Image conversion error: {e}")
return None
def display_images(current_images):
"""Format images for Gradio gallery display"""
if not current_images:
return None
return [
(img["image"], f"{img['caption']} (Source: {img['source']}, Page: {img['page']})")
for img in current_images
if img.get("image")
]
def display_tables(current_tables):
"""Format tables for HTML display"""
if not current_tables:
return None
html = ""
for table in current_tables:
html += f"""
<div style="margin-bottom: 20px; border: 1px solid #ddd; padding: 15px; border-radius: 8px;">
<h3>{table['caption']}</h3>
<p style="color:#666; font-size:0.9em;">Source: {table['source']}, Page: {table['page']}</p>
<div class="table-container">{table.get('data', '')}</div>
</div>
"""
return html if html else None