Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 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
|
| 8 |
-
from io import BytesIO
|
| 9 |
import numpy as np
|
| 10 |
|
| 11 |
# Coder: Create directories if they don't exist
|
|
@@ -14,13 +13,7 @@ if not os.path.exists('saved_prompts'):
|
|
| 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,8 +56,8 @@ def list_saved_prompts_and_images():
|
|
| 63 |
|
| 64 |
return html_str
|
| 65 |
|
| 66 |
-
# Coder: Modified function to save the prompt and image
|
| 67 |
-
def send_it1(inputs, model_choice
|
| 68 |
proc1 = models2[model_choice]
|
| 69 |
output1 = proc1(inputs)
|
| 70 |
|
|
@@ -84,21 +77,18 @@ def send_it1(inputs, model_choice, download_link):
|
|
| 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 |
-
|
| 89 |
-
|
| 90 |
-
# Encode image as base64
|
| 91 |
-
img_base64 = ""
|
| 92 |
-
if isinstance(output1, Image.Image):
|
| 93 |
-
img_base64 = image_to_base64(output1)
|
| 94 |
|
| 95 |
-
|
| 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">
|
|
@@ -126,11 +116,7 @@ with gr.Blocks(css=css) as myface:
|
|
| 126 |
with gr.Tab("Title"):
|
| 127 |
gr.HTML("""<title>Prompt to Generate Image</title><div style="text-align: center; max-width: 1500px; margin: 0 auto;">
|
| 128 |
<h1>Enter a Prompt in Textbox then click Generate Image</h1>""")
|
| 129 |
-
|
| 130 |
-
with gr.Column(scale=100):
|
| 131 |
-
# New addition: HTML component to hold download link
|
| 132 |
-
download_link = gr.HTML(label="Download Link")
|
| 133 |
-
|
| 134 |
with gr.Tab("Tools"):
|
| 135 |
with gr.Tab("View"):
|
| 136 |
with gr.Row():
|
|
@@ -197,10 +183,7 @@ with gr.Blocks(css=css) as myface:
|
|
| 197 |
|
| 198 |
# Reasoning: Link functions to Gradio components 🎛️
|
| 199 |
model_name1.change(set_model, inputs=model_name1, outputs=[output1])
|
| 200 |
-
|
| 201 |
-
# Reasoning: Link functions to Gradio components 🎛️
|
| 202 |
-
run.click(send_it1, inputs=[magic1, model_name1, download_link], outputs=[output1])
|
| 203 |
-
|
| 204 |
|
| 205 |
myface.queue(concurrency_count=200)
|
| 206 |
myface.launch(inline=True, show_api=False, max_threads=400)
|
|
|
|
| 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 |
+
from PIL import Image
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
|
| 10 |
# Coder: Create directories if they don't exist
|
|
|
|
| 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 |
|
| 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 |
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 |
css=""""""
|
| 90 |
+
|
| 91 |
+
|
| 92 |
with gr.Blocks(css=css) as myface:
|
| 93 |
gr.HTML("""<!DOCTYPE html>
|
| 94 |
<html lang="en">
|
|
|
|
| 116 |
with gr.Tab("Title"):
|
| 117 |
gr.HTML("""<title>Prompt to Generate Image</title><div style="text-align: center; max-width: 1500px; margin: 0 auto;">
|
| 118 |
<h1>Enter a Prompt in Textbox then click Generate Image</h1>""")
|
| 119 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
with gr.Tab("Tools"):
|
| 121 |
with gr.Tab("View"):
|
| 122 |
with gr.Row():
|
|
|
|
| 183 |
|
| 184 |
# Reasoning: Link functions to Gradio components 🎛️
|
| 185 |
model_name1.change(set_model, inputs=model_name1, outputs=[output1])
|
| 186 |
+
run.click(send_it1, inputs=[magic1, model_name1], outputs=[output1])
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
myface.queue(concurrency_count=200)
|
| 189 |
myface.launch(inline=True, show_api=False, max_threads=400)
|