Spaces:
Sleeping
Sleeping
Mahdi Naser Moghadasi commited on
Commit ·
ab01353
1
Parent(s): 47262e1
Implement file-based image storage for better Gradio compatibility
Browse files- Save generated images to files instead of using base64 in HTML
- Use file paths in img src instead of data URLs
- Create generated_images directory for image storage
- Add fallback to base64 if file saving fails
- Should resolve image display issues in Gradio interface
1743 unmodified lines17441745174617471748174917501743 unmodified linesprint(f"🖼️ Image data length: {len(image_data)} characters")print(f"🖼️ Image data preview: {image_data[:100]}...")return f'''<div style="margin: 20px 0; text-align: center;"><img src="{image_data}"alt="{safe_description}"1743 unmodified lines17441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891743 unmodified linesprint(f"🖼️ Image data length: {len(image_data)} characters")print(f"🖼️ Image data preview: {image_data[:100]}...")# Save image to file and use file path instead of base64import osimport base64# Create images directory if it doesn't existos.makedirs("generated_images", exist_ok=True)# Extract base64 dataif 'base64,' in image_data:base64_data = image_data.split('base64,')[1]else:base64_data = image_data# Save image to fileimage_filename = f"image_{hash(safe_description) % 10000}.png"image_path = f"generated_images/{image_filename}"try:with open(image_path, "wb") as f:f.write(base64.b64decode(base64_data))# Use file path instead of base64return f'''<div style="margin: 20px 0; text-align: center;"><img src="{image_path}"alt="{safe_description}"style="max-width: 100%; height: auto; border-radius: 8px; margin: 20px 0;"loading="lazy"onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"><div style="display: none; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 8px; color: white; font-style: italic;"><div style="font-size: 24px; margin-bottom: 10px;">🖼️</div><div style="font-weight: bold; margin-bottom: 5px;">{safe_description}</div><div style="font-size: 14px; opacity: 0.8;">Educational Visual Content</div></div></div>'''except Exception as e:print(f"❌ Error saving image: {str(e)}")# Fallback to base64 if file saving failsreturn f'''<div style="margin: 20px 0; text-align: center;"><img src="{image_data}"alt="{safe_description}"
app.py+40-1