import gradio as gr import requests import base64 import os from PIL import Image from io import BytesIO import numpy as np from gradio_imageslider import ImageSlider # Assicurati di avere questa libreria installata from loadimg import load_img # Assicurati che questa funzione sia disponibile from dotenv import load_dotenv # Carica le variabili di ambiente dal file .env load_dotenv() def numpy_to_pil(image): """Convert a numpy array to a PIL Image.""" if image.dtype == np.uint8: # Most common case mode = "RGB" else: mode = "F" # Floating point return Image.fromarray(image.astype('uint8'), mode) def process_image(image): image = numpy_to_pil(image) # Convert numpy array to PIL Image buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') response = requests.post( os.getenv('BACKEND_URL'), files={"file": ("image.png", base64.b64decode(img_str), "image/png")} ) result = response.json() processed_image_b64 = result["processed_image"] processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64))) return [image, processed_image] # Return the original and processed images # Carica l'esempio di immagine chameleon = load_img("elephant.jpg", output_type="pil") image = gr.Image(label="Upload a photo") output_slider = ImageSlider(label="Processed photo", type="pil") demo = gr.Interface( fn=process_image, inputs=image, outputs=output_slider, title="Magic Eraser", examples=[["elephant.jpg"]] # Esempio locale ) if __name__ == "__main__": demo.launch()