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