|
import requests |
|
import base64 |
|
import os |
|
from PIL import Image |
|
import io |
|
import time |
|
import datetime |
|
|
|
def encode_image_to_base64(image_path): |
|
"""Convert an image file to base64 string.""" |
|
with open(image_path, "rb") as image_file: |
|
return base64.b64encode(image_file.read()).decode('utf-8') |
|
|
|
def process_image( |
|
image_path, |
|
mask_path=None, |
|
prompt="", |
|
height=1632, |
|
width=1232, |
|
guidance_scale=30, |
|
num_inference_steps=50, |
|
max_sequence_length=512, |
|
api_token=None, |
|
output_path="output_image.jpg" |
|
): |
|
""" |
|
Send a request to the Hugging Face Inference Endpoint. |
|
|
|
Args: |
|
image_path (str): Path to the input image |
|
mask_path (str, optional): Path to the mask image |
|
prompt (str): Text prompt to guide the model |
|
height (int): Output image height |
|
width (int): Output image width |
|
guidance_scale (float): Guidance scale for the model |
|
num_inference_steps (int): Number of inference steps |
|
max_sequence_length (int): Maximum sequence length |
|
api_token (str): Hugging Face API token |
|
output_path (str): Path to save the output image |
|
|
|
Returns: |
|
The response from the API or the path to the saved image |
|
""" |
|
|
|
start_time = time.time() |
|
start_timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") |
|
print(f"Request started at: {start_timestamp}") |
|
|
|
|
|
url = "https://pewtn8bsankvfriv.us-east-1.aws.endpoints.huggingface.cloud" |
|
|
|
|
|
if api_token is None: |
|
api_token = os.environ.get("HF_API_TOKEN") |
|
if api_token is None: |
|
raise ValueError("API token not provided. Please set HF_API_TOKEN environment variable or pass it as a parameter.") |
|
|
|
|
|
base64_image = encode_image_to_base64(image_path) |
|
|
|
|
|
base64_mask = None |
|
if mask_path: |
|
base64_mask = encode_image_to_base64(mask_path) |
|
|
|
|
|
payload = { |
|
"inputs": { |
|
"image": base64_image, |
|
"prompt": prompt |
|
}, |
|
"parameters": { |
|
"height": height, |
|
"width": width, |
|
"guidance_scale": guidance_scale, |
|
"num_inference_steps": num_inference_steps, |
|
"max_sequence_length": max_sequence_length, |
|
} |
|
} |
|
|
|
|
|
if base64_mask: |
|
payload["inputs"]["mask"] = base64_mask |
|
|
|
|
|
headers = { |
|
"Authorization": f"Bearer {api_token}", |
|
"Content-Type": "application/json" |
|
} |
|
|
|
|
|
print(f"Sending request to {url}...") |
|
response = requests.post(url, json=payload, headers=headers) |
|
|
|
|
|
end_time = time.time() |
|
end_timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") |
|
elapsed_time = end_time - start_time |
|
print(f"Request completed at: {end_timestamp}") |
|
print(f"Total request time: {elapsed_time:.2f} seconds ({elapsed_time/60:.2f} minutes)") |
|
|
|
|
|
if response.status_code == 200: |
|
try: |
|
response_data = response.json() |
|
|
|
|
|
if "image" in response_data and "status" in response_data and response_data["status"] == "success": |
|
|
|
image_data = base64.b64decode(response_data["image"]) |
|
|
|
|
|
image = Image.open(io.BytesIO(image_data)) |
|
|
|
|
|
image.save(output_path) |
|
print(f"Image successfully saved to {output_path}") |
|
|
|
return output_path |
|
else: |
|
print("Unexpected response format:", response_data) |
|
return response_data |
|
except Exception as e: |
|
print(f"Error processing response: {e}") |
|
return response.json() |
|
else: |
|
print(f"Error: {response.status_code}") |
|
print(response.text) |
|
return None |
|
|
|
if __name__ == "__main__": |
|
|
|
output_file = "generated_image1.jpg" |
|
|
|
|
|
api_token = os.environ.get("HF_API_TOKEN") |
|
if not api_token: |
|
api_token = input("请输入您的Hugging Face API令牌: ") |
|
|
|
print("Starting image processing...") |
|
result = process_image( |
|
image_path="cup.png", |
|
mask_path="cup_mask.png", |
|
prompt="a blue paper cup", |
|
api_token=api_token, |
|
output_path=output_file |
|
) |
|
|
|
if result == output_file: |
|
print(f"Processing completed successfully. Image saved to {output_file}") |
|
else: |
|
print("Processing completed with unexpected result:", result) |