uhdessai commited on
Commit
e55d57d
·
verified ·
1 Parent(s): ddfe5aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -28
app.py CHANGED
@@ -41,29 +41,29 @@
41
  # image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
42
  # return image_paths
43
 
44
- # # === Send Image to Backend ===
45
- # def send_to_backend(img_path, user_id):
46
- # if not user_id:
47
- # return "❌ user_id not found in URL."
48
 
49
- # if not img_path or not os.path.exists(img_path):
50
- # return "⚠️ No image selected or image not found."
51
 
52
- # try:
53
- # with open(img_path, 'rb') as f:
54
- # files = {'file': ('generated_image.png', f, 'image/png')}
55
 
56
- # # Your backend endpoint here
57
- # url = f" https://7da2-2409-4042-6e81-1806-de6-b8e5-836c-6b95.ngrok-free.app/images/upload/{user_id}"
58
- # response = requests.post(url, files=files)
59
 
60
- # if response.status_code == 201:
61
- # return "✅ Image uploaded and saved to database!"
62
- # else:
63
- # return f"❌ Upload failed: {response.status_code} - {response.text}"
64
 
65
- # except Exception as e:
66
- # return f"⚠️ Error: {str(e)}"
67
 
68
  # # === Gradio Interface ===
69
  # with gr.Blocks() as demo:
@@ -107,15 +107,15 @@
107
  # outputs=output
108
  # )
109
 
110
- # # Generate button logicg
111
- # def generate_and_display_images():
112
- # image_paths = get_random_images()
113
- # return image_paths + image_paths # One for display, one for hidden path tracking
114
 
115
- # generate_button.click(
116
- # fn=generate_and_display_images,
117
- # outputs=image_components + file_paths
118
- # )
119
 
120
  # if __name__ == "__main__":
121
  # demo.launch()
@@ -189,8 +189,37 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
189
 
190
  with gr.Row():
191
  submit_button = gr.Button("Generate Designs")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
- output_gallery = gr.Gallery(label="Generated Designs", show_label=True, columns=2, height="auto")
 
 
 
 
194
 
195
 
196
  examples = gr.Examples(
@@ -201,6 +230,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
201
  inputs=[input_box]
202
  )
203
 
204
- submit_button.click(fn=generate_top_dresses, inputs=input_box, outputs=output_gallery)
 
 
205
 
206
  demo.launch()
 
41
  # image_paths = [os.path.join(OUTPUT_DIR, img) for img in random_images]
42
  # return image_paths
43
 
44
+ # === Send Image to Backend ===
45
+ def send_to_backend(img_path, user_id):
46
+ if not user_id:
47
+ return "❌ user_id not found in URL."
48
 
49
+ if not img_path or not os.path.exists(img_path):
50
+ return "⚠️ No image selected or image not found."
51
 
52
+ try:
53
+ with open(img_path, 'rb') as f:
54
+ files = {'file': ('generated_image.png', f, 'image/png')}
55
 
56
+ # Your backend endpoint here
57
+ url = f" https://7da2-2409-4042-6e81-1806-de6-b8e5-836c-6b95.ngrok-free.app/images/upload/{user_id}"
58
+ response = requests.post(url, files=files)
59
 
60
+ if response.status_code == 201:
61
+ return "✅ Image uploaded and saved to database!"
62
+ else:
63
+ return f"❌ Upload failed: {response.status_code} - {response.text}"
64
 
65
+ except Exception as e:
66
+ return f"⚠️ Error: {str(e)}"
67
 
68
  # # === Gradio Interface ===
69
  # with gr.Blocks() as demo:
 
107
  # outputs=output
108
  # )
109
 
110
+ # Generate button logicg
111
+ def generate_and_display_images():
112
+ image_paths = get_random_images()
113
+ return image_paths + image_paths # One for display, one for hidden path tracking
114
 
115
+ # generate_button.click(
116
+ # fn=generate_and_display_images,
117
+ # outputs=image_components + file_paths
118
+ # )
119
 
120
  # if __name__ == "__main__":
121
  # demo.launch()
 
189
 
190
  with gr.Row():
191
  submit_button = gr.Button("Generate Designs")
192
+ user_id_state = gr.State()
193
+
194
+ @demo.load(inputs=None, outputs=[user_id_state])
195
+ def get_user_id(request: gr.Request):
196
+ return request.query_params.get("user_id", "")
197
+
198
+ image_components = []
199
+ file_paths = []
200
+ save_buttons = []
201
+ outputs = []
202
+
203
+ with gr.Row():
204
+ for i in range(2): # Only 2 images
205
+ with gr.Column():
206
+ img = gr.Image(width=180, height=180, label=f"Design {i+1}")
207
+ image_components.append(img)
208
+
209
+ file_path = gr.Textbox(visible=False)
210
+ file_paths.append(file_path)
211
+
212
+ save_btn = gr.Button("💾 Save to DB")
213
+ save_buttons.append(save_btn)
214
+
215
+ output = gr.Textbox(label="Status", interactive=False)
216
+ outputs.append(output)
217
 
218
+ save_btn.click(
219
+ fn=send_to_backend,
220
+ inputs=[file_path, user_id_state],
221
+ outputs=output
222
+ )
223
 
224
 
225
  examples = gr.Examples(
 
230
  inputs=[input_box]
231
  )
232
 
233
+ submit_button.click(
234
+ fn=generate_and_display_images,
235
+ outputs=image_components + file_paths
236
 
237
  demo.launch()