Spaces:
Runtime error
Runtime error
File size: 3,775 Bytes
2264c63 91dce90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import gradio as gr
import base64
import requests
import secrets
import os
from io import BytesIO
from pydub import AudioSegment
def create_chat_session():
r = requests.post("http://121.176.153.117:5000/create")
if (r.status_code != 201):
raise Exception("Failed to create chat session")
# create temp audio folder
session_id = r.json()["id"]
os.makedirs(f"./temp_audio/{session_id}")
return session_id
session_id = create_chat_session()
chat_history = []
def add_text(history, text):
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
def add_audio(history, audio):
audio_bytes = base64.b64decode(audio['data'].split(',')[-1].encode('utf-8'))
audio_file = BytesIO(audio_bytes)
AudioSegment.from_file(audio_file).export(audio_file, format="mp3")
# save audio file temporary to disk
audio_id = secrets.token_hex(8)
AudioSegment.from_file(audio_file).export(f"temp_audio/{session_id}/audio_input_{audio_id}.mp3", format="mp3")
history = history + [((f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",), None)]
response = requests.post(
"http://121.176.153.117:5000/transcribe",
files={'audio': audio_file.getvalue()}
)
if (response.status_code != 200):
raise Exception(response.text)
text = response.json()['text']
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
def bot(history):
if type(history[-1][0]) == str:
message = history[-1][0]
else:
message = history[-2][0]
response = requests.post(
f"http://121.176.153.117:5000/send/text/{session_id}",
headers={'Content-type': 'application/json'},
json={
'message': message,
'role': 'user'
}
)
if (response.status_code != 200):
raise Exception(f"Failed to send message, {response.text}")
response = response.json()
text, audio = response['text'], response['audio']
audio_bytes = base64.b64decode(audio.encode('utf-8'))
audio_file = BytesIO(audio_bytes)
audio_id = secrets.token_hex(8)
AudioSegment.from_file(audio_file).export(f"temp_audio/{session_id}/audio_input_{audio_id}.mp3", format="mp3")
history = history + [(None, (f"temp_audio/{session_id}/audio_input_{audio_id}.mp3",))]
history = history + [(None, text)]
global chat_history
chat_history = history.copy()
return history
def load_chat_history(history):
global chat_history
if len(chat_history) > len(history):
history = chat_history
return history
with gr.Blocks() as demo:
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
demo.load(load_chat_history, [chatbot], [chatbot], queue=False)
with gr.Row():
with gr.Column(scale=0.85):
txt = gr.Textbox(
show_label=False,
placeholder="Enter text and press enter, or record audio",
).style(container=False)
with gr.Column(scale=0.15, min_width=0):
audio = gr.Audio(
source="microphone", type="numpy", show_label=False, format="mp3"
).style(container=False)
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
bot, chatbot, chatbot
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
audio_msg = audio.change(add_audio, [chatbot, audio], [chatbot, audio], queue=False, preprocess=False, postprocess=False).then(
bot, chatbot, chatbot
)
audio_msg.then(lambda: gr.update(interactive=True, value=None), None, [audio], queue=False)
demo.launch(show_error=True) |