from gradio_client import Client, handle_file import gradio as gr import concurrent.futures import tempfile import os from PIL import Image # Impor tema custom dari themes.py from themes import IndonesiaTheme # Siapkan URL dan header untuk permintaan API url_api1 = os.environ['url_api1'] url_api2 = os.environ['url_api2'] # Fungsi untuk FLUX Std def infer_image(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress()): client = Client(url_api1) try: progress(0, "Memulai proses...") result = client.predict( prompt=prompt, seed=seed, randomize_seed=randomize_seed, width=width, height=height, guidance_scale=guidance_scale, num_inference_steps=num_inference_steps, api_name="/infer" ) progress(1, "Proses selesai.") return result[0], seed if not randomize_seed else result[1] except concurrent.futures.CancelledError: return None, "Request was cancelled. Please try again." # Fungsi untuk FLUX Inpainting def inpainting_process(input_image_editor, input_text, seed_slicer, randomize_seed_checkbox, strength_slider, num_inference_steps_slider, progress=gr.Progress()): client = Client(url_api2) try: progress(0, "Memulai proses inpainting...") # Simpan gambar sementara ke file with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp: input_image_editor["composite"].save(temp.name) temp_image_path = temp.name # Tutup file setelah penulisan selesai temp.close() # Gunakan handle_file untuk memproses gambar dari jalur file sementara input_image_editor = {"background": handle_file(temp_image_path)} result = client.predict( input_image_editor=input_image_editor, input_text=input_text, seed_slicer=seed_slicer, randomize_seed_checkbox=randomize_seed_checkbox, strength_slider=strength_slider, num_inference_steps_slider=num_inference_steps_slider, api_name="/process" ) progress(1, "Proses inpainting selesai.") return result[0], result[1] except concurrent.futures.CancelledError: return None, "Request was cancelled. Please try again." finally: # Hapus file sementara setelah selesai digunakan import os os.remove(temp_image_path) # CSS untuk styling antarmuka css = """ #col-left, #col-mid, #col-right { margin: 0 auto; max-width: 400px; padding: 10px; border-radius: 15px; background-color: #f9f9f9; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } #banner { width: 100%; text-align: center; margin-bottom: 20px; } #run-button { background-color: #ff4b5c; color: white; font-weight: bold; padding: 10px; border-radius: 10px; cursor: pointer; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); } #footer { text-align: center; margin-top: 20px; color: silver; } """ # Interface Gradio with gr.Blocks(css=css, theme=IndonesiaTheme()) as app: # Tambahkan banner gr.HTML("""