John Langley commited on
Commit
718f6da
·
1 Parent(s): cd00a26

working of a streaming solution

Browse files
Files changed (2) hide show
  1. utils-original.py +114 -0
  2. utilsasync.py +183 -0
utils-original.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import nltk
3
+ import edge_tts
4
+ import tempfile
5
+ import asyncio
6
+
7
+ # Download the 'punkt' tokenizer for the NLTK library
8
+ nltk.download("punkt")
9
+
10
+ def format_prompt(message, history):
11
+ system_message = f"""
12
+ You are an empathetic, insightful, and supportive training coach who helps people deal with challenges and celebrate achievements.
13
+ You help people feel better by asking questions to reflect on and evoke feelings of positivity, gratitude, joy, and love.
14
+ You show radical candor and tough love.
15
+ Respond in a casual and friendly tone.
16
+ Sprinkle in filler words, contractions, idioms, and other casual speech that we use in conversation.
17
+ Emulate the user’s speaking style and be concise in your response.
18
+ """
19
+ prompt = (
20
+ "<s>[INST]" + system_message + "[/INST]"
21
+ )
22
+ for user_prompt, bot_response in history:
23
+ if user_prompt is not None:
24
+ prompt += f"[INST] {user_prompt} [/INST]"
25
+
26
+ prompt += f" {bot_response}</s> "
27
+
28
+ if message=="":
29
+ message="Hello"
30
+ prompt += f"[INST] {message} [/INST]"
31
+ return prompt
32
+
33
+
34
+ def generate_llm_output(
35
+ prompt,
36
+ history,
37
+ llm,
38
+ temperature=0.8,
39
+ max_tokens=256,
40
+ top_p=0.95,
41
+ stop_words=["<s>","[/INST]", "</s>"]
42
+ ):
43
+ temperature = float(temperature)
44
+ if temperature < 1e-2:
45
+ temperature = 1e-2
46
+ top_p = float(top_p)
47
+
48
+ generate_kwargs = dict(
49
+ temperature=temperature,
50
+ max_tokens=max_tokens,
51
+ top_p=top_p,
52
+ stop=stop_words
53
+ )
54
+ formatted_prompt = format_prompt(prompt, history)
55
+ try:
56
+ print("LLM Input:", formatted_prompt)
57
+ # Local GGUF
58
+ output = ""
59
+ stream = llm(
60
+ formatted_prompt,
61
+ **generate_kwargs,
62
+ stream=True,
63
+ )
64
+ for r in stream:
65
+ print(r["choices"][0]["text"])
66
+ character = r["choices"][0]["text"]
67
+ if character in stop_words:
68
+ # end of context
69
+ return
70
+
71
+ output += r["choices"][0]["text"]
72
+
73
+
74
+ except Exception as e:
75
+ print("Unhandled Exception: ", str(e))
76
+ gr.Warning("Unfortunately Mistral is unable to process")
77
+ output = "I do not know what happened but I could not understand you ."
78
+ return output
79
+
80
+
81
+ # tts interface function
82
+ def tts_interface(text, voice):
83
+ audio = asyncio.run(text_to_speech(text, voice))
84
+ return audio
85
+
86
+
87
+ # Text-to-speech function
88
+ async def text_to_speech(text, voice):
89
+ rate = 10
90
+ pitch = 10
91
+ rate_str = f"{rate:+d}%"
92
+ pitch_str = f"{pitch:+d}Hz"
93
+
94
+ voice_short_name = voice.split(" - ")[0]
95
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
96
+
97
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
98
+ tmp_path = tmp_file.name
99
+ await communicate.save(tmp_path)
100
+ return tmp_path
101
+
102
+
103
+
104
+ def get_sentence(history, llm):
105
+ history = [["", None]] if history is None else history
106
+ history[-1][1] = ""
107
+
108
+ text_to_generate = ""
109
+ text_to_generate = generate_llm_output(history[-1][0], history[:-1], llm)
110
+
111
+ history.append([None, text_to_generate])
112
+ return (history, text_to_generate)
113
+
114
+
utilsasync.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import nltk
3
+ import edge_tts
4
+ import tempfile
5
+ import asyncio
6
+
7
+ # Download the 'punkt' tokenizer for the NLTK library
8
+ nltk.download("punkt")
9
+
10
+ def format_prompt(message, history):
11
+ system_message = f"""
12
+ You are an empathetic, insightful, and supportive training coach who helps people deal with challenges and celebrate achievements.
13
+ You help people feel better by asking questions to reflect on and evoke feelings of positivity, gratitude, joy, and love.
14
+ You show radical candor and tough love.
15
+ Respond in a casual and friendly tone.
16
+ Sprinkle in filler words, contractions, idioms, and other casual speech that we use in conversation.
17
+ Emulate the user’s speaking style and be concise in your response.
18
+ """
19
+ prompt = (
20
+ "<s>[INST]" + system_message + "[/INST]"
21
+ )
22
+ for user_prompt, bot_response in history:
23
+ if user_prompt is not None:
24
+ prompt += f"[INST] {user_prompt} [/INST]"
25
+
26
+ prompt += f" {bot_response}</s> "
27
+
28
+ if message=="":
29
+ message="Hello"
30
+ prompt += f"[INST] {message} [/INST]"
31
+ return prompt
32
+
33
+
34
+ def generate_llm_output(
35
+ prompt,
36
+ history,
37
+ llm,
38
+ temperature=0.8,
39
+ max_tokens=256,
40
+ top_p=0.95,
41
+ stop_words=["<s>","[/INST]", "</s>"]
42
+ ):
43
+ temperature = float(temperature)
44
+ if temperature < 1e-2:
45
+ temperature = 1e-2
46
+ top_p = float(top_p)
47
+
48
+ generate_kwargs = dict(
49
+ temperature=temperature,
50
+ max_tokens=max_tokens,
51
+ top_p=top_p,
52
+ stop=stop_words
53
+ )
54
+ formatted_prompt = format_prompt(prompt, history)
55
+ try:
56
+ print("LLM Input:", formatted_prompt)
57
+ # Local GGUF
58
+ stream = llm(
59
+ formatted_prompt,
60
+ **generate_kwargs,
61
+ stream=True,
62
+ )
63
+ output = ""
64
+ for response in stream:
65
+ character= response["choices"][0]["text"]
66
+ print(character)
67
+
68
+ if character in stop_words:
69
+ # end of context
70
+ return
71
+
72
+ if emoji.is_emoji(character):
73
+ # Bad emoji not a meaning messes chat from next lines
74
+ return
75
+
76
+ output += response["choices"][0]["text"]
77
+ yield output
78
+
79
+ except Exception as e:
80
+ print("Unhandled Exception: ", str(e))
81
+ gr.Warning("Unfortunately Mistral is unable to process")
82
+ output = "I do not know what happened but I could not understand you ."
83
+ return output
84
+
85
+
86
+ # tts interface function
87
+ def tts_interface(text, voice):
88
+ audio = asyncio.run(text_to_speech(text, voice))
89
+ return audio
90
+
91
+
92
+ # Text-to-speech function
93
+ async def text_to_speech(text, voice):
94
+ rate = 10
95
+ pitch = 10
96
+ rate_str = f"{rate:+d}%"
97
+ pitch_str = f"{pitch:+d}Hz"
98
+
99
+ voice_short_name = voice.split(" - ")[0]
100
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
101
+
102
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
103
+ tmp_path = tmp_file.name
104
+ await communicate.save(tmp_path)
105
+ return tmp_path
106
+
107
+
108
+
109
+ def get_sentence(history, llm):
110
+ history = [["", None]] if history is None else history
111
+ history[-1][1] = ""
112
+ sentence_list = []
113
+ sentence_hash_list = []
114
+
115
+ text_to_generate = ""
116
+ stored_sentence = None
117
+ stored_sentence_hash = None
118
+
119
+ for character in generate_llm_output(history[-1][0], history[:-1], llm):
120
+ history[-1][1] = character.replace("<|assistant|>","")
121
+ # It is coming word by word
122
+ text_to_generate = nltk.sent_tokenize(history[-1][1].replace("\n", " ").replace("<|assistant|>"," ").replace("<|ass>","").replace("[/ASST]","").replace("[/ASSI]","").replace("[/ASS]","").replace("","").strip())
123
+ if len(text_to_generate) > 1:
124
+
125
+ dif = len(text_to_generate) - len(sentence_list)
126
+
127
+ if dif == 1 and len(sentence_list) != 0:
128
+ continue
129
+
130
+ if dif == 2 and len(sentence_list) != 0 and stored_sentence is not None:
131
+ continue
132
+
133
+ # All this complexity due to trying append first short sentence to next one for proper language auto-detect
134
+ if stored_sentence is not None and stored_sentence_hash is None and dif>1:
135
+ #means we consumed stored sentence and should look at next sentence to generate
136
+ sentence = text_to_generate[len(sentence_list)+1]
137
+ elif stored_sentence is not None and len(text_to_generate)>2 and stored_sentence_hash is not None:
138
+ print("Appending stored")
139
+ sentence = stored_sentence + text_to_generate[len(sentence_list)+1]
140
+ stored_sentence_hash = None
141
+ else:
142
+ sentence = text_to_generate[len(sentence_list)]
143
+
144
+ # too short sentence just append to next one if there is any
145
+ # this is for proper language detection
146
+ if len(sentence)<=15 and stored_sentence_hash is None and stored_sentence is None:
147
+ if sentence[-1] in [".","!","?"]:
148
+ if stored_sentence_hash != hash(sentence):
149
+ stored_sentence = sentence
150
+ stored_sentence_hash = hash(sentence)
151
+ print("Storing:",stored_sentence)
152
+ continue
153
+
154
+
155
+ sentence_hash = hash(sentence)
156
+ if stored_sentence_hash is not None and sentence_hash == stored_sentence_hash:
157
+ continue
158
+
159
+ if sentence_hash not in sentence_hash_list:
160
+ sentence_hash_list.append(sentence_hash)
161
+ sentence_list.append(sentence)
162
+ print("New Sentence: ", sentence)
163
+ yield (sentence, history)
164
+
165
+ # return that final sentence token
166
+ try:
167
+ last_sentence = nltk.sent_tokenize(history[-1][1].replace("\n", " ").replace("<|ass>","").replace("[/ASST]","").replace("[/ASSI]","").replace("[/ASS]","").replace("","").strip())[-1]
168
+ sentence_hash = hash(last_sentence)
169
+ if sentence_hash not in sentence_hash_list:
170
+ if stored_sentence is not None and stored_sentence_hash is not None:
171
+ last_sentence = stored_sentence + last_sentence
172
+ stored_sentence = stored_sentence_hash = None
173
+ print("Last Sentence with stored:",last_sentence)
174
+
175
+ sentence_hash_list.append(sentence_hash)
176
+ sentence_list.append(last_sentence)
177
+ print("Last Sentence: ", last_sentence)
178
+
179
+ yield (last_sentence, history)
180
+ except:
181
+ print("ERROR on last sentence history is :", history)
182
+
183
+