Spaces:
Runtime error
Runtime error
Create run_gradio_demo.py
Browse files- run_gradio_demo.py +131 -0
run_gradio_demo.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
from diffusers.utils import load_image
|
| 5 |
+
import requests
|
| 6 |
+
from awesome_chat import chat_huggingface
|
| 7 |
+
|
| 8 |
+
all_messages = []
|
| 9 |
+
OPENAI_KEY = ""
|
| 10 |
+
|
| 11 |
+
def add_message(content, role):
|
| 12 |
+
message = {"role":role, "content":content}
|
| 13 |
+
all_messages.append(message)
|
| 14 |
+
|
| 15 |
+
def extract_medias(message):
|
| 16 |
+
image_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(jpg|jpeg|tiff|gif|png)")
|
| 17 |
+
image_urls = []
|
| 18 |
+
for match in image_pattern.finditer(message):
|
| 19 |
+
if match.group(0) not in image_urls:
|
| 20 |
+
image_urls.append(match.group(0))
|
| 21 |
+
|
| 22 |
+
audio_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(flac|wav)")
|
| 23 |
+
audio_urls = []
|
| 24 |
+
for match in audio_pattern.finditer(message):
|
| 25 |
+
if match.group(0) not in audio_urls:
|
| 26 |
+
audio_urls.append(match.group(0))
|
| 27 |
+
|
| 28 |
+
video_pattern = re.compile(r"(http(s?):|\/)?([\.\/_\w:-])*?\.(mp4)")
|
| 29 |
+
video_urls = []
|
| 30 |
+
for match in video_pattern.finditer(message):
|
| 31 |
+
if match.group(0) not in video_urls:
|
| 32 |
+
video_urls.append(match.group(0))
|
| 33 |
+
|
| 34 |
+
return image_urls, audio_urls, video_urls
|
| 35 |
+
|
| 36 |
+
def set_openai_key(openai_key):
|
| 37 |
+
global OPENAI_KEY
|
| 38 |
+
OPENAI_KEY = openai_key
|
| 39 |
+
return OPENAI_KEY
|
| 40 |
+
|
| 41 |
+
def add_text(messages, message):
|
| 42 |
+
if len(OPENAI_KEY) == 0 or not OPENAI_KEY.startswith("sk-"):
|
| 43 |
+
return messages, "Please set your OpenAI API key first."
|
| 44 |
+
add_message(message, "user")
|
| 45 |
+
messages = messages + [(message, None)]
|
| 46 |
+
image_urls, audio_urls, video_urls = extract_medias(message)
|
| 47 |
+
|
| 48 |
+
for image_url in image_urls:
|
| 49 |
+
if not image_url.startswith("http"):
|
| 50 |
+
image_url = "public/" + image_url
|
| 51 |
+
image = load_image(image_url)
|
| 52 |
+
name = f"public/images/{str(uuid.uuid4())[:4]}.jpg"
|
| 53 |
+
image.save(name)
|
| 54 |
+
messages = messages + [((f"{name}",), None)]
|
| 55 |
+
for audio_url in audio_urls:
|
| 56 |
+
if not audio_url.startswith("http"):
|
| 57 |
+
audio_url = "public/" + audio_url
|
| 58 |
+
ext = audio_url.split(".")[-1]
|
| 59 |
+
name = f"public/audios/{str(uuid.uuid4()[:4])}.{ext}"
|
| 60 |
+
response = requests.get(audio_url)
|
| 61 |
+
with open(name, "wb") as f:
|
| 62 |
+
f.write(response.content)
|
| 63 |
+
messages = messages + [((f"{name}",), None)]
|
| 64 |
+
for video_url in video_urls:
|
| 65 |
+
if not video_url.startswith("http"):
|
| 66 |
+
video_url = "public/" + video_url
|
| 67 |
+
ext = video_url.split(".")[-1]
|
| 68 |
+
name = f"public/audios/{str(uuid.uuid4()[:4])}.{ext}"
|
| 69 |
+
response = requests.get(video_url)
|
| 70 |
+
with open(name, "wb") as f:
|
| 71 |
+
f.write(response.content)
|
| 72 |
+
messages = messages + [((f"{name}",), None)]
|
| 73 |
+
return messages, ""
|
| 74 |
+
|
| 75 |
+
def bot(messages):
|
| 76 |
+
if len(OPENAI_KEY) == 0 or not OPENAI_KEY.startswith("sk-"):
|
| 77 |
+
return messages
|
| 78 |
+
message = chat_huggingface(all_messages, OPENAI_KEY, "openai", "https://api.openai.com/v1/completions")["message"]
|
| 79 |
+
image_urls, audio_urls, video_urls = extract_medias(message)
|
| 80 |
+
add_message(message, "assistant")
|
| 81 |
+
messages[-1][1] = message
|
| 82 |
+
for image_url in image_urls:
|
| 83 |
+
if not image_url.startswith("http"):
|
| 84 |
+
image_url = image_url.replace("public/", "")
|
| 85 |
+
messages = messages + [((None, (f"public/{image_url}",)))]
|
| 86 |
+
for audio_url in audio_urls:
|
| 87 |
+
if not audio_url.startswith("http"):
|
| 88 |
+
audio_url = audio_url.replace("public/", "")
|
| 89 |
+
messages = messages + [((None, (f"public/{audio_url}",)))]
|
| 90 |
+
for video_url in video_urls:
|
| 91 |
+
if not video_url.startswith("http"):
|
| 92 |
+
video_url = video_url.replace("public/", "")
|
| 93 |
+
messages = messages + [((None, (f"public/{video_url}",)))]
|
| 94 |
+
return messages
|
| 95 |
+
|
| 96 |
+
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("<h2><center>HuggingSPACES (Dev)</center></h2>")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
openai_api_key = gr.Textbox(
|
| 100 |
+
show_label=False,
|
| 101 |
+
placeholder="Set your OpenAI API key here and press Enter",
|
| 102 |
+
lines=1,
|
| 103 |
+
type="password",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=500)
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
txt = gr.Textbox(
|
| 110 |
+
show_label=False,
|
| 111 |
+
placeholder="Enter text and press enter. The url of the multimedia resource must contain the extension name.",
|
| 112 |
+
).style(container=False)
|
| 113 |
+
|
| 114 |
+
txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
|
| 115 |
+
bot, chatbot, chatbot
|
| 116 |
+
)
|
| 117 |
+
openai_api_key.submit(set_openai_key, [openai_api_key], [openai_api_key])
|
| 118 |
+
|
| 119 |
+
gr.Examples(
|
| 120 |
+
examples=["Given a collection of image A: /examples/a.jpg, B: /examples/b.jpg, C: /examples/c.jpg, please tell me how many zebras in these picture?",
|
| 121 |
+
"Please generate a canny image based on /examples/f.jpg",
|
| 122 |
+
"show me a joke and an image of cat",
|
| 123 |
+
"what is in the /examples/a.jpg",
|
| 124 |
+
"generate a video and audio about a dog is running on the grass",
|
| 125 |
+
"based on the /examples/a.jpg, please generate a video and audio",
|
| 126 |
+
"based on pose of /examples/d.jpg and content of /examples/e.jpg, please show me a new image",
|
| 127 |
+
],
|
| 128 |
+
inputs=txt
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
demo.launch()
|