Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
import sys
|
4 |
from pathlib import Path
|
5 |
from PIL import Image
|
6 |
import re
|
7 |
-
|
|
|
8 |
import numpy as np
|
9 |
|
10 |
# Coder: Create directories if they don't exist
|
@@ -13,7 +14,13 @@ if not os.path.exists('saved_prompts'):
|
|
13 |
|
14 |
if not os.path.exists('saved_images'):
|
15 |
os.makedirs('saved_images')
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
# Humanities: Elegant function to generate a safe filename 📝
|
18 |
def generate_safe_filename(text):
|
19 |
return re.sub('[^a-zA-Z0-9]', '_', text)
|
@@ -56,8 +63,8 @@ def list_saved_prompts_and_images():
|
|
56 |
|
57 |
return html_str
|
58 |
|
59 |
-
# Coder: Modified function to save the prompt and image
|
60 |
-
def send_it1(inputs, model_choice):
|
61 |
proc1 = models2[model_choice]
|
62 |
output1 = proc1(inputs)
|
63 |
|
@@ -77,18 +84,21 @@ def send_it1(inputs, model_choice):
|
|
77 |
print(f"Warning: output1 is a string. Cannot save as image. Value: {output1}")
|
78 |
else:
|
79 |
print(f"Warning: Unexpected type {type(output1)} for output1.")
|
80 |
-
|
81 |
-
#Image.fromarray(output1).save(image_path)
|
82 |
-
|
83 |
saved_output.update(list_saved_prompts_and_images())
|
84 |
-
|
85 |
-
return output1
|
86 |
|
|
|
|
|
|
|
|
|
87 |
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
|
|
|
92 |
with gr.Blocks(css=css) as myface:
|
93 |
gr.HTML("""<!DOCTYPE html>
|
94 |
<html lang="en">
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
import sys
|
4 |
from pathlib import Path
|
5 |
from PIL import Image
|
6 |
import re
|
7 |
+
import base64
|
8 |
+
from io import BytesIO
|
9 |
import numpy as np
|
10 |
|
11 |
# Coder: Create directories if they don't exist
|
|
|
14 |
|
15 |
if not os.path.exists('saved_images'):
|
16 |
os.makedirs('saved_images')
|
17 |
+
|
18 |
+
# New addition: Function to convert PIL Image to base64 encoded string
|
19 |
+
def image_to_base64(img):
|
20 |
+
buffered = BytesIO()
|
21 |
+
img.save(buffered, format="PNG")
|
22 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
23 |
+
|
24 |
# Humanities: Elegant function to generate a safe filename 📝
|
25 |
def generate_safe_filename(text):
|
26 |
return re.sub('[^a-zA-Z0-9]', '_', text)
|
|
|
63 |
|
64 |
return html_str
|
65 |
|
66 |
+
# Coder: Modified function to save the prompt and image and create download link
|
67 |
+
def send_it1(inputs, model_choice, download_link):
|
68 |
proc1 = models2[model_choice]
|
69 |
output1 = proc1(inputs)
|
70 |
|
|
|
84 |
print(f"Warning: output1 is a string. Cannot save as image. Value: {output1}")
|
85 |
else:
|
86 |
print(f"Warning: Unexpected type {type(output1)} for output1.")
|
87 |
+
|
|
|
|
|
88 |
saved_output.update(list_saved_prompts_and_images())
|
|
|
|
|
89 |
|
90 |
+
# Encode image as base64
|
91 |
+
img_base64 = ""
|
92 |
+
if isinstance(output1, Image.Image):
|
93 |
+
img_base64 = image_to_base64(output1)
|
94 |
|
95 |
+
# Update HTML component with download link
|
96 |
+
download_html = f'<a href="data:image/png;base64,{img_base64}" download="{safe_filename}.png">Download Image</a>'
|
97 |
+
download_link.update(download_html)
|
98 |
|
99 |
+
return output1
|
|
|
100 |
|
101 |
+
css=""""""
|
102 |
with gr.Blocks(css=css) as myface:
|
103 |
gr.HTML("""<!DOCTYPE html>
|
104 |
<html lang="en">
|