Alex commited on
Commit
9872905
·
verified ·
1 Parent(s): 777e4f3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +239 -0
app.py ADDED
@@ -0,0 +1,239 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ # Function for Text-to-Image (Send POST request with prompt, ratio, style, etc.)
6
+ def send_text2image_request(prompt, ratio, style, uuid_value, token):
7
+ # Access the URL from Secrets (Private)
8
+ url = os.getenv('API_URL') # Secret variable for the URL
9
+ if not url:
10
+ return "Error: API URL not set in environment secrets."
11
+
12
+ # Define the headers with the required values
13
+ headers = {
14
+ "Uid": uuid_value, # Using the provided UUID
15
+ "Versioncode": "16701",
16
+ "Accept-Version": "v1",
17
+ "Token": token, # Replace with the actual token
18
+ "Content-Type": "application/x-www-form-urlencoded"
19
+ }
20
+
21
+ # Define the values to be sent in the POST request
22
+ data = {
23
+ "is_translate": "1",
24
+ "is_first": "true",
25
+ "prompt": prompt,
26
+ "ratio": ratio,
27
+ "style_id": style
28
+ }
29
+
30
+ # Sending POST request
31
+ response = requests.post(url, headers=headers, data=data)
32
+
33
+ # Return the response text or any relevant information
34
+ if response.status_code == 200:
35
+ return response.text
36
+ else:
37
+ return f"Error: {response.status_code} - {response.text}"
38
+
39
+ # Function for Image-to-Image (Send POST request with an image and other parameters)
40
+ def send_image2image_request(image, ratio, style, uuid_value, token):
41
+ # Access the URL from Secrets (Private)
42
+ url = os.getenv('API_URL_IMAGE') # Secret variable for the URL
43
+ if not url:
44
+ return "Error: API URL not set in environment secrets."
45
+
46
+ # Define the headers with the required values
47
+ headers = {
48
+ "Uid": uuid_value, # Using the provided UUID
49
+ "Versioncode": "16701",
50
+ "Accept-Version": "v1",
51
+ "Token": token, # Replace with the actual token
52
+ "Content-Type": "application/x-www-form-urlencoded"
53
+ }
54
+
55
+ # Define the values to be sent in the POST request
56
+ data = {
57
+ "is_translate": "1",
58
+ "is_first": "true",
59
+ "ratio": ratio,
60
+ "denoising_strength": "50",
61
+ "image_name": image,
62
+ "is_img2img": "1"
63
+ }
64
+
65
+ if style and style != "0":
66
+ data["style_id"] = style
67
+
68
+ response = requests.post(url, headers=headers, data=data)
69
+
70
+ # Return the response text or any relevant information
71
+ if response.status_code == 200:
72
+ return response.text
73
+ else:
74
+ return f"Error: {response.status_code} - {response.text}"
75
+
76
+ # Function for Image-to-Image (Send POST request with an image and other parameters)
77
+ def send_inpaint2image_request(image, mask, prompt, ratio, uuid_value, token, style):
78
+ # Access the URL from Secrets (Private)
79
+ url = os.getenv('API_URL_IMAGE') # Secret variable for the URL
80
+ if not url:
81
+ return "Error: API URL not set in environment secrets."
82
+
83
+ # Define the headers with the required values
84
+ headers = {
85
+ "Uid": uuid_value, # Using the provided UUID
86
+ "Versioncode": "16701",
87
+ "Accept-Version": "v1",
88
+ "Token": token, # Replace with the actual token
89
+ "Content-Type": "application/x-www-form-urlencoded"
90
+ }
91
+
92
+ # Define the values to be sent in the POST request
93
+ data = {
94
+ "is_translate": "1",
95
+ "is_first": "true",
96
+ "image_name": image,
97
+ "mask_name":mask,
98
+ "prompt": prompt,
99
+ "ratio": ratio,
100
+ "is_img2img": "1"
101
+ }
102
+
103
+ if style and style != "0":
104
+ data["style_id"] = style
105
+
106
+ response = requests.post(url, headers=headers, data=data)
107
+
108
+ # Return the response text or any relevant information
109
+ if response.status_code == 200:
110
+ return response.text
111
+ else:
112
+ return f"Error: {response.status_code} - {response.text}"
113
+
114
+ # Function to get the Upload URL from Firebase
115
+ def get_upload_url(uuid_value):
116
+ # The URL to send the request to
117
+ firebase_url = 'https://firebasestorage.googleapis.com/v0/b/hardstone_img_us/o?name=umagic_crop%2Fupload%2F2024-11-17%2F{}.jpg&uploadType=resumable'.format(uuid_value)
118
+
119
+ # Define the headers with the required values
120
+ headers = {
121
+ "Content-Type": "application/x-www-form-urlencoded", # Required content type
122
+ "X-Firebase-Storage-Version": "Android/24.43.37 (100400-693941914)",
123
+ "X-Firebase-Gmpid": "1:873188155690:android:1fd5d2e7b355e982807e53",
124
+ "X-Goog-Upload-Command": "start",
125
+ "X-Goog-Upload-Protocol": "resumable",
126
+ "X-Goog-Upload-Header-Content-Type": "application/x-www-form-urlencoded",
127
+ "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10; Android SDK built for arm64 Build/QSR1.210802.001)",
128
+ "Connection": "Keep-Alive",
129
+ "Accept-Encoding": "gzip, deflate, br"
130
+ }
131
+
132
+ # Sending POST request to Firebase Storage
133
+ response = requests.post(firebase_url, headers=headers)
134
+
135
+ # Check if the request was successful
136
+ if response.status_code == 200:
137
+ # Return the response body as JSON and the response headers
138
+ return dict(response.headers)
139
+ else:
140
+ # Return an error with the status code and the error message
141
+ return dict(response.headers)
142
+
143
+ # Function to PUT image to a specified URL
144
+ def put_image_to_url(image, url):
145
+ # Read the image file to send as data
146
+ with open(image.name, 'rb') as f:
147
+ image_data = f.read()
148
+
149
+ # Define the headers with the required values for PUT request
150
+ headers = {
151
+ "X-Firebase-Storage-Version": "Android/24.43.37 (100400-693941914)",
152
+ "X-Firebase-Gmpid": "1:873188155690:android:1fd5d2e7b355e982807e53",
153
+ "X-Goog-Upload-Command": "upload, finalize",
154
+ "X-Goog-Upload-Protocol": "resumable",
155
+ "X-Goog-Upload-Offset": "0",
156
+ "Content-Type": "application/x-www-form-urlencoded",
157
+ "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 10; Android SDK built for arm64 Build/QSR1.210802.001)",
158
+ "Connection": "Keep-Alive",
159
+ "Accept-Encoding": "gzip, deflate, br",
160
+ "Content-Length": str(len(image_data)) # Set the content length to the size of the image
161
+ }
162
+
163
+ # Sending PUT request with image data
164
+ response = requests.put(url, headers=headers, data=image_data)
165
+
166
+ # Check if the PUT request was successful
167
+ if response.status_code == 200:
168
+ return response.text
169
+ else:
170
+ return f"Error: {response.status_code} - {response.text}"
171
+
172
+ # Gradio interface with tabs for Text-to-Image, Image-to-Image, Get Upload URL, and Image Put
173
+ def create_interface():
174
+ with gr.Blocks() as demo:
175
+ with gr.Tab("Text to Image"):
176
+ # Inputs for Text-to-Image
177
+ text_prompt = gr.Textbox(label="Prompt")
178
+ text_ratio = gr.Textbox(label="Ratio")
179
+ text_style = gr.Textbox(label="Style ID")
180
+ text_uuid = gr.Textbox(label="UUID ID") # Default UUID
181
+ text_token = gr.Textbox(label="Token ID") # Password type for token
182
+ text_output = gr.Textbox() # Output for text-to-image response
183
+
184
+ # Bind the text-to-image function to the interface
185
+ text_submit_button = gr.Button("Generate Image")
186
+ text_submit_button.click(send_text2image_request, inputs=[text_prompt, text_ratio, text_style, text_uuid, text_token], outputs=text_output)
187
+
188
+ with gr.Tab("Image to Image"):
189
+ # Inputs for Image-to-Image
190
+ img_url = gr.Textbox(label="URL")
191
+ img_ratio = gr.Textbox(label="Ratio")
192
+ img_style = gr.Textbox(label="Style ID")
193
+ img_uuid = gr.Textbox(label="UUID ID") # Default UUID
194
+ img_token = gr.Textbox(label="Token ID") # Password type for token
195
+ img_output = gr.Textbox() # Output for image-to-image response
196
+
197
+ # Bind the image-to-image function to the interface
198
+ img_submit_button = gr.Button("Generate Image")
199
+ img_submit_button.click(send_image2image_request, inputs=[img_url, img_ratio, img_style, img_uuid, img_token], outputs=img_output)
200
+
201
+ with gr.Tab("Inpaint Image"):
202
+ # Inputs for Inpaint-to-Image
203
+ in_url = gr.Textbox(label="URL")
204
+ in_mask = gr.Textbox(label="Mask URL")
205
+ in_prompt = gr.Textbox(label="Prompt")
206
+ in_ratio = gr.Textbox(label="Ratio")
207
+ in_uuid = gr.Textbox(label="UUID ID") # Default UUID
208
+ in_token = gr.Textbox(label="Token ID")
209
+ in_style = gr.Textbox(label="Style ID") # Password type for token
210
+ in_output = gr.Textbox() # Output for image-to-image response
211
+
212
+ # Bind the image-to-image function to the interface
213
+ in_submit_button = gr.Button("Generate Image")
214
+ in_submit_button.click(send_inpaint2image_request, inputs=[in_url, in_mask, in_prompt, in_ratio, in_uuid, in_token, in_style], outputs=in_output)
215
+
216
+ with gr.Tab("Get Upload URL"):
217
+ # Inputs for Get Upload URL
218
+ uuid_input = gr.Textbox(label="UUID ID") # UUID for getting upload URL
219
+ upload_url_output = gr.Textbox() # Output for the upload URL response
220
+
221
+ # Bind the get_upload_url function to the interface
222
+ upload_submit_button = gr.Button("Get Upload URL")
223
+ upload_submit_button.click(get_upload_url, inputs=[uuid_input], outputs=upload_url_output)
224
+
225
+ with gr.Tab("Image Put"):
226
+ # Inputs for Image Put
227
+ image_input = gr.File(label="Upload Image") # Input for image file
228
+ url_input = gr.Textbox(label="Upload URL") # Input for the URL where to PUT the image
229
+ put_image_output = gr.Textbox() # Output for the PUT request response
230
+
231
+ # Bind the put_image_to_url function to the interface
232
+ put_submit_button = gr.Button("Upload Image")
233
+ put_submit_button.click(put_image_to_url, inputs=[image_input, url_input], outputs=put_image_output)
234
+
235
+ return demo
236
+
237
+ # Launch the Gradio interface
238
+ interface = create_interface()
239
+ interface.launch()