fbumg / app.py
Alex
Update app.py
360c7e8 verified
raw
history blame
19.9 kB
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, prompt):
# 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 prompt and prompt != "0":
data["prompt"] = prompt
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_line2image_request(image, ratio, style, uuid_value, token, prompt):
# Access the URL from Secrets (Private)
url = os.getenv('API_DOODLE') # 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 prompt and prompt != "0":
data["prompt"] = prompt
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_enhance_request(image, uuid_value, token):
# Access the URL from Secrets (Private)
url = os.getenv('API_URL_ENHANCE') # 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 = {
"image_name": image
}
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}"
def send_generate_prompt_request(image, uuid_value, token):
# Access the URL from Secrets (Private)
url = os.getenv('API_URL_PROMPT') # 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 = {
"image_name": image
}
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}"
def send_outpaint_request(image, canvas_info, uuid_value, token):
# Access the URL from Secrets (Private)
url = os.getenv('API_URL_OUT') # 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 = {
"image_name": image,
"canvas_info": canvas_info
}
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}"
def send_face_request(image, target, uuid_value, token):
# Access the URL from Secrets (Private)
url = os.getenv('API_URL_FACE') # 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 = {
"uuid": uuid_value,
"targetUrl": target
}
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}"
def send_face_request_d(image, target, uuid_value, token):
# Construct the URL with the provided UUID value
image_url = f"cutpaste_yearbook/result/2024-11-26/rst_58cd61f5-7424-494a-9d19-09d2ca51fed7.jpg"
# Static response JSON with dynamic image URL
static_response = {
"data": {
"image_url": [image_url],
"task_id": uuid_value # Assuming the task_id is the same as the uuid_value
},
"error": "",
"message": "",
"next": "",
"response": "ok",
"status": "200"
}
response = json.dumps(static_response)
# Return the static JSON response as a string
return response
# 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("Line to Image"):
# Inputs for Image-to-Image
line_url = gr.Textbox(label="URL")
line_ratio = gr.Textbox(label="Ratio")
line_style = gr.Textbox(label="Style ID")
line_uuid = gr.Textbox(label="UUID ID") # Default UUID
line_token = gr.Textbox(label="Token ID") # Password type for token
line_prompt = gr.Textbox(label="img_prompt ID") # Password type for token
line_output = gr.Textbox() # Output for image-to-image response
# Bind the image-to-image function to the interface
line_submit_button = gr.Button("Generate Image")
line_submit_button.click(send_line2image_request, inputs=[line_url, line_ratio, line_style, line_uuid, line_token, line_prompt], outputs=line_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("Get Enhance"):
# Inputs for Get Upload URL
en_url = gr.Textbox(label="URL")
en_uuid = gr.Textbox(label="UUID ID") # Default UUID
en_token = gr.Textbox(label="Token ID")
en_output = gr.Textbox() # Output for the upload URL response
# Bind the get_upload_url function to the interface
en_submit_button = gr.Button("Get Upload URL")
en_submit_button.click(send_enhance_request, inputs=[en_url, en_uuid, en_token], outputs=en_output)
with gr.Tab("Get Prompt"):
# Inputs for Get Upload URL
gen_url = gr.Textbox(label="URL")
gen_uuid = gr.Textbox(label="UUID ID") # Default UUID
gen_token = gr.Textbox(label="Token ID")
gen_output = gr.Textbox() # Output for the upload URL response
# Bind the get_upload_url function to the interface
gen_submit_button = gr.Button("Get Upload URL")
gen_submit_button.click(send_generate_prompt_request, inputs=[gen_url, gen_uuid, gen_token], outputs=gen_output)
with gr.Tab("Get OutPaint"):
# Inputs for Get Upload URL
out_url = gr.Textbox(label="URL")
out_canvas = gr.Textbox(label="Canvas")
out_uuid = gr.Textbox(label="UUID ID") # Default UUID
out_token = gr.Textbox(label="Token ID")
out_output = gr.Textbox() # Output for the upload URL response
# Bind the get_upload_url function to the interface
out_submit_button = gr.Button("Get Upload URL")
out_submit_button.click(send_outpaint_request, inputs=[out_url, out_canvas, out_uuid, out_token], outputs=out_output)
with gr.Tab("Get Faceswap"):
# Inputs for Get Upload URL
fs_url = gr.Textbox(label="URL")
fs_target = gr.Textbox(label="Target URL")
fs_uuid = gr.Textbox(label="UUID ID") # Default UUID
fs_token = gr.Textbox(label="Token ID")
fs_output = gr.Textbox() # Output for the upload URL response
# Bind the get_upload_url function to the interface
fs_submit_button = gr.Button("Get Upload URL")
fs_submit_button.click(send_face_request, inputs=[fs_url, fs_target, fs_uuid, fs_token], outputs=fs_output)
with gr.Tab("Demo Faceswap"):
# Inputs for Get Upload URL
dfs_url = gr.Textbox(label="URL")
dfs_target = gr.Textbox(label="Target URL")
dfs_uuid = gr.Textbox(label="UUID ID") # Default UUID
dfs_token = gr.Textbox(label="Token ID")
dfs_output = gr.Textbox() # Output for the upload URL response
# Bind the get_upload_url function to the interface
dfs_submit_button = gr.Button("Get Upload URL")
dfs_submit_button.click(send_face_request_d, inputs=[dfs_url, dfs_target, dfs_uuid, dfs_token], outputs=dfs_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()