amir22010 commited on
Commit
d85b3ab
·
verified ·
1 Parent(s): 826e641

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -40,6 +40,9 @@ llm = Llama.from_pretrained(
40
  verbose=False
41
  )
42
 
 
 
 
43
  #guardrail model
44
  guard_llm = "llama-3.1-8b-instant"
45
 
@@ -95,14 +98,15 @@ def greet(product,description):
95
  response = client.chat.completions.create(model=guard_llm, messages=messages, temperature=0)
96
  if response.choices[0].message.content != "not moderated":
97
  a_list = ["Sorry, I can't proceed for generating marketing email. Your content needs to be moderated first. Thank you!"]
98
- tts = TTS(os.path.join(os.getcwd(), tts_model_str))
99
- speakers = tts.get_speakers()
100
- if len(a_list[0]) > 1024:
101
- # truncate the text
102
- text_str = a_list[0][:1024]
103
- else:
104
- text_str = a_list[0]
105
- samples = tts.synthesize(text_str, speakers[-1])
 
106
  yield gr.Audio(value=(tts.get_sampling_rate(), samples))
107
  else:
108
  output = llm.create_chat_completion(
@@ -118,14 +122,15 @@ def greet(product,description):
118
  stream=True
119
  )
120
  partial_message = ""
121
- tts = TTS(os.path.join(os.getcwd(), tts_model_str))
122
- speakers = tts.get_speakers()
123
  for chunk in output:
124
  delta = chunk['choices'][0]['delta']
125
  if 'content' in delta:
126
- samples = tts.synthesize(delta.get('content', ''), speakers[-1])
 
 
 
127
  yield gr.Audio(value=(tts.get_sampling_rate(), samples))
128
 
129
-
130
- demo = gr.Interface(fn=greet, inputs=["text","text"], concurrency_limit=10)
131
  demo.launch()
 
40
  verbose=False
41
  )
42
 
43
+ # locker that disallow access to the tts object from more then one thread
44
+ locker = Lock()
45
+
46
  #guardrail model
47
  guard_llm = "llama-3.1-8b-instant"
48
 
 
98
  response = client.chat.completions.create(model=guard_llm, messages=messages, temperature=0)
99
  if response.choices[0].message.content != "not moderated":
100
  a_list = ["Sorry, I can't proceed for generating marketing email. Your content needs to be moderated first. Thank you!"]
101
+ with locker:
102
+ tts = TTS(os.path.join(os.getcwd(), tts_model_str))
103
+ speakers = tts.get_speakers()
104
+ if len(a_list[0]) > 1024:
105
+ # truncate the text
106
+ text_str = a_list[0][:1024]
107
+ else:
108
+ text_str = a_list[0]
109
+ samples = tts.synthesize(text_str, speakers[-1])
110
  yield gr.Audio(value=(tts.get_sampling_rate(), samples))
111
  else:
112
  output = llm.create_chat_completion(
 
122
  stream=True
123
  )
124
  partial_message = ""
 
 
125
  for chunk in output:
126
  delta = chunk['choices'][0]['delta']
127
  if 'content' in delta:
128
+ with locker:
129
+ tts = TTS(os.path.join(os.getcwd(), tts_model_str))
130
+ speakers = tts.get_speakers()
131
+ samples = tts.synthesize(delta.get('content', ''), speakers[-1])
132
  yield gr.Audio(value=(tts.get_sampling_rate(), samples))
133
 
134
+ audio = gr.Audio()
135
+ demo = gr.Interface(fn=greet, inputs=["text","text"], concurrency_limit=10, outputs=audio)
136
  demo.launch()