Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.chains import LLMChain
|
5 |
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
|
|
6 |
|
7 |
# Set up OpenAI API key
|
8 |
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
@@ -97,6 +98,7 @@ with gr.Blocks(css=css) as demo:
|
|
97 |
msg = gr.Textbox()
|
98 |
clear = gr.Button("Clear")
|
99 |
state = gr.State("initial")
|
|
|
100 |
|
101 |
def user(user_message, history, stage):
|
102 |
return "", history + [[user_message, None]], stage
|
@@ -104,12 +106,16 @@ with gr.Blocks(css=css) as demo:
|
|
104 |
def bot(history, stage):
|
105 |
bot_message, new_stage = respond(history[-1][0], history[:-1], stage)
|
106 |
history[-1][1] = bot_message
|
107 |
-
|
|
|
|
|
|
|
|
|
108 |
|
109 |
msg.submit(user, [msg, chatbot, state], [msg, chatbot, state], queue=False).then(
|
110 |
-
bot, [chatbot, state], [chatbot, state]
|
111 |
)
|
112 |
-
clear.click(lambda: ([], "initial"), None, [chatbot, state], queue=False)
|
113 |
|
114 |
gr.Examples(
|
115 |
examples=[
|
|
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.chains import LLMChain
|
5 |
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
|
6 |
+
import hashlib
|
7 |
|
8 |
# Set up OpenAI API key
|
9 |
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
|
|
|
98 |
msg = gr.Textbox()
|
99 |
clear = gr.Button("Clear")
|
100 |
state = gr.State("initial")
|
101 |
+
audio_output = gr.Audio(label="Response Audio", autoplay=True)
|
102 |
|
103 |
def user(user_message, history, stage):
|
104 |
return "", history + [[user_message, None]], stage
|
|
|
106 |
def bot(history, stage):
|
107 |
bot_message, new_stage = respond(history[-1][0], history[:-1], stage)
|
108 |
history[-1][1] = bot_message
|
109 |
+
|
110 |
+
# Generate a unique, short filename for the audio
|
111 |
+
filename = hashlib.md5(bot_message.encode()).hexdigest()[:10] + ".wav"
|
112 |
+
|
113 |
+
return history, new_stage, (filename, bot_message)
|
114 |
|
115 |
msg.submit(user, [msg, chatbot, state], [msg, chatbot, state], queue=False).then(
|
116 |
+
bot, [chatbot, state], [chatbot, state, audio_output]
|
117 |
)
|
118 |
+
clear.click(lambda: ([], "initial", None), None, [chatbot, state, audio_output], queue=False)
|
119 |
|
120 |
gr.Examples(
|
121 |
examples=[
|