Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,62 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import time
|
| 3 |
-
from imgen import *
|
| 4 |
-
|
| 5 |
-
def print_like_dislike(x: gr.LikeData):
|
| 6 |
-
print(x.index, x.value, x.liked)
|
| 7 |
-
|
| 8 |
-
def add_message(history, message):
|
| 9 |
-
if len(message["files"]) > 0:
|
| 10 |
-
history.append((message["files"], None))
|
| 11 |
-
if message["text"] is not None and message["text"] != "":
|
| 12 |
-
history.append((message["text"], None))
|
| 13 |
-
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def bot(history):
|
| 18 |
-
if type(history[-1][0]) != tuple:
|
| 19 |
-
try:
|
| 20 |
-
prompt = history[-1][0]
|
| 21 |
-
image = pipeline(prompt).images[0]
|
| 22 |
-
image.save("generated_image.png")
|
| 23 |
-
response = ("generated_img.png",)
|
| 24 |
-
history[-1][1] = response
|
| 25 |
-
yield history
|
| 26 |
-
except Exception as e:
|
| 27 |
-
response = f"Sorry, the error '{e}' occured while generating the response; check [troubleshooting documentation](https://astrabert.github.io/awesome-tiny-sd/#troubleshooting) for more"
|
| 28 |
-
history[-1][1] = ""
|
| 29 |
-
for character in response:
|
| 30 |
-
history[-1][1] += character
|
| 31 |
-
time.sleep(0.05)
|
| 32 |
-
yield history
|
| 33 |
-
if type(history[-1][0]) == tuple:
|
| 34 |
-
response = f"Sorry, this version still does not support uploaded files :("
|
| 35 |
-
history[-1][1] = ""
|
| 36 |
-
for character in response:
|
| 37 |
-
history[-1][1] += character
|
| 38 |
-
time.sleep(0.05)
|
| 39 |
-
yield history
|
| 40 |
-
|
| 41 |
-
with gr.Blocks() as demo:
|
| 42 |
-
chatbot = gr.Chatbot(
|
| 43 |
-
[[None,
|
| 44 |
-
label="awesome-tiny-sd",
|
| 45 |
-
elem_id="chatbot",
|
| 46 |
-
bubble_full_width=False,
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["pdf"], placeholder="Enter your image-generating prompt...", show_label=False)
|
| 50 |
-
|
| 51 |
-
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 52 |
-
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
| 53 |
-
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 54 |
-
|
| 55 |
-
chatbot.like(print_like_dislike, None, None)
|
| 56 |
-
clear = gr.ClearButton(chatbot)
|
| 57 |
-
|
| 58 |
-
demo.queue()
|
| 59 |
-
if __name__ == "__main__":
|
| 60 |
-
demo.launch()
|
| 61 |
-
|
| 62 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
from imgen import *
|
| 4 |
+
|
| 5 |
+
def print_like_dislike(x: gr.LikeData):
|
| 6 |
+
print(x.index, x.value, x.liked)
|
| 7 |
+
|
| 8 |
+
def add_message(history, message):
|
| 9 |
+
if len(message["files"]) > 0:
|
| 10 |
+
history.append((message["files"], None))
|
| 11 |
+
if message["text"] is not None and message["text"] != "":
|
| 12 |
+
history.append((message["text"], None))
|
| 13 |
+
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def bot(history):
|
| 18 |
+
if type(history[-1][0]) != tuple:
|
| 19 |
+
try:
|
| 20 |
+
prompt = history[-1][0]
|
| 21 |
+
image = pipeline(prompt).images[0]
|
| 22 |
+
image.save("generated_image.png")
|
| 23 |
+
response = ("generated_img.png",)
|
| 24 |
+
history[-1][1] = response
|
| 25 |
+
yield history
|
| 26 |
+
except Exception as e:
|
| 27 |
+
response = f"Sorry, the error '{e}' occured while generating the response; check [troubleshooting documentation](https://astrabert.github.io/awesome-tiny-sd/#troubleshooting) for more"
|
| 28 |
+
history[-1][1] = ""
|
| 29 |
+
for character in response:
|
| 30 |
+
history[-1][1] += character
|
| 31 |
+
time.sleep(0.05)
|
| 32 |
+
yield history
|
| 33 |
+
if type(history[-1][0]) == tuple:
|
| 34 |
+
response = f"Sorry, this version still does not support uploaded files :("
|
| 35 |
+
history[-1][1] = ""
|
| 36 |
+
for character in response:
|
| 37 |
+
history[-1][1] += character
|
| 38 |
+
time.sleep(0.05)
|
| 39 |
+
yield history
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
chatbot = gr.Chatbot(
|
| 43 |
+
[[None, "Hi, I am awesome-tiny-sd, a little stable diffusion model that lets you generate images:blush:\nJust write me a prompt, I'll generate what you ask for:heart:"]],
|
| 44 |
+
label="awesome-tiny-sd",
|
| 45 |
+
elem_id="chatbot",
|
| 46 |
+
bubble_full_width=False,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
chat_input = gr.MultimodalTextbox(interactive=True, file_types=["pdf"], placeholder="Enter your image-generating prompt...", show_label=False)
|
| 50 |
+
|
| 51 |
+
chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
|
| 52 |
+
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
|
| 53 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
| 54 |
+
|
| 55 |
+
chatbot.like(print_like_dislike, None, None)
|
| 56 |
+
clear = gr.ClearButton(chatbot)
|
| 57 |
+
|
| 58 |
+
demo.queue()
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|
| 61 |
+
|
| 62 |
|