Spaces:
Sleeping
Sleeping
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() | |