Spaces:
Sleeping
Sleeping
sab
commited on
Commit
•
2ccd3fd
1
Parent(s):
e01ff34
test
Browse files- app_bck.py +0 -54
app_bck.py
DELETED
@@ -1,54 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
import base64
|
4 |
-
import os
|
5 |
-
from PIL import Image
|
6 |
-
from io import BytesIO
|
7 |
-
import numpy as np
|
8 |
-
from gradio_imageslider import ImageSlider # Assicurati di avere questa libreria installata
|
9 |
-
from loadimg import load_img # Assicurati che questa funzione sia disponibile
|
10 |
-
|
11 |
-
from dotenv import load_dotenv
|
12 |
-
|
13 |
-
# Carica le variabili di ambiente dal file .env
|
14 |
-
load_dotenv()
|
15 |
-
|
16 |
-
def numpy_to_pil(image):
|
17 |
-
"""Convert a numpy array to a PIL Image."""
|
18 |
-
if image.dtype == np.uint8: # Most common case
|
19 |
-
mode = "RGB"
|
20 |
-
else:
|
21 |
-
mode = "F" # Floating point
|
22 |
-
return Image.fromarray(image.astype('uint8'), mode)
|
23 |
-
|
24 |
-
|
25 |
-
def process_image(image):
|
26 |
-
image = numpy_to_pil(image) # Convert numpy array to PIL Image
|
27 |
-
buffered = BytesIO()
|
28 |
-
image.save(buffered, format="PNG")
|
29 |
-
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
30 |
-
response = requests.post(
|
31 |
-
os.getenv('BACKEND_URL'),
|
32 |
-
files={"file": ("image.png", base64.b64decode(img_str), "image/png")}
|
33 |
-
)
|
34 |
-
result = response.json()
|
35 |
-
processed_image_b64 = result["processed_image"]
|
36 |
-
processed_image = Image.open(BytesIO(base64.b64decode(processed_image_b64)))
|
37 |
-
return [image, processed_image] # Return the original and processed images
|
38 |
-
|
39 |
-
|
40 |
-
# Carica l'esempio di immagine
|
41 |
-
chameleon = load_img("elephant.jpg", output_type="pil")
|
42 |
-
|
43 |
-
image = gr.Image(label="Upload a photo")
|
44 |
-
output_slider = ImageSlider(label="Processed photo", type="pil")
|
45 |
-
demo = gr.Interface(
|
46 |
-
fn=process_image,
|
47 |
-
inputs=image,
|
48 |
-
outputs=output_slider,
|
49 |
-
title="Magic Eraser",
|
50 |
-
examples=[["elephant.jpg"]] # Esempio locale
|
51 |
-
)
|
52 |
-
|
53 |
-
if __name__ == "__main__":
|
54 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|