sab commited on
Commit
434b792
·
1 Parent(s): 6fc8f2e
Files changed (2) hide show
  1. app.py +9 -44
  2. app_bck.py +54 -0
app.py CHANGED
@@ -1,54 +1,19 @@
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()
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ os.getenv('BACKEND_URL')
4
+
5
  from gradio_imageslider import ImageSlider # Assicurati di avere questa libreria installata
6
  from loadimg import load_img # Assicurati che questa funzione sia disponibile
7
 
8
  from dotenv import load_dotenv
9
 
10
+ def greet(name, intensity):
11
+ return "Hello, " + name + "!" * int(intensity)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
 
13
  demo = gr.Interface(
14
+ fn=greet,
15
+ inputs=["text", "slider"],
16
+ outputs=["text"],
 
 
17
  )
18
 
19
+ demo.launch()
 
app_bck.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()