gaur3009 commited on
Commit
587d018
·
verified ·
1 Parent(s): 88523c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -67
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
  import requests
3
- from PIL import Image, ImageDraw, ImageFont
4
  from io import BytesIO
5
  from tqdm import tqdm
6
- import numpy as np
7
  import time
8
 
9
  repo = "artificialguybr/TshirtDesignRedmond-V2"
@@ -43,70 +42,12 @@ def infer(color_prompt, phone_type_prompt, design_prompt):
43
  else:
44
  raise Exception(f"API Error: {response.status_code}")
45
 
46
- def save_design(image, color_prompt, phone_type_prompt, design_prompt):
47
- # Convert NumPy array to PIL Image
48
- if isinstance(image, np.ndarray):
49
- image = Image.fromarray(image)
50
-
51
- # Add caption
52
- draw = ImageDraw.Draw(image)
53
- caption = f"Color: {color_prompt}\nMobile Type: {phone_type_prompt}\nDesign: {design_prompt}"
54
- font_size = 20
55
-
56
- try:
57
- # Attempt to load a default font
58
- font = ImageFont.truetype("arial.ttf", font_size)
59
- except IOError:
60
- font = ImageFont.load_default()
61
-
62
- # Calculate position for text
63
- text_width, text_height = font.getsize_multiline(caption) # Updated function
64
- position = (10, image.height - text_height - 10) # Bottom-left corner with padding
65
-
66
- # Add caption to the image
67
- draw.multiline_text(position, caption, fill="white", font=font)
68
-
69
- # Save the image
70
- file_path = "saved_design_with_caption.png"
71
  image.save(file_path)
72
  return f"Design saved as {file_path}!"
73
 
74
-
75
- # HTML-based carousel content
76
- carousel_html = """
77
- <div class="carousel">
78
- <div class="slides">
79
- <img src="https://via.placeholder.com/300x400.png?text=Design+1" alt="Design 1">
80
- <img src="https://via.placeholder.com/300x400.png?text=Design+2" alt="Design 2">
81
- <img src="https://via.placeholder.com/300x400.png?text=Design+3" alt="Design 3">
82
- </div>
83
- </div>
84
- <style>
85
- .carousel {
86
- display: flex;
87
- overflow: hidden;
88
- width: 100%;
89
- max-width: 800px;
90
- margin: 0 auto;
91
- }
92
- .slides {
93
- display: flex;
94
- transition: transform 0.5s ease-in-out;
95
- animation: slide 10s infinite;
96
- }
97
- .slides img {
98
- width: 300px;
99
- height: 400px;
100
- margin: 0 10px;
101
- }
102
- @keyframes slide {
103
- 0%, 20% { transform: translateX(0); }
104
- 40%, 60% { transform: translateX(-320px); }
105
- 80%, 100% { transform: translateX(-640px); }
106
- }
107
- </style>
108
- """
109
-
110
  # Custom CSS for animations
111
  custom_css = """
112
  body {
@@ -134,17 +75,46 @@ body::before {
134
  }
135
  """
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  # Gradio interface
138
  with gr.Blocks(css=custom_css) as interface:
 
139
  gr.Markdown("# **AI Phone Cover Designer**")
140
  gr.Markdown("Create custom phone covers with AI. Save your designs for future use.")
141
 
142
  # Navigation Tabs
143
  with gr.Tabs():
144
  with gr.Tab("Home"):
145
- gr.Markdown("### Welcome to the **AI Phone Cover Designer**!")
146
- gr.Markdown("Below is a carousel showcasing some of the saved designs.")
147
- gr.HTML(carousel_html)
148
 
149
  with gr.Tab("Design"):
150
  with gr.Row():
@@ -152,6 +122,7 @@ with gr.Blocks(css=custom_css) as interface:
152
  color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red")
153
  phone_type_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung")
154
  design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns")
 
155
  generate_button = gr.Button("Generate Design")
156
  save_button = gr.Button("Save Design")
157
  with gr.Column(scale=1):
@@ -166,7 +137,7 @@ with gr.Blocks(css=custom_css) as interface:
166
  )
167
  save_button.click(
168
  save_design,
169
- inputs=[output_image, color_prompt, phone_type_prompt, design_prompt],
170
  outputs=output_message,
171
  )
172
 
@@ -184,4 +155,5 @@ with gr.Blocks(css=custom_css) as interface:
184
  Start designing today and bring your creative ideas to life!
185
  """)
186
 
 
187
  interface.launch(debug=True)
 
1
  import gradio as gr
2
  import requests
3
+ from PIL import Image
4
  from io import BytesIO
5
  from tqdm import tqdm
 
6
  import time
7
 
8
  repo = "artificialguybr/TshirtDesignRedmond-V2"
 
42
  else:
43
  raise Exception(f"API Error: {response.status_code}")
44
 
45
+ # Function to save the design
46
+ def save_design(image):
47
+ file_path = "saved_design.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  image.save(file_path)
49
  return f"Design saved as {file_path}!"
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # Custom CSS for animations
52
  custom_css = """
53
  body {
 
75
  }
76
  """
77
 
78
+ # JavaScript for text-to-speech and particles
79
+ custom_js = """
80
+ <script>
81
+ document.addEventListener('DOMContentLoaded', function () {
82
+ // Add particles
83
+ const particlesContainer = document.createElement('div');
84
+ particlesContainer.classList.add('particles');
85
+ for (let i = 0; i < 5; i++) {
86
+ const particle = document.createElement('div');
87
+ particle.classList.add('particle');
88
+ particlesContainer.appendChild(particle);
89
+ }
90
+ document.body.appendChild(particlesContainer);
91
+
92
+ // Text-to-speech functionality
93
+ function speak(text) {
94
+ const synth = window.speechSynthesis;
95
+ const utterance = new SpeechSynthesisUtterance(text);
96
+ synth.speak(utterance);
97
+ }
98
+ document.addEventListener('gradio_event:output_update', (event) => {
99
+ const outputText = event.detail?.text || '';
100
+ if (outputText) {
101
+ speak(outputText);
102
+ }
103
+ });
104
+ });
105
+ </script>
106
+ """
107
+
108
  # Gradio interface
109
  with gr.Blocks(css=custom_css) as interface:
110
+ gr.HTML(custom_js)
111
  gr.Markdown("# **AI Phone Cover Designer**")
112
  gr.Markdown("Create custom phone covers with AI. Save your designs for future use.")
113
 
114
  # Navigation Tabs
115
  with gr.Tabs():
116
  with gr.Tab("Home"):
117
+ gr.Markdown("Welcome to the **AI Phone Cover Designer**! Use the 'Design' tab to start creating custom designs.")
 
 
118
 
119
  with gr.Tab("Design"):
120
  with gr.Row():
 
122
  color_prompt = gr.Textbox(label="Color", placeholder="E.g., Red")
123
  phone_type_prompt = gr.Textbox(label="Mobile type", placeholder="E.g., iPhone, Samsung")
124
  design_prompt = gr.Textbox(label="Design Details", placeholder="E.g., Bold stripes with geometric patterns")
125
+ chatbot = gr.Chatbot()
126
  generate_button = gr.Button("Generate Design")
127
  save_button = gr.Button("Save Design")
128
  with gr.Column(scale=1):
 
137
  )
138
  save_button.click(
139
  save_design,
140
+ inputs=[output_image],
141
  outputs=output_message,
142
  )
143
 
 
155
  Start designing today and bring your creative ideas to life!
156
  """)
157
 
158
+ # Launch the app
159
  interface.launch(debug=True)