ylacombe commited on
Commit
fbacfdf
1 Parent(s): 1b6c331

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -89
app.py CHANGED
@@ -17,23 +17,12 @@ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1", gpu=True)
17
 
18
  title = "Speak with Llama2 70B"
19
 
20
- DESCRIPTION = """# Speak with Llama2 70B
21
-
22
- This Space demonstrates how to speak to a chatbot, based solely on open-source models.
23
- It relies on 3 models:
24
- 1. [Whisper-large-v2](https://huggingface.co/spaces/sanchit-gandhi/whisper-large-v2) as an ASR model, to transcribe recorded audio to text. It is called through a [gradio client](https://www.gradio.app/docs/client).
25
- 2. [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) as the chat model, the actual chat model. It is also called through a [gradio client](https://www.gradio.app/docs/client).
26
- 3. [Coqui's XTTS](https://huggingface.co/spaces/coqui/xtts) as a TTS model, to generate the chatbot answers. This time, the model is hosted locally.
27
-
28
-
29
- Note:
30
- - As a derivate work of [Llama-2-70b-chat](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta,
31
- this demo is governed by the original [license](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/USE_POLICY.md).
32
- - By using this demo you agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml
33
- """
34
  css = """.toast-wrap { display: none !important } """
35
 
36
 
 
 
37
  system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
38
  temperature = 0.9
39
  top_p = 0.6
@@ -67,85 +56,67 @@ def transcribe(wav_path):
67
 
68
 
69
  def add_text(history, text, agree):
70
- if agree == True:
71
- history = [] if history is None else history
72
- history = history + [(text, None)]
73
- return history, gr.update(value="", interactive=False)
74
- else:
75
- gr.Warning("Please accept the Terms & Condition!")
76
- return None, gr.update(value="", interactive=True)
77
 
78
  def add_file(history, file, agree):
79
- if agree == True:
80
- history = [] if history is None else history
81
- text = transcribe(
82
- file
83
- )
84
-
85
- history = history + [(text, None)]
86
- return history
87
- else:
88
- gr.Warning("Please accept the Terms & Condition!")
89
- return None
90
 
91
 
92
- def bot(history, agree, system_prompt=""):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- if agree==True:
95
- history = [] if history is None else history
96
-
97
- if system_prompt == "":
98
- system_prompt = system_message
99
-
100
- history[-1][1] = ""
101
- for character in text_client.submit(
102
- history,
103
- system_prompt,
104
- temperature,
105
- 4096,
106
- temperature,
107
- repetition_penalty,
108
- api_name="/chat"
109
- ):
110
- history[-1][1] = character
111
- yield history
112
- else:
113
- gr.Warning("Please accept the Terms & Condition!")
114
- return None
115
 
116
- def generate_speech(history, agree):
117
- if agree==True:
118
- text_to_generate = history[-1][1]
119
- text_to_generate = text_to_generate.replace("\n", " ").strip()
120
- text_to_generate = nltk.sent_tokenize(text_to_generate)
121
-
122
- filename = f"{uuid.uuid4()}.wav"
123
- sampling_rate = tts.synthesizer.tts_config.audio["sample_rate"]
124
- silence = [0] * int(0.25 * sampling_rate)
125
 
 
 
 
 
 
 
 
 
126
 
127
- for sentence in text_to_generate:
128
- # generate speech by cloning a voice using default settings
129
- wav = tts.tts(text=sentence,
130
- speaker_wav="examples/female.wav",
131
- decoder_iterations=20,
132
- speed=1.2,
133
- language="en")
134
-
135
- yield (sampling_rate, np.array(wav)) #np.array(wav + silence))
136
- else:
137
- gr.Warning("Please accept the Terms & Condition!")
138
- return None
139
 
140
 
141
  with gr.Blocks(title=title) as demo:
142
  gr.Markdown(DESCRIPTION)
143
 
144
- agree = gr.Checkbox(
145
- label="Agree",
146
- value=False,
147
- info="I agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml",
148
- )
149
 
150
  chatbot = gr.Chatbot(
151
  [],
@@ -169,19 +140,27 @@ with gr.Blocks(title=title) as demo:
169
  clear_btn = gr.ClearButton([chatbot, audio])
170
 
171
 
172
- txt_msg = txt.submit(add_text, [chatbot, txt, agree], [chatbot, txt], queue=False).then(
173
- bot, [chatbot, agree], chatbot
174
- ).then(generate_speech, [chatbot, agree], audio)
175
 
176
  txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
177
 
178
- file_msg = btn.stop_recording(add_file, [chatbot, btn, agree], [chatbot], queue=False).then(
179
- bot, [chatbot, agree], chatbot
180
- ).then(generate_speech, [chatbot, agree], audio)
181
 
182
 
183
- gr.Markdown("""<div style='margin:20px auto;'>
184
- <p>By using this demo you agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml</p>
185
- </div>""")
 
 
 
 
 
 
 
 
186
  demo.queue()
187
  demo.launch(debug=True)
 
17
 
18
  title = "Speak with Llama2 70B"
19
 
20
+ DESCRIPTION = """# Speak with Llama2 70B"""
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  css = """.toast-wrap { display: none !important } """
22
 
23
 
24
+ os.environ["GRADIO_TEMP_DIR"] = "/home/yoach/spaces/tmp"
25
+
26
  system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
27
  temperature = 0.9
28
  top_p = 0.6
 
56
 
57
 
58
  def add_text(history, text, agree):
59
+ history = [] if history is None else history
60
+ history = history + [(text, None)]
61
+ return history, gr.update(value="", interactive=False)
62
+
 
 
 
63
 
64
  def add_file(history, file, agree):
65
+ history = [] if history is None else history
66
+ text = transcribe(
67
+ file
68
+ )
69
+
70
+ history = history + [(text, None)]
71
+ return history
 
 
 
 
72
 
73
 
74
+
75
+ def bot(history, agree, system_prompt=""):
76
+ history = [] if history is None else history
77
+
78
+ if system_prompt == "":
79
+ system_prompt = system_message
80
+
81
+ history[-1][1] = ""
82
+ for character in text_client.submit(
83
+ history,
84
+ system_prompt,
85
+ temperature,
86
+ 4096,
87
+ temperature,
88
+ repetition_penalty,
89
+ api_name="/chat"
90
+ ):
91
+ history[-1][1] = character
92
+ yield history
93
+
94
 
95
+ def generate_speech(history):
96
+ text_to_generate = history[-1][1]
97
+ text_to_generate = text_to_generate.replace("\n", " ").strip()
98
+ text_to_generate = nltk.sent_tokenize(text_to_generate)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
+ filename = f"{uuid.uuid4()}.wav"
101
+ sampling_rate = tts.synthesizer.tts_config.audio["sample_rate"]
102
+ silence = [0] * int(0.25 * sampling_rate)
 
 
 
 
 
 
103
 
104
+
105
+ for sentence in text_to_generate:
106
+ # generate speech by cloning a voice using default settings
107
+ wav = tts.tts(text=sentence,
108
+ speaker_wav="examples/female.wav",
109
+ decoder_iterations=20,
110
+ speed=1.2,
111
+ language="en")
112
 
113
+ yield (sampling_rate, np.array(wav)) #np.array(wav + silence))
114
+
 
 
 
 
 
 
 
 
 
 
115
 
116
 
117
  with gr.Blocks(title=title) as demo:
118
  gr.Markdown(DESCRIPTION)
119
 
 
 
 
 
 
120
 
121
  chatbot = gr.Chatbot(
122
  [],
 
140
  clear_btn = gr.ClearButton([chatbot, audio])
141
 
142
 
143
+ txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
144
+ bot, chatbot, chatbot
145
+ ).then(generate_speech, chatbot, audio)
146
 
147
  txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
148
 
149
+ file_msg = btn.stop_recording(add_file, [chatbot, btn], [chatbot], queue=False).then(
150
+ bot, chatbot, chatbot
151
+ ).then(generate_speech, chatbot, audio)
152
 
153
 
154
+ gr.Markdown("""
155
+ This Space demonstrates how to speak to a chatbot, based solely on open-source models.
156
+ It relies on 3 models:
157
+ 1. [Whisper-large-v2](https://huggingface.co/spaces/sanchit-gandhi/whisper-large-v2) as an ASR model, to transcribe recorded audio to text. It is called through a [gradio client](https://www.gradio.app/docs/client).
158
+ 2. [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) as the chat model, the actual chat model. It is also called through a [gradio client](https://www.gradio.app/docs/client).
159
+ 3. [Coqui's XTTS](https://huggingface.co/spaces/coqui/xtts) as a TTS model, to generate the chatbot answers. This time, the model is hosted locally.
160
+
161
+ Note:
162
+ - As a derivate work of [Llama-2-70b-chat](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta,
163
+ this demo is governed by the original [license](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/USE_POLICY.md).
164
+ - By using this demo you agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml""")
165
  demo.queue()
166
  demo.launch(debug=True)