rayochoajr's picture
Create app.py
9d560e2
import gradio as gr
import random
from random import choice
import requests
import base64
import io
from PIL import Image
import plotly.graph_objects as go
import numpy as np
from PIL import Image, ImageDraw
import random
import re
url = "http://73.255.78.150:7909/sdapi/v1/txt2img"
session_seed = None
last_session_seed = None
# Predefined arrays for design parameters and their corresponding values
ALL_TITLES = [
'Minimalism', 'Bauhaus', 'Organic Design', 'Brutalism', 'Mid-century Modern',
'Retro-Vintage', 'Futurism', 'Tesselated', 'Streamlined', 'Timeless',
'Industrial Chic', 'Art Deco', 'Elegant', 'Biomorphic Design', 'High Contrast',
'Deconstructivism', 'Zen Design', 'Pop Art', 'Cyberpunk', 'Sustainable Design',
'Angular', 'Textured', 'Symmetric', 'Utilitarian', 'Dynamic Energy Flow'
]
ALL_VALUES = [
1.5, 1.2, 0.8, 1.0, 0.5, -1.0, 0.3, -0.5, 1.1, -0.7,
0.9, 1.3, 0.2, -1.3, -0.9, -1.1, 0.7, 0.6, -0.8, 0.4,
-0.2, 0.1, -1.2, 0.0, -0.4
]
# Base payload for API call
base_payload = {
"steps": 20,
"seed": 1,
"width": 768,
"height": 512,
}
# New function to generate radar chart using Plotly
def generate_figure(r_values, theta_values, chart_title):
fig = go.Figure(data=go.Scatterpolar(
r=r_values, theta=theta_values, mode='lines+markers',
marker=dict(size=10), fill='toself')
)
fig.update_layout(
polar=dict(radialaxis=dict(visible=True, range=[0, 1.5])),
showlegend=False, title=chart_title
)
return fig
# New function for generating the radar chart
def display_radar_chart(input_parameters, randomize_values, randomize_titles, randomize_param_count, chart_title, param_count, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10):
if randomize_param_count:
param_count = random.randint(3, 10)
# πŸ€– Create a temporary array from comma-separated input_parameters
if input_parameters:
parameters = re.split(r'\s*,\s*', input_parameters.strip())
if len(parameters) > param_count:
parameters = parameters[:param_count]
elif randomize_titles:
parameters = random.sample(ALL_TITLES, param_count)
else:
parameters = []
# πŸ€– Fill in missing parameters based on param_count
if len(parameters) < param_count:
remaining_count = param_count - len(parameters)
if randomize_titles:
additional_params = random.sample(set(ALL_TITLES) - set(parameters), remaining_count)
else:
additional_params = ALL_TITLES[len(parameters):len(parameters) + remaining_count]
parameters.extend(additional_params)
elif len(parameters) > param_count:
parameters = parameters[:param_count]
# Existing code for randomizing values remains the same
if randomize_values:
r_values = random.sample(ALL_VALUES, len(parameters))
else:
r_values = [param1, param2, param3, param4, param5, param6, param7, param8, param9, param10][:len(parameters)]
chart_figure = generate_figure(r_values, parameters, chart_title)
slider_labels = ", ".join([f"{parameters[i]}: {r_values[i]:.2f}" for i in range(len(r_values))])
return chart_figure, slider_labels
def generate_image(prompt, steps, reset_seed, increment_seed):
global session_seed
global last_session_seed
if reset_seed:
if session_seed != -1:
last_session_seed = session_seed
session_seed = -1
elif not reset_seed and last_session_seed is not None:
session_seed = last_session_seed
elif session_seed is None:
session_seed = random.randint(0, 10000)
if increment_seed and session_seed != -1:
session_seed += 1
last_session_seed = session_seed
payload = {
"prompt": prompt,
"steps": steps,
"seed": session_seed,
"height": 768,
"width": 1024,
}
response = requests.post(url, json=payload)
response_data = response.json()
try:
image = Image.open(io.BytesIO(base64.b64decode(response_data['images'][0])))
return image
except KeyError:
error_message = response_data.get('error', 'Unknown error')
error_image = Image.new('RGB', (512, 512), color=(73, 109, 137))
d = ImageDraw.Draw(error_image)
d.text((10, 10), f"Error: {error_message}", fill=(255, 255, 0))
return error_image
image_list = [f"Images/Image {i}.png" for i in range(1, 19)]
def update_moodboard(btn_value):
return {moodboard_image: choice(image_list)}
with gr.Blocks() as app:
gr.Markdown("# Magazine Layouts")
with gr.Tab("Dynamic Dashboard"):
with gr.Row():
gr.Textbox(lines=1, value="Dashboard: Dynamic View")
gr.Dropdown(options=["Overview", "Details", "Summary"], label="Dashboard Mode")
with gr.Row():
gr.Textbox(lines=1, value="Status: Active")
gr.Slider(minimum=0, maximum=100, label="Dashboard Progress")
# Image Generation Section
with gr.Column():
gr.Markdown("### Image Generation")
prompt_input = gr.Textbox(lines=2, placeholder="Imagine something...", label="Prompt")
steps_input = gr.Slider(minimum=15, maximum=50, step=15, default=5, label="Steps")
reset_seed_input = gr.Checkbox(label="Randomize Composition")
increment_seed_input = gr.Checkbox(label="New Composition")
img_output = gr.Image(label="Generated Image")
# Radar Chart Section
with gr.Column():
gr.Markdown("### Radar Chart")
input_parameters = gr.Textbox(placeholder="Enter parameters, separated by commas", label="Parameters")
randomize_values = gr.Checkbox(label="Randomize Values")
chart_title = gr.Textbox(value="Radar Chart", label="Chart Title")
param_count = gr.Slider(minimum=3, maximum=10, step=1, label="Parameter Count")
radar_output = gr.Plot(label="Radar Chart")
with gr.Row():
gr.Image(choice(image_list))
gr.Textbox(lines=4, value="Dashboard Insights: Real-time updates and metrics")
gr.Button("Refresh Dashboard", label="Refresh Dashboard")
with gr.Tab("API Call Interface"):
with gr.Blocks() as main_blocks:
with gr.Row():
with gr.Column():
img_output = gr.Image(label="Generated Image")
with gr.Row():
with gr.Column(scale=1):
prompt_input = gr.Textbox(lines=2, placeholder="Imagine something...", label="Prompt")
steps_input = gr.Slider(minimum=15, maximum=50, step=15, default=5, label="Steps")
with gr.Column(scale=1):
reset_seed_input = gr.Checkbox(label="Randomize Composition")
increment_seed_input = gr.Checkbox(label="New Composition")
iface = gr.Interface(
fn=generate_image,
inputs=[prompt_input, steps_input, reset_seed_input, increment_seed_input],
outputs=[img_output],
live=False,
layout=main_blocks
)
# Design Proposal Board with Multi-layered Layouts
with gr.Tab("Design Proposal Board"):
with gr.Row():
gr.Textbox(lines=1, value="Project: Redesign UI")
gr.Dropdown(options=["Planning", "Execution", "Review"], label="Project Stage")
with gr.Row():
gr.Textbox(lines=1, value="Timeline: 3 months")
gr.Slider(minimum=0, maximum=100, label="Progress")
gr.Image(choice(image_list))
gr.Button("View Milestones", label="View Milestones")
gr.Textbox(lines=4, value="Goals: Improve user engagement by 20%")
with gr.Tab("The Minimalist Layout"):
with gr.Column():
gr.Markdown("## Minimalist")
gr.Image("Images/Image 1.png")
gr.Textbox(lines=4, value="It was a stormy night, and the vintage car roared down the highway.")
with gr.Tab("The Grid Layout"):
with gr.Row():
with gr.Column(scale=1):
gr.Image("Images/Image 2.png")
gr.Textbox(lines=4, value="The old leather seats had stories to tell.")
with gr.Column(scale=1):
gr.Image("Images/Image 3.png")
gr.Textbox(lines=4, value="With every curve and turn, the car seemed to whisper.")
with gr.Tab("The Asymmetrical Layout"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("## Artistic Layout")
gr.Image("Images/Image 4.png")
with gr.Column(scale=1):
gr.Image("Images/Image 5.png")
gr.Textbox(lines=3, value="As the dawn broke, the car finally stopped.")
with gr.Tab("The F-Layout"):
with gr.Row():
with gr.Column(scale=3):
gr.Image("Images/Image 6.png")
with gr.Column(scale=1):
gr.Textbox(lines=4, value="It was a stormy night, and the vintage car roared down the highway.")
with gr.Tab("The Radial Layout"):
with gr.Row():
with gr.Column(scale=1):
gr.Image("Images/Image 9.png")
with gr.Column(scale=1):
gr.Image("Images/Image 10.png")
with gr.Row():
with gr.Column(scale=1):
gr.Image("Images/Image 11.png")
with gr.Column(scale=1):
gr.Textbox(lines=4, value="As the dawn broke, the car finally stopped.")
with gr.Column(scale=1):
gr.Image("Images/Image 12.png")
with gr.Tab("Trading Card Layout 1"):
with gr.Column():
gr.Image(choice(image_list))
gr.Textbox(lines=1, value="Car Model: Mustang")
gr.Textbox(lines=1, value="Year: 1965")
gr.Textbox(lines=2, value="Stats: Speed 200mph, Mileage 15mpg")
gr.Textbox(lines=3, value="The Mustang is a classic American muscle car.")
# Museum Art Gallery: Spotlight Layout
with gr.Tab("Spotlight Layout"):
gr.Image(choice(image_list))
with gr.Row():
gr.Textbox(lines=1, value="Art: Moonlit Sonata")
gr.Textbox(lines=1, value="Artist: L. Vinci")
gr.Textbox(lines=1, value="Year: 1911")
gr.Textbox(lines=3, value="A single spotlight shines on this masterpiece, illuminating its intricate details and vivid colors.")
# Museum Art Gallery: Interactive Kiosk Layout
with gr.Tab("Interactive Kiosk Layout"):
with gr.Row():
with gr.Column(scale=1):
gr.Image(choice(image_list))
with gr.Column(scale=1):
gr.Textbox(lines=1, value="Art: Pixelated Reality")
gr.Textbox(lines=1, value="Artist: G. O'Keeffe")
gr.Textbox(lines=1, value="Year: 2001")
gr.Textbox(lines=4, value="Step up to the interactive kiosk to dive deeper into the story and significance of the artwork.")
# Moodboard for a Designer
with gr.Tab("Moodboard for a Designer"):
with gr.Row():
gr.Image(choice(image_list))
gr.Textbox(lines=1, value="Color: #FF5733")
gr.Textbox(lines=1, value="Font: Arial")
with gr.Row():
gr.Textbox(lines=1, value="Quote: Design is intelligence made visible.")
gr.Image(choice(image_list))
gr.Textbox(lines=1, value="Project: Logo Design")
# Comic Book with Dynamic Content Loading
with gr.Tab("Comic Book"):
with gr.Row():
gr.Textbox(lines=1, value="Title: The Adventures of Grado")
gr.Image(choice(image_list))
with gr.Row():
gr.Textbox(lines=1, value="BOOM!")
gr.Image(choice(image_list))
gr.Textbox(lines=2, value="Character: Oh no!")
gr.Button("Next Page", label="Next Page")
gr.Textbox(lines=1, value="Caption: To be continued...")
with gr.Tab("Radar Chart"):
with gr.Row():
input_parameters = gr.Textbox(placeholder="Enter parameters, separated by commas", label="Parameters")
randomize_values = gr.Checkbox(label="Randomize Values")
randomize_titles = gr.Checkbox(label="Randomize Titles")
randomize_param_count = gr.Checkbox(label="Randomize Parameter Count")
chart_title = gr.Textbox(value="Radar Chart", label="Chart Title")
param_count = gr.Slider(minimum=3, maximum=10, step=1, label="Parameter Count")
param1 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 1")
param2 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 2")
param3 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 3")
param4 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 4")
param5 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 5")
param6 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 6")
param7 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 7")
param8 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 8")
param9 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 9")
param10 = gr.Slider(minimum=-1.5, maximum=1.5, step=0.1, label="Parameter 10")
radar_output = gr.Plot(label="Radar Chart")
slider_labels_output = gr.Textbox(label="Prompt: Slider Labels")
radar_interface = gr.Interface(
fn=display_radar_chart,
inputs=[input_parameters, randomize_values, randomize_titles, randomize_param_count, chart_title, param_count, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10], # Include param6 to param10
outputs=[radar_output, slider_labels_output]
)
app.launch()