Spaces:
Sleeping
Sleeping
new chatbot interface
Browse files- app.py +43 -78
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -12,10 +12,6 @@ log_to_console = False
|
|
| 12 |
|
| 13 |
temp_files = []
|
| 14 |
|
| 15 |
-
# constants
|
| 16 |
-
image_embed_prefix = "๐ผ๏ธ๐ "
|
| 17 |
-
audio_embed_prefix = "๐๏ธ๐ "
|
| 18 |
-
|
| 19 |
def encode_image(image_data):
|
| 20 |
"""Generates a prefix for image base64 data in the required format for the
|
| 21 |
four known image formats: png, jpeg, gif, and webp.
|
|
@@ -49,10 +45,6 @@ def encode_image(image_data):
|
|
| 49 |
|
| 50 |
return f"data:image/{image_type};base64,{base64.b64encode(image_data).decode('utf-8')}"
|
| 51 |
|
| 52 |
-
def add_text(history, text):
|
| 53 |
-
history = history + [(text, None)]
|
| 54 |
-
return history, gr.Textbox(value="", interactive=False)
|
| 55 |
-
|
| 56 |
def add_file(history, files):
|
| 57 |
for file in files:
|
| 58 |
if file.name.endswith(".docx"):
|
|
@@ -73,27 +65,6 @@ def add_file(history, files):
|
|
| 73 |
|
| 74 |
return history
|
| 75 |
|
| 76 |
-
def add_img(history, files):
|
| 77 |
-
for file in files:
|
| 78 |
-
temp_files.append(file.name)
|
| 79 |
-
|
| 80 |
-
if log_to_console:
|
| 81 |
-
print(f"add_img {file.name}")
|
| 82 |
-
|
| 83 |
-
if file.name.endswith((".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm")):
|
| 84 |
-
prefix = audio_embed_prefix
|
| 85 |
-
else:
|
| 86 |
-
prefix = image_embed_prefix
|
| 87 |
-
|
| 88 |
-
history = history + [(prefix + file.name, None)]
|
| 89 |
-
|
| 90 |
-
gr.Info(f"Media added as {file.name}")
|
| 91 |
-
|
| 92 |
-
return history
|
| 93 |
-
|
| 94 |
-
def submit_text(txt_value):
|
| 95 |
-
return add_text([chatbot, txt_value], [chatbot, txt_value])
|
| 96 |
-
|
| 97 |
def undo(history):
|
| 98 |
history.pop()
|
| 99 |
return history
|
|
@@ -127,8 +98,8 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
|
|
| 127 |
whisper_prompt = system_prompt
|
| 128 |
for human, assi in history:
|
| 129 |
if human is not None:
|
| 130 |
-
if human
|
| 131 |
-
audio_fn = human
|
| 132 |
with open(audio_fn, "rb") as f:
|
| 133 |
transcription = client.audio.transcriptions.create(
|
| 134 |
model="whisper-1",
|
|
@@ -156,11 +127,31 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
|
|
| 156 |
history_openai_format.append({"role": "system", "content": system_prompt})
|
| 157 |
for human, assi in history:
|
| 158 |
if human is not None:
|
| 159 |
-
if human
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
else:
|
| 165 |
user_msg_parts.append({"type": "text", "text": human})
|
| 166 |
|
|
@@ -171,11 +162,16 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
|
|
| 171 |
|
| 172 |
history_openai_format.append({"role": "assistant", "content": assi})
|
| 173 |
|
| 174 |
-
if message:
|
| 175 |
-
user_msg_parts.append({"type": "text", "text":
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
if log_to_console:
|
| 181 |
print(f"br_prompt: {str(history_openai_format)}")
|
|
@@ -193,14 +189,13 @@ def bot(message, history, oai_key, system_prompt, seed, temperature, max_tokens,
|
|
| 193 |
|
| 194 |
result = response.choices[0].message.content
|
| 195 |
|
| 196 |
-
history[-1][1] = result
|
| 197 |
if log_to_console:
|
| 198 |
print(f"br_result: {str(history)}")
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
raise gr.Error(f"Error: {str(e)}")
|
| 202 |
|
| 203 |
-
return
|
| 204 |
|
| 205 |
def import_history(history, file):
|
| 206 |
with open(file.name, mode="rb") as f:
|
|
@@ -279,34 +274,11 @@ with gr.Blocks(delete_cache=(86400, 86400)) as demo:
|
|
| 279 |
dl_settings_button.click(None, controls, js=generate_download_settings_js("oai_chat_settings.bin", control_ids))
|
| 280 |
ul_settings_button.click(None, None, None, js=generate_upload_settings_js(control_ids))
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
)
|
| 288 |
-
|
| 289 |
-
with gr.Row():
|
| 290 |
-
btn = gr.UploadButton("๐ Upload", size="sm", file_count="multiple")
|
| 291 |
-
img_btn = gr.UploadButton("๐ผ๏ธ Upload", size="sm", file_count="multiple", file_types=["image", "audio"])
|
| 292 |
-
undo_btn = gr.Button("โฉ๏ธ Undo")
|
| 293 |
-
undo_btn.click(undo, inputs=[chatbot], outputs=[chatbot])
|
| 294 |
-
|
| 295 |
-
clear = gr.ClearButton(chatbot, value="๐๏ธ Clear")
|
| 296 |
-
|
| 297 |
-
with gr.Row():
|
| 298 |
-
txt = gr.TextArea(
|
| 299 |
-
scale=4,
|
| 300 |
-
show_label=False,
|
| 301 |
-
placeholder="Enter text and press enter, or upload a file",
|
| 302 |
-
container=False,
|
| 303 |
-
lines=3,
|
| 304 |
-
)
|
| 305 |
-
submit_btn = gr.Button("๐ Send", scale=0)
|
| 306 |
-
submit_click = submit_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 307 |
-
bot, [txt, chatbot, oai_key, system_prompt, seed, temp, max_tokens, model], [txt, chatbot],
|
| 308 |
-
)
|
| 309 |
-
submit_click.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
| 310 |
|
| 311 |
if dump_controls:
|
| 312 |
with gr.Row():
|
|
@@ -314,13 +286,6 @@ with gr.Blocks(delete_cache=(86400, 86400)) as demo:
|
|
| 314 |
txt_dmp = gr.Textbox("Dump")
|
| 315 |
dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])
|
| 316 |
|
| 317 |
-
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 318 |
-
bot, [txt, chatbot, oai_key, system_prompt, seed, temp, max_tokens, model], [txt, chatbot],
|
| 319 |
-
)
|
| 320 |
-
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
| 321 |
-
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False, postprocess=False)
|
| 322 |
-
img_msg = img_btn.upload(add_img, [chatbot, img_btn], [chatbot], queue=False, postprocess=False)
|
| 323 |
-
|
| 324 |
with gr.Accordion("Import/Export", open = False):
|
| 325 |
import_button = gr.UploadButton("History Import")
|
| 326 |
export_button = gr.Button("History Export")
|
|
|
|
| 12 |
|
| 13 |
temp_files = []
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def encode_image(image_data):
|
| 16 |
"""Generates a prefix for image base64 data in the required format for the
|
| 17 |
four known image formats: png, jpeg, gif, and webp.
|
|
|
|
| 45 |
|
| 46 |
return f"data:image/{image_type};base64,{base64.b64encode(image_data).decode('utf-8')}"
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def add_file(history, files):
|
| 49 |
for file in files:
|
| 50 |
if file.name.endswith(".docx"):
|
|
|
|
| 65 |
|
| 66 |
return history
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def undo(history):
|
| 69 |
history.pop()
|
| 70 |
return history
|
|
|
|
| 98 |
whisper_prompt = system_prompt
|
| 99 |
for human, assi in history:
|
| 100 |
if human is not None:
|
| 101 |
+
if type(human) is tuple:
|
| 102 |
+
audio_fn = human[0]
|
| 103 |
with open(audio_fn, "rb") as f:
|
| 104 |
transcription = client.audio.transcriptions.create(
|
| 105 |
model="whisper-1",
|
|
|
|
| 127 |
history_openai_format.append({"role": "system", "content": system_prompt})
|
| 128 |
for human, assi in history:
|
| 129 |
if human is not None:
|
| 130 |
+
if type(human) is tuple:
|
| 131 |
+
fn = human[0]
|
| 132 |
+
if fn.endswith(".docx"):
|
| 133 |
+
content = process_docx(fn)
|
| 134 |
+
else:
|
| 135 |
+
with open(fn, mode="rb") as f:
|
| 136 |
+
content = f.read()
|
| 137 |
+
|
| 138 |
+
isImage = False
|
| 139 |
+
if isinstance(content, bytes):
|
| 140 |
+
try:
|
| 141 |
+
# try to add as image
|
| 142 |
+
content = encode_image(content)
|
| 143 |
+
isImage = True
|
| 144 |
+
except:
|
| 145 |
+
# not an image, try text
|
| 146 |
+
content = content.decode('utf-8', 'replace')
|
| 147 |
+
else:
|
| 148 |
+
content = str(content)
|
| 149 |
+
|
| 150 |
+
if isImage:
|
| 151 |
+
user_msg_parts.append({"type": "image_url",
|
| 152 |
+
"image_url":{"url": content}})
|
| 153 |
+
else:
|
| 154 |
+
user_msg_parts.append({"type": "text", "text": content})
|
| 155 |
else:
|
| 156 |
user_msg_parts.append({"type": "text", "text": human})
|
| 157 |
|
|
|
|
| 162 |
|
| 163 |
history_openai_format.append({"role": "assistant", "content": assi})
|
| 164 |
|
| 165 |
+
if message['text']:
|
| 166 |
+
user_msg_parts.append({"type": "text", "text": message['text']})
|
| 167 |
+
if message['files']:
|
| 168 |
+
for file in message['files']:
|
| 169 |
+
with open(file['path'], mode="rb") as f:
|
| 170 |
+
content = f.read()
|
| 171 |
+
user_msg_parts.append({"type": "image_url",
|
| 172 |
+
"image_url":{"url": encode_image(content)}})
|
| 173 |
+
history_openai_format.append({"role": "user", "content": user_msg_parts})
|
| 174 |
+
user_msg_parts = []
|
| 175 |
|
| 176 |
if log_to_console:
|
| 177 |
print(f"br_prompt: {str(history_openai_format)}")
|
|
|
|
| 189 |
|
| 190 |
result = response.choices[0].message.content
|
| 191 |
|
|
|
|
| 192 |
if log_to_console:
|
| 193 |
print(f"br_result: {str(history)}")
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
raise gr.Error(f"Error: {str(e)}")
|
| 197 |
|
| 198 |
+
return result
|
| 199 |
|
| 200 |
def import_history(history, file):
|
| 201 |
with open(file.name, mode="rb") as f:
|
|
|
|
| 274 |
dl_settings_button.click(None, controls, js=generate_download_settings_js("oai_chat_settings.bin", control_ids))
|
| 275 |
ul_settings_button.click(None, None, None, js=generate_upload_settings_js(control_ids))
|
| 276 |
|
| 277 |
+
chat = gr.ChatInterface(fn=bot, multimodal=True, additional_inputs=controls, retry_btn = None, autofocus = False)
|
| 278 |
+
chat.textbox.file_count = "multiple"
|
| 279 |
+
chatbot = chat.chatbot
|
| 280 |
+
chatbot.show_copy_button = True
|
| 281 |
+
chatbot.height = 350
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
if dump_controls:
|
| 284 |
with gr.Row():
|
|
|
|
| 286 |
txt_dmp = gr.Textbox("Dump")
|
| 287 |
dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])
|
| 288 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
with gr.Accordion("Import/Export", open = False):
|
| 290 |
import_button = gr.UploadButton("History Import")
|
| 291 |
export_button = gr.Button("History Export")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
gradio >= 4.
|
| 2 |
openai >= 1.0.0
|
| 3 |
lxml
|
|
|
|
| 1 |
+
gradio >= 4.38.1
|
| 2 |
openai >= 1.0.0
|
| 3 |
lxml
|