|
import gradio as gr |
|
import requests |
|
import json |
|
import hashlib |
|
import time |
|
import os |
|
from PIL import Image |
|
from pathlib import Path |
|
from io import BytesIO |
|
|
|
|
|
images = ["bxh.jpg", "bxh1.jpg", "bxh2.jpg", "bxh3.jpg"] |
|
|
|
|
|
url_pre = "https://ap-east-1.tensorart.cloud/v1" |
|
|
|
|
|
SAVE_DIR = "generated_images" |
|
Path(SAVE_DIR).mkdir(exist_ok=True) |
|
|
|
|
|
api_key_token = os.getenv("api_key_token") |
|
if not api_key_token: |
|
raise ValueError("API key token không tìm thấy trong biến môi trường.") |
|
|
|
|
|
def txt2img(prompt, width, height): |
|
model_id = "770694094415489962" |
|
vae_id = "sdxl-vae-fp16-fix.safetensors" |
|
lora_items = [ |
|
{"loraModel": "808188171067237481", "weight": 1.0} |
|
] |
|
|
|
txt2img_data = { |
|
"request_id": hashlib.md5(str(int(time.time())).encode()).hexdigest(), |
|
"stages": [ |
|
{ |
|
"type": "INPUT_INITIALIZE", |
|
"inputInitialize": { |
|
"seed": -1, |
|
"count": 1 |
|
} |
|
}, |
|
{ |
|
"type": "DIFFUSION", |
|
"diffusion": { |
|
"width": width, |
|
"height": height, |
|
"prompts": [ |
|
{ |
|
"text": prompt |
|
} |
|
], |
|
"negativePrompts": [ |
|
{ |
|
"text": "nsfw" |
|
} |
|
], |
|
"sdModel": model_id, |
|
"sdVae": vae_id, |
|
"sampler": "Euler a", |
|
"steps": 20, |
|
"cfgScale": 3, |
|
"clipSkip": 1, |
|
"etaNoiseSeedDelta": 31337, |
|
"lora": { |
|
"items": lora_items |
|
} |
|
} |
|
} |
|
] |
|
} |
|
|
|
body = json.dumps(txt2img_data) |
|
|
|
|
|
headers = { |
|
'Content-Type': 'application/json', |
|
'Accept': 'application/json', |
|
'Authorization': f'Bearer {api_key_token}' |
|
} |
|
|
|
|
|
response = requests.post(f"{url_pre}/jobs", json=txt2img_data, headers=headers) |
|
|
|
if response.status_code != 200: |
|
return f"Error: {response.status_code} - {response.text}" |
|
|
|
response_data = response.json() |
|
job_id = response_data['job']['id'] |
|
print(f"Job created. ID: {job_id}") |
|
|
|
|
|
start_time = time.time() |
|
timeout = 300 |
|
|
|
while True: |
|
time.sleep(10) |
|
|
|
|
|
elapsed_time = time.time() - start_time |
|
if elapsed_time > timeout: |
|
return f"Error: Job timed out after {timeout} seconds." |
|
|
|
|
|
response = requests.get(f"{url_pre}/jobs/{job_id}", headers=headers) |
|
|
|
if response.status_code != 200: |
|
return f"Error: {response.status_code} - {response.text}" |
|
|
|
get_job_response_data = response.json() |
|
print("Job response data:", get_job_response_data) |
|
job_status = get_job_response_data['job']['status'] |
|
print(f"Job status: {job_status}") |
|
|
|
if job_status == 'SUCCESS': |
|
if 'successInfo' in get_job_response_data['job']: |
|
image_url = get_job_response_data['job']['successInfo']['images'][0]['url'] |
|
print(f"Job succeeded. Image URL: {image_url}") |
|
|
|
|
|
response_image = requests.get(image_url) |
|
img = Image.open(BytesIO(response_image.content)) |
|
return img |
|
else: |
|
return "Error: Output is missing in the job response." |
|
elif job_status == 'FAILED': |
|
return "Error: Job failed. Please try again with different settings." |
|
else: |
|
print("Job is still in progress...") |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(""" |
|
# Flux ENHANCED ALL - Realistic |
|
<p style='font-size: 14px;'>Feel free to explore my work and contact me for any inquiries via email: dung.ngt1988@gmail.com</p> |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
prompt_input = gr.Textbox(label="Mô tả ảnh cần tạo", |
|
placeholder="A cover image for podcast at Youtube channel about buddhism with a girl stands on the left of image. She points a big perfect typography '4 things Buddha can not help' on the center of image") |
|
size_options = gr.Dropdown(choices=["1152x768","1024x1024", "768x1152"], label="Chọn cỡ ảnh") |
|
generate_button = gr.Button("Generate") |
|
|
|
with gr.Column(): |
|
|
|
output_image = gr.Image(label="Ảnh đã tạo") |
|
|
|
|
|
def generate(prompt, size_choice): |
|
if size_choice == "1152x768": |
|
width, height = 1152, 768 |
|
elif size_choice == "768x1152": |
|
width, height = 768, 1152 |
|
elif size_choice == "1024x1024": |
|
width, height = 1024, 1024 |
|
else: |
|
raise ValueError("Invalid size choice") |
|
|
|
return txt2img(prompt, width, height) |
|
|
|
|
|
generate_button.click(fn=generate, inputs=[prompt_input, size_options], outputs=output_image) |
|
|
|
|
|
demo.launch() |