Spaces:
Runtime error
Runtime error
Commit
Β·
17e82fc
1
Parent(s):
13f6fc8
support streaming output
Browse files- app.py +14 -7
- panda_gpt.py +24 -5
app.py
CHANGED
|
@@ -9,8 +9,6 @@ panda_path = hf_hub_download(repo_id="openllmplayground/pandagpt_13b_max_len_400
|
|
| 9 |
a = PandaGPT((vicuna_path,))
|
| 10 |
a.load_projection(panda_path)
|
| 11 |
|
| 12 |
-
import gradio as gr
|
| 13 |
-
|
| 14 |
|
| 15 |
def add_text(history, text):
|
| 16 |
history = history + [(text, None)]
|
|
@@ -39,31 +37,40 @@ def bot(history):
|
|
| 39 |
audio_paths = []
|
| 40 |
video_paths = []
|
| 41 |
if len(image_paths) == 0 and len(audio_paths) == 0 and len(video_paths) == 0:
|
| 42 |
-
response = a.
|
| 43 |
else:
|
| 44 |
-
response = a.
|
| 45 |
-
history[-1][1] =
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
-
with gr.Column(scale=0.
|
| 54 |
txt = gr.Textbox(
|
| 55 |
show_label=False,
|
| 56 |
placeholder="Enter text and press enter, or upload an image",
|
| 57 |
).style(container=False)
|
| 58 |
with gr.Column(scale=0.15, min_width=0):
|
| 59 |
btn = gr.UploadButton("π", file_types=["image", "video", "audio"])
|
|
|
|
|
|
|
| 60 |
|
| 61 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 62 |
bot, chatbot, chatbot
|
| 63 |
)
|
| 64 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
| 65 |
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False)
|
|
|
|
| 66 |
|
|
|
|
| 67 |
demo.launch()
|
| 68 |
|
| 69 |
|
|
|
|
| 9 |
a = PandaGPT((vicuna_path,))
|
| 10 |
a.load_projection(panda_path)
|
| 11 |
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def add_text(history, text):
|
| 14 |
history = history + [(text, None)]
|
|
|
|
| 37 |
audio_paths = []
|
| 38 |
video_paths = []
|
| 39 |
if len(image_paths) == 0 and len(audio_paths) == 0 and len(video_paths) == 0:
|
| 40 |
+
response = a.eval_with_image(None, text)
|
| 41 |
else:
|
| 42 |
+
response = a.eval_with_image({"image_paths": image_paths,"audio_paths": audio_paths, "video_paths": video_paths}, text)
|
| 43 |
+
history[-1][1] = ""
|
| 44 |
+
for i in a.generate():
|
| 45 |
+
history[-1][1] += i
|
| 46 |
+
yield history
|
| 47 |
+
if history[-1][1].endswith("###"):
|
| 48 |
+
history[-1][1] = history[-1][1][:-3]
|
| 49 |
+
yield history
|
| 50 |
|
| 51 |
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
+
with gr.Column(scale=0.65):
|
| 57 |
txt = gr.Textbox(
|
| 58 |
show_label=False,
|
| 59 |
placeholder="Enter text and press enter, or upload an image",
|
| 60 |
).style(container=False)
|
| 61 |
with gr.Column(scale=0.15, min_width=0):
|
| 62 |
btn = gr.UploadButton("π", file_types=["image", "video", "audio"])
|
| 63 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 64 |
+
btn2 = gr.Button("reset")
|
| 65 |
|
| 66 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 67 |
bot, chatbot, chatbot
|
| 68 |
)
|
| 69 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
| 70 |
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False)
|
| 71 |
+
btn2.click(a.reset, None, chatbot, queue=False)
|
| 72 |
|
| 73 |
+
demo.queue()
|
| 74 |
demo.launch()
|
| 75 |
|
| 76 |
|
panda_gpt.py
CHANGED
|
@@ -40,7 +40,7 @@ class PandaGPT:
|
|
| 40 |
s = self.model.tokenize(s.encode())
|
| 41 |
self.model.eval(s)
|
| 42 |
|
| 43 |
-
def generate_with_print(self, end="###"):
|
| 44 |
end = end.encode()
|
| 45 |
ret = b""
|
| 46 |
for i in range(self.max_tgt_len):
|
|
@@ -48,7 +48,7 @@ class PandaGPT:
|
|
| 48 |
self.model.eval([token])
|
| 49 |
txt = self.model.detokenize([token])
|
| 50 |
ret += txt
|
| 51 |
-
|
| 52 |
if ret.endswith(end):
|
| 53 |
break
|
| 54 |
return ret.decode(errors="replace")
|
|
@@ -70,6 +70,24 @@ class PandaGPT:
|
|
| 70 |
return self.chat_with_image(None, question)
|
| 71 |
|
| 72 |
def chat_with_image(self, inputs, question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if self.generated_text == "":
|
| 74 |
self.eval_string("###")
|
| 75 |
self.eval_string(" Human: ")
|
|
@@ -77,9 +95,10 @@ class PandaGPT:
|
|
| 77 |
self.eval_inputs(inputs)
|
| 78 |
self.eval_string(question)
|
| 79 |
self.eval_string("\n### Assistant:")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
def extract_multimoal_feature(self, inputs):
|
| 85 |
features = []
|
|
|
|
| 40 |
s = self.model.tokenize(s.encode())
|
| 41 |
self.model.eval(s)
|
| 42 |
|
| 43 |
+
def generate_with_print(self, end="###", hook=lambda x: print(x,flush=True, end="")):
|
| 44 |
end = end.encode()
|
| 45 |
ret = b""
|
| 46 |
for i in range(self.max_tgt_len):
|
|
|
|
| 48 |
self.model.eval([token])
|
| 49 |
txt = self.model.detokenize([token])
|
| 50 |
ret += txt
|
| 51 |
+
hook(txt.decode(errors="replace"))
|
| 52 |
if ret.endswith(end):
|
| 53 |
break
|
| 54 |
return ret.decode(errors="replace")
|
|
|
|
| 70 |
return self.chat_with_image(None, question)
|
| 71 |
|
| 72 |
def chat_with_image(self, inputs, question):
|
| 73 |
+
self.eval_with_image(inputs, question)
|
| 74 |
+
ret = self.generate_with_print(end="###")
|
| 75 |
+
self.generated_text += ret
|
| 76 |
+
return ret
|
| 77 |
+
|
| 78 |
+
def generate(self, end="###"):
|
| 79 |
+
end = end.encode()
|
| 80 |
+
ret = b""
|
| 81 |
+
for i in range(self.max_tgt_len):
|
| 82 |
+
token = self.model.sample()
|
| 83 |
+
self.model.eval([token])
|
| 84 |
+
txt = self.model.detokenize([token])
|
| 85 |
+
ret += txt
|
| 86 |
+
yield txt.decode(errors="replace")
|
| 87 |
+
if ret.endswith(end):
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
def eval_with_image(self, inputs, question):
|
| 91 |
if self.generated_text == "":
|
| 92 |
self.eval_string("###")
|
| 93 |
self.eval_string(" Human: ")
|
|
|
|
| 95 |
self.eval_inputs(inputs)
|
| 96 |
self.eval_string(question)
|
| 97 |
self.eval_string("\n### Assistant:")
|
| 98 |
+
|
| 99 |
+
def reset(self):
|
| 100 |
+
self.generated_text = ""
|
| 101 |
+
self.model.reset()
|
| 102 |
|
| 103 |
def extract_multimoal_feature(self, inputs):
|
| 104 |
features = []
|