Vijish commited on
Commit
b2e3aaf
·
verified ·
1 Parent(s): 6947538

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import time
7
+ import os
8
+
9
+ # Your endpoint URL and token (you might want to use environment variables for these in production)
10
+ API_URL = "https://ca80xvp8jmhqanbz.us-east-1.aws.endpoints.huggingface.cloud"
11
+
12
+ def process_image(input_image, prompt):
13
+ # Convert Gradio's image input to base64
14
+ if input_image is None:
15
+ return None, "Please upload an image"
16
+
17
+ # Convert PIL Image to bytes
18
+ img_byte_arr = io.BytesIO()
19
+ input_image.save(img_byte_arr, format='WEBP')
20
+ img_byte_arr = img_byte_arr.getvalue()
21
+
22
+ # Encode to base64
23
+ base64_image = f"data:image/webp;base64,{base64.b64encode(img_byte_arr).decode('utf-8')}"
24
+
25
+ # Prepare headers and data
26
+ headers = {
27
+ "Authorization": f"Bearer {os.getenv('HF_API_KEY')}",
28
+ "Content-Type": "application/json",
29
+ "Accept": "application/json"
30
+ }
31
+
32
+ data = {
33
+ "inputs": {
34
+ "prompt": prompt,
35
+ "image": base64_image
36
+ }
37
+ }
38
+
39
+ # Make the API request with retry mechanism
40
+ max_retries = 5
41
+ retry_delay = 60 # seconds
42
+
43
+ for retry in range(max_retries):
44
+ try:
45
+ response = requests.post(API_URL, headers=headers, json=data, timeout=300)
46
+
47
+ if response.status_code == 200:
48
+ # Convert the response base64 image back to PIL Image
49
+ result = response.json()
50
+ image_bytes = base64.b64decode(result["final_image"])
51
+ output_image = Image.open(io.BytesIO(image_bytes))
52
+
53
+ # Return both the image and the parameters used
54
+ params_text = "\nParameters used:\n"
55
+ for key, value in result["parameters"].items():
56
+ params_text += f"{key}: {value}\n"
57
+
58
+ return output_image, params_text
59
+
60
+ elif response.status_code == 503:
61
+ if retry < max_retries - 1:
62
+ time.sleep(retry_delay)
63
+ continue
64
+ return None, f"Service unavailable after {max_retries} retries"
65
+ else:
66
+ return None, f"Error: {response.status_code}\n{response.text}"
67
+
68
+ except requests.exceptions.RequestException as e:
69
+ if retry < max_retries - 1:
70
+ time.sleep(retry_delay)
71
+ continue
72
+ return None, f"Request error: {str(e)}"
73
+
74
+ return None, "Maximum retries reached"
75
+
76
+ # Create Gradio interface
77
+ with gr.Blocks(title="Room Design Diffusion", theme=gr.themes.Soft()) as demo:
78
+ gr.Markdown("""
79
+ # Room Design Diffusion
80
+ Upload a room image and provide a prompt describing how you'd like to redesign it.
81
+ The AI will generate a new version of your room based on your description.
82
+ """)
83
+
84
+ with gr.Row():
85
+ with gr.Column():
86
+ input_image = gr.Image(label="Upload Room Image", type="pil")
87
+ prompt = gr.Textbox(
88
+ label="Describe how you want to redesign the room",
89
+ placeholder="Example: A modern living room with a comfortable gray sectional sofa, glass coffee table, minimalist TV stand, and geometric area rug",
90
+ lines=3
91
+ )
92
+ submit_btn = gr.Button("Generate Design", variant="primary")
93
+
94
+ with gr.Column():
95
+ output_image = gr.Image(label="Generated Design")
96
+ output_text = gr.Textbox(label="Processing Details", lines=5)
97
+
98
+ submit_btn.click(
99
+ fn=process_image,
100
+ inputs=[input_image, prompt],
101
+ outputs=[output_image, output_text]
102
+ )
103
+
104
+ gr.Markdown("""
105
+ ### Tips for best results:
106
+ - Use clear, detailed prompts describing the desired room style
107
+ - Make sure the input image is well-lit and clearly shows the room
108
+ - Be patient as generation can take a few minutes
109
+ """)
110
+
111
+ # Launch the app
112
+ demo.launch(share=True)