Spaces:
Running
Running
import gradio as gr | |
import requests, base64, io | |
from dotenv import load_dotenv | |
import os, datetime, mimetypes | |
from PIL import Image | |
load_dotenv() | |
API_URL = os.environ.get("API_URL", "http://0.0.0.0:8021") | |
APP_ENV = os.environ.get("APP_ENV", None) | |
def image_to_base64(input_file): | |
file_bytes = None | |
file_path = None | |
if input_file is None: | |
return None | |
# Check if the input is a string (file path) | |
if isinstance(input_file, str): | |
file_path = input_file | |
with open(input_file, "rb") as file: | |
file_bytes = file.read() | |
else: | |
# Handle file-like object (_TemporaryFileWrapper) | |
file_path = input_file.name | |
file_bytes = input_file.read() | |
# Guess the MIME type of the file based on the file extension | |
mime_type, _ = mimetypes.guess_type(file_path) | |
if mime_type is None: | |
mime_type = 'application/octet-stream' # Use a binary data MIME type as a fallback | |
data_url = f"data:{mime_type};base64,{base64.b64encode(file_bytes).decode()}" | |
return data_url | |
# Assuming the API returns an image URL in the response | |
def generate_image(brand_name, primary_color, secondary_color, description, target_audience, font, logo_image, heading_text, sub_heading_text, image_layout, custom_graphic_prompt): | |
if logo_image: | |
logo_base64 = image_to_base64(logo_image) | |
else: | |
logo_base64 = "" | |
# Define your payload/data to send to the image generation API | |
data = { | |
"brand_name": brand_name, | |
"primary_color": primary_color, | |
"secondary_color": secondary_color, | |
"description": description, | |
"target_audience": target_audience, | |
"font": font, | |
"heading_text": heading_text or "", | |
"sub_heading_text": sub_heading_text or "", | |
"logo_url": logo_base64, | |
"user_prompt": custom_graphic_prompt, | |
"image_dimension": image_layout | |
} | |
# Make the API call | |
response = requests.post(API_URL, json=data) | |
# Ensure the API call was successful | |
if response.status_code != 200: | |
print(f"Error {response.status_code}: {response.text}") | |
return "Error: Unable to fetch image from the external API." | |
# Create the directory if it doesn't exist | |
image_directory = "images" | |
if not os.path.exists(image_directory): | |
os.makedirs(image_directory) | |
image_path = f"images/temp_image_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}.png" | |
with open(image_path, "wb") as image_file: | |
image_file.write(response.content) | |
return image_path | |
content_html = """ | |
<div style="text-align: center; margin-top: 30px;"> | |
<h2>Slidegen Examples</h2> | |
<div style="display: flex; justify-content: space-around; align-items: center;"> | |
<!-- Displaying one video --> | |
<video controls width="30%"> | |
<source src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/IxuuqPhmIX3Jggqkeiseb.mp4" type="video/mp4"> | |
Your browser does not support the video tag. | |
</video> | |
<!-- Displaying two images --> | |
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/f4xH6ZNx1rVbKzr8jAHmt.png" alt="Image Description 1" style="width: 30%; margin: 0 10px;"> | |
<img src="https://cdn-uploads.huggingface.co/production/uploads/644252f59a5bbb07ab7a987a/A9BoWW2ulZycv0OQaihKO.png" alt="Image Description 2" style="width: 30%; margin: 0 10px;"> | |
</div> | |
</div> | |
""" | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.components.Textbox(placeholder="Brand/Company Name", label="Brand Name"), | |
gr.components.ColorPicker(value="#FFFFFF", label="Primary Color"), | |
gr.components.ColorPicker(value="#05A592", label="Secondary Color"), | |
gr.components.Textbox(placeholder="Brief description of your brand and design guidelines", label="Description", lines=2), | |
gr.components.Textbox(placeholder="Your Brand's target audience", label="Target Audience"), | |
gr.components.Textbox(placeholder="Google Font Label i.e Rubik", label="Font"), | |
gr.components.File(label="Logo Image", scale=2, file_types=["jpg", "jpeg", "png", "gif", "bmp"]), | |
gr.components.Textbox(placeholder="Heading text (not required)", label="Heading Text"), | |
gr.components.Textbox(placeholder="Paragraph text (not required)", label="Sub Heading Text"), | |
gr.components.Dropdown( | |
choices=[ | |
("Select Layout", "750x1334"), | |
("Mobile Portrait (750x1334)", "750x1334"), | |
("Mobile Landscape (940x470)", "940x470"), | |
("Square (2048x2048)", "2048x2048") | |
], | |
value="750x1334", | |
label="Image Layout" | |
), | |
gr.components.Textbox(lines=4, placeholder="Enter your custom dall-e 2-3 image prompt here (not required)", value="", label="Custom Graphic Prompt") | |
], | |
outputs=[ | |
gr.components.Image(label="Generated Image") | |
], | |
title="Slidegen AI - Image generator", | |
article=content_html, | |
description="Generate social media creatives from a few prompts", | |
live=False | |
) | |
# iface.queue() | |
# Run the interface | |
#trigger | |
iface.launch() | |