mavinsao commited on
Commit
f2a853a
·
verified ·
1 Parent(s): 0726ed5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -4,6 +4,8 @@ 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")
@@ -65,6 +67,12 @@ def respond(message, history, stage):
65
 
66
  return response, new_stage
67
 
 
 
 
 
 
 
68
  css = """
69
  .chatbot-header {
70
  display: flex;
@@ -107,10 +115,9 @@ with gr.Blocks(css=css) as demo:
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]
 
4
  from langchain.chains import LLMChain
5
  from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
6
  import hashlib
7
+ from gtts import gTTS
8
+ import tempfile
9
 
10
  # Set up OpenAI API key
11
  os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
 
67
 
68
  return response, new_stage
69
 
70
+ def text_to_speech(text):
71
+ tts = gTTS(text=text, lang='en')
72
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
73
+ tts.save(fp.name)
74
+ return fp.name
75
+
76
  css = """
77
  .chatbot-header {
78
  display: flex;
 
115
  bot_message, new_stage = respond(history[-1][0], history[:-1], stage)
116
  history[-1][1] = bot_message
117
 
118
+ audio_file = text_to_speech(bot_message)
 
119
 
120
+ return history, new_stage, audio_file
121
 
122
  msg.submit(user, [msg, chatbot, state], [msg, chatbot, state], queue=False).then(
123
  bot, [chatbot, state], [chatbot, state, audio_output]