Richie-O3 commited on
Commit
15ca86d
1 Parent(s): 7ab2803

update backend functions

Browse files
Files changed (2) hide show
  1. backend_functions.py +167 -358
  2. gcp.py +1 -1
backend_functions.py CHANGED
@@ -13,6 +13,8 @@ from google.cloud import storage
13
  from elevenlabs.client import ElevenLabs, AsyncElevenLabs
14
  from elevenlabs import play, save, Voice, stream
15
  from pymongo.mongo_client import MongoClient
 
 
16
  from dotenv import load_dotenv
17
  load_dotenv()
18
 
@@ -67,175 +69,95 @@ def _add_question_vectorstore(question: str, response: str):
67
 
68
 
69
  def _update_elements(question, chatbot, output, history_messages, url_audio, url_video, df_table_times):
70
-
71
- if tf.test.is_gpu_available():
72
- chatbot.append([question, output])
73
- new_comp_audio = gr.Audio(value=str(url_audio), autoplay=False, label="Audio")
74
- new_comp_video = gr.Video(value=str(url_video), autoplay=True, height=400, label="Video")
75
-
76
- history_messages.append({'role': 'user', 'content': question})
77
- history_messages.append({'role': 'assistant', 'content': output})
78
- else:
79
- chatbot.append([question, output])
80
- new_comp_audio = gr.Audio(value=str(url_audio), autoplay=False, label="Audio")
81
- new_comp_video = gr.Video(value=str(url_video), autoplay=True, height=400, label="Video")
82
 
83
- history_messages.append({'role': 'user', 'content': question})
84
- history_messages.append({'role': 'assistant', 'content': output})
85
 
86
  return chatbot, new_comp_audio, new_comp_video, df_table_times
87
 
88
 
89
  def _query_pinecone(embedding):
90
-
91
- if tf.test.is_gpu_available():
92
- results = index.query(
93
- vector=embedding,
94
- top_k=10,
95
- include_metadata=True,
96
- )
97
-
98
- final_results = """"""
99
- for result in results['matches']:
100
- final_results += f"{result['metadata']['text']}\n"
101
- else:
102
- results = index.query(
103
- vector=embedding,
104
- top_k=10,
105
- include_metadata=True,
106
- )
107
 
108
- final_results = """"""
109
- for result in results['matches']:
110
- final_results += f"{result['metadata']['text']}\n"
111
 
112
  return final_results
113
 
114
 
115
  def _general_prompt(context):
116
-
117
- if tf.test.is_gpu_available():
118
- with open("prompt_general.txt", "r") as file:
119
- file_prompt = file.read().replace("\n", "")
120
-
121
- context_prompt = file_prompt.replace('CONTEXT', context)
122
- print(context_prompt)
123
- print("--------------------")
124
- else:
125
- with open("prompt_general.txt", "r") as file:
126
- file_prompt = file.read().replace("\n", "")
127
-
128
- context_prompt = file_prompt.replace('CONTEXT', context)
129
- print(context_prompt)
130
- print("--------------------")
131
 
132
  return context_prompt
133
 
134
 
135
  def _call_embedding(text: str):
136
-
137
- if tf.test.is_gpu_available():
138
- response = openai_client.embeddings.create(
139
- input=text,
140
- model='text-embedding-ada-002'
141
- )
142
- else:
143
- response = openai_client.embeddings.create(
144
- input=text,
145
- model='text-embedding-ada-002'
146
- )
147
 
148
  return response.data[0].embedding
149
 
150
 
151
  def _call_gpt(prompt: str, message: str):
152
-
153
- if tf.test.is_gpu_available():
154
- response = openai_client.chat.completions.create(
155
- model=MODEL_OPENAI,
156
- temperature=0.2,
157
- messages=[
158
- {'role': 'system', 'content': prompt},
159
- {'role': 'user', 'content': message}
160
- ]
161
- )
162
- else:
163
- response = openai_client.chat.completions.create(
164
- model=MODEL_OPENAI,
165
- temperature=0.2,
166
- messages=[
167
- {'role': 'system', 'content': prompt},
168
- {'role': 'user', 'content': message}
169
- ]
170
- )
171
 
172
  return response.choices[0].message.content
173
 
174
 
175
  def _call_gpt_standalone(prompt: str):
176
-
177
- if tf.test.is_gpu_available():
178
- response = openai_client.chat.completions.create(
179
- model=MODEL_OPENAI,
180
- temperature=0.2,
181
- messages=[
182
- {'role': 'system', 'content': prompt},
183
- ]
184
- )
185
- else:
186
- response = openai_client.chat.completions.create(
187
- model=MODEL_OPENAI,
188
- temperature=0.2,
189
- messages=[
190
- {'role': 'system', 'content': prompt},
191
- ]
192
- )
193
 
194
  return response.choices[0].message.content
195
 
196
 
197
  def _get_standalone_question(question, history_messages):
198
-
199
- if tf.test.is_gpu_available():
200
- with open("prompt_standalone_message.txt", "r") as file:
201
- file_prompt_standalone = file.read().replace("\n", "")
202
-
203
- history = ''
204
- for i, msg in enumerate(history_messages):
205
- try:
206
- if i == 0:
207
- continue # Omit the prompt
208
- if i % 2 == 0:
209
- history += f'user: {msg["content"]}\n'
210
- else:
211
- history += f'assistant: {msg["content"]}\n'
212
- except Exception as e:
213
- print(e)
214
-
215
- prompt_standalone = file_prompt_standalone.replace('HISTORY', history).replace('QUESTION', question)
216
- standalone_msg_q = _call_gpt_standalone(prompt_standalone)
217
- print(standalone_msg_q)
218
- print("------------------")
219
- else:
220
- with open("prompt_standalone_message.txt", "r") as file:
221
- file_prompt_standalone = file.read().replace("\n", "")
222
-
223
- history = ''
224
- for i, msg in enumerate(history_messages):
225
- try:
226
- if i == 0:
227
- continue # Omit the prompt
228
- if i % 2 == 0:
229
- history += f'user: {msg["content"]}\n'
230
- else:
231
- history += f'assistant: {msg["content"]}\n'
232
- except Exception as e:
233
- print(e)
234
-
235
- prompt_standalone = file_prompt_standalone.replace('HISTORY', history).replace('QUESTION', question)
236
- standalone_msg_q = _call_gpt_standalone(prompt_standalone)
237
- print(standalone_msg_q)
238
- print("------------------")
239
 
240
  return standalone_msg_q
241
 
@@ -246,243 +168,130 @@ def _create_clean_message(text: str):
246
 
247
 
248
  def _create_audio(clean_text: str):
 
 
 
 
249
 
250
- if tf.test.is_gpu_available():
251
- STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
252
-
253
- unique_id = str(uuid.uuid4())
254
-
255
- # Create audio file
256
- client_elevenlabs = ElevenLabs(api_key=API_KEY_ELEVENLABS)
257
- voice_custom = Voice(voice_id = "ZQe5CZNOzWyzPSCn5a3c")
258
-
259
- audio = client_elevenlabs.generate(
260
- text=clean_text,
261
- voice=voice_custom,
262
- model="eleven_multilingual_v2"
263
- )
264
-
265
- source_audio_file_name = f'./audios/file_audio_{unique_id}.wav'
266
-
267
- try:
268
- save(audio, source_audio_file_name)
269
- except Exception as e:
270
- print(e)
271
-
272
- # Save audio and get url of gcp
273
- destination_blob_name_audio = unique_id + '.wav'
274
-
275
- bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
276
- blob = bucket.blob(destination_blob_name_audio)
277
- try:
278
- blob.upload_from_filename(source_audio_file_name)
279
- except Exception as e:
280
- print(e)
281
-
282
- signed_url_audio = "None"
283
- try:
284
- url_expiration = timedelta(minutes=15)
285
- signed_url_audio = blob.generate_signed_url(expiration=url_expiration)
286
- except Exception as e:
287
- print(e)
288
- else:
289
- STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
290
-
291
- unique_id = str(uuid.uuid4())
292
-
293
- # Create audio file
294
- client_elevenlabs = ElevenLabs(api_key=API_KEY_ELEVENLABS)
295
- voice_custom = Voice(voice_id = "ZQe5CZNOzWyzPSCn5a3c")
296
 
297
- audio = client_elevenlabs.generate(
298
- text=clean_text,
299
- voice=voice_custom,
300
- model="eleven_multilingual_v2"
301
- )
302
 
303
- source_audio_file_name = f'./audios/file_audio_{unique_id}.wav'
 
 
 
 
304
 
305
- try:
306
- save(audio, source_audio_file_name)
307
- except Exception as e:
308
- print(e)
309
 
310
- # Save audio and get url of gcp
311
- destination_blob_name_audio = unique_id + '.wav'
312
-
313
- bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
314
- blob = bucket.blob(destination_blob_name_audio)
315
- try:
316
- blob.upload_from_filename(source_audio_file_name)
317
- except Exception as e:
318
- print(e)
319
 
320
- signed_url_audio = "None"
321
- try:
322
- url_expiration = timedelta(minutes=15)
323
- signed_url_audio = blob.generate_signed_url(expiration=url_expiration)
324
- except Exception as e:
325
- print(e)
 
 
 
 
 
 
 
 
 
 
326
 
327
  return signed_url_audio, unique_id
328
 
329
 
330
  def _create_video(link_audio: str, unique_id: str):
331
-
332
- if tf.test.is_gpu_available():
333
- STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
334
- bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
335
-
336
- # Create video talk with file audio created by elevenlabs api
337
- url_did = "https://api.d-id.com/talks"
338
-
339
- payload = {
340
- "script": {
341
- "type": "audio",
342
- "provider": {
343
- "type": "microsoft",
344
- "voice_id": "en-US-JennyNeural"
345
- },
346
- "ssml": "false",
347
- "audio_url": link_audio
348
- },
349
- "config": {
350
- "fluent": "false",
351
- "pad_audio": "0.0",
352
- "stitch": True
353
- },
354
- "source_url": IMG_XAVY
355
- }
356
- headers = {
357
- "accept": "application/json",
358
- "content-type": "application/json",
359
- "authorization": f"Basic {D_ID_KEY}"
360
- }
361
-
362
- request_create_talk = requests.post(url_did, json=payload, headers=headers)
363
- resp_create_talk = request_create_talk.json()
364
-
365
- talk_id = "None"
366
- try:
367
- talk_id = resp_create_talk['id']
368
- except Exception as e:
369
- print(e)
370
-
371
- # Get url of video file
372
- url_get_talk_id = f"https://api.d-id.com/talks/{talk_id}"
373
-
374
-
375
- while True:
376
- request_video_url = requests.get(url_get_talk_id, headers=headers)
377
- resp_video_url = request_video_url.json()
378
-
379
- if resp_video_url['status'] == 'done':
380
- break
381
- # Sleep until the video is ready
382
- time.sleep(0.5)
383
-
384
- result_url_video = resp_video_url['result_url']
385
-
386
- # Saves the video into a file to later upload it to the GCP
387
- source_video_file_name = f'./videos/video_final_{unique_id}.mp4'
388
- request_video = requests.get(result_url_video)
389
- if request_video.status_code == 200:
390
- with open(source_video_file_name, 'wb') as outfile:
391
- outfile.write(request_video.content)
392
-
393
- # Save video file to the GCP
394
- destination_blob_name_video = unique_id + '.mp4'
395
-
396
- # Configure bucket
397
- blob = bucket.blob(destination_blob_name_video)
398
- try:
399
- blob.upload_from_filename(source_video_file_name)
400
- except Exception as e:
401
- print(e)
402
-
403
- signed_url_video = "None"
404
- try:
405
- url_expiration_video = timedelta(minutes=15)
406
- signed_url_video = blob.generate_signed_url(expiration=url_expiration_video)
407
- except Exception as e:
408
- print(e)
409
- else:
410
- STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
411
- bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
412
-
413
- # Create video talk with file audio created by elevenlabs api
414
- url_did = "https://api.d-id.com/talks"
415
-
416
- payload = {
417
- "script": {
418
- "type": "audio",
419
- "provider": {
420
- "type": "microsoft",
421
- "voice_id": "en-US-JennyNeural"
422
- },
423
- "ssml": "false",
424
- "audio_url": link_audio
425
- },
426
- "config": {
427
- "fluent": "false",
428
- "pad_audio": "0.0",
429
- "stitch": True
430
  },
431
- "source_url": IMG_XAVY
432
- }
433
- headers = {
434
- "accept": "application/json",
435
- "content-type": "application/json",
436
- "authorization": f"Basic {D_ID_KEY}"
437
- }
438
-
439
- request_create_talk = requests.post(url_did, json=payload, headers=headers)
440
- resp_create_talk = request_create_talk.json()
441
-
442
- talk_id = "None"
443
- try:
444
- talk_id = resp_create_talk['id']
445
- except Exception as e:
446
- print(e)
447
-
448
- # Get url of video file
449
- url_get_talk_id = f"https://api.d-id.com/talks/{talk_id}"
450
-
451
-
452
- while True:
453
- request_video_url = requests.get(url_get_talk_id, headers=headers)
454
- resp_video_url = request_video_url.json()
455
-
456
- if resp_video_url['status'] == 'done':
457
- break
458
- # Sleep until the video is ready
459
- time.sleep(0.5)
460
-
461
- result_url_video = resp_video_url['result_url']
462
 
463
- # Saves the video into a file to later upload it to the GCP
464
- source_video_file_name = f'./videos/video_final_{unique_id}.mp4'
465
- request_video = requests.get(result_url_video)
466
- if request_video.status_code == 200:
467
- with open(source_video_file_name, 'wb') as outfile:
468
- outfile.write(request_video.content)
469
 
470
- # Save video file to the GCP
471
- destination_blob_name_video = unique_id + '.mp4'
 
 
 
472
 
473
- # Configure bucket
474
- blob = bucket.blob(destination_blob_name_video)
475
- try:
476
- blob.upload_from_filename(source_video_file_name)
477
- except Exception as e:
478
- print(e)
479
 
480
- signed_url_video = "None"
481
- try:
482
- url_expiration_video = timedelta(minutes=15)
483
- signed_url_video = blob.generate_signed_url(expiration=url_expiration_video)
484
- except Exception as e:
485
- print(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
 
487
  return signed_url_video
488
 
 
13
  from elevenlabs.client import ElevenLabs, AsyncElevenLabs
14
  from elevenlabs import play, save, Voice, stream
15
  from pymongo.mongo_client import MongoClient
16
+ from utils import create_folders
17
+ from gcp import download_credentials
18
  from dotenv import load_dotenv
19
  load_dotenv()
20
 
 
69
 
70
 
71
  def _update_elements(question, chatbot, output, history_messages, url_audio, url_video, df_table_times):
72
+ chatbot.append([question, output])
73
+ new_comp_audio = gr.Audio(value=str(url_audio), autoplay=False, label="Audio")
74
+ new_comp_video = gr.Video(value=str(url_video), autoplay=True, height=400, label="Video")
 
 
 
 
 
 
 
 
 
75
 
76
+ history_messages.append({'role': 'user', 'content': question})
77
+ history_messages.append({'role': 'assistant', 'content': output})
78
 
79
  return chatbot, new_comp_audio, new_comp_video, df_table_times
80
 
81
 
82
  def _query_pinecone(embedding):
83
+ results = index.query(
84
+ vector=embedding,
85
+ top_k=10,
86
+ include_metadata=True,
87
+ )
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ final_results = """"""
90
+ for result in results['matches']:
91
+ final_results += f"{result['metadata']['text']}\n"
92
 
93
  return final_results
94
 
95
 
96
  def _general_prompt(context):
97
+ with open("prompt_general.txt", "r") as file:
98
+ file_prompt = file.read().replace("\n", "")
99
+
100
+ context_prompt = file_prompt.replace('CONTEXT', context)
101
+ print(context_prompt)
102
+ print("--------------------")
 
 
 
 
 
 
 
 
 
103
 
104
  return context_prompt
105
 
106
 
107
  def _call_embedding(text: str):
108
+ response = openai_client.embeddings.create(
109
+ input=text,
110
+ model='text-embedding-ada-002'
111
+ )
 
 
 
 
 
 
 
112
 
113
  return response.data[0].embedding
114
 
115
 
116
  def _call_gpt(prompt: str, message: str):
117
+ response = openai_client.chat.completions.create(
118
+ model=MODEL_OPENAI,
119
+ temperature=0.2,
120
+ messages=[
121
+ {'role': 'system', 'content': prompt},
122
+ {'role': 'user', 'content': message}
123
+ ]
124
+ )
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  return response.choices[0].message.content
127
 
128
 
129
  def _call_gpt_standalone(prompt: str):
130
+ response = openai_client.chat.completions.create(
131
+ model=MODEL_OPENAI,
132
+ temperature=0.2,
133
+ messages=[
134
+ {'role': 'system', 'content': prompt},
135
+ ]
136
+ )
 
 
 
 
 
 
 
 
 
 
137
 
138
  return response.choices[0].message.content
139
 
140
 
141
  def _get_standalone_question(question, history_messages):
142
+ with open("prompt_standalone_message.txt", "r") as file:
143
+ file_prompt_standalone = file.read().replace("\n", "")
144
+
145
+ history = ''
146
+ for i, msg in enumerate(history_messages):
147
+ try:
148
+ if i == 0:
149
+ continue # Omit the prompt
150
+ if i % 2 == 0:
151
+ history += f'user: {msg["content"]}\n'
152
+ else:
153
+ history += f'assistant: {msg["content"]}\n'
154
+ except Exception as e:
155
+ print(e)
156
+
157
+ prompt_standalone = file_prompt_standalone.replace('HISTORY', history).replace('QUESTION', question)
158
+ standalone_msg_q = _call_gpt_standalone(prompt_standalone)
159
+ print(standalone_msg_q)
160
+ print("------------------")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  return standalone_msg_q
163
 
 
168
 
169
 
170
  def _create_audio(clean_text: str):
171
+ download_credentials()
172
+ create_folders()
173
+
174
+ STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
175
 
176
+ unique_id = str(uuid.uuid4())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
+ # Create audio file
179
+ client_elevenlabs = ElevenLabs(api_key=API_KEY_ELEVENLABS)
180
+ voice_custom = Voice(voice_id = "ZQe5CZNOzWyzPSCn5a3c")
 
 
181
 
182
+ audio = client_elevenlabs.generate(
183
+ text=clean_text,
184
+ voice=voice_custom,
185
+ model="eleven_multilingual_v2"
186
+ )
187
 
188
+ source_audio_file_name = f'./audios/file_audio_{unique_id}.wav'
 
 
 
189
 
190
+ try:
191
+ save(audio, source_audio_file_name)
192
+ except Exception as e:
193
+ print(e)
 
 
 
 
 
194
 
195
+ # Save audio and get url of gcp
196
+ destination_blob_name_audio = unique_id + '.wav'
197
+
198
+ bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
199
+ blob = bucket.blob(destination_blob_name_audio)
200
+ try:
201
+ blob.upload_from_filename(source_audio_file_name)
202
+ except Exception as e:
203
+ print(e)
204
+
205
+ signed_url_audio = "None"
206
+ try:
207
+ url_expiration = timedelta(minutes=15)
208
+ signed_url_audio = blob.generate_signed_url(expiration=url_expiration)
209
+ except Exception as e:
210
+ print(e)
211
 
212
  return signed_url_audio, unique_id
213
 
214
 
215
  def _create_video(link_audio: str, unique_id: str):
216
+ download_credentials()
217
+ create_folders()
218
+
219
+ STORAGE_CLIENT = storage.Client.from_service_account_json(CREDENTIALS_GCP)
220
+ bucket = STORAGE_CLIENT.bucket(NAME_BUCKET)
221
+
222
+ # Create video talk with file audio created by elevenlabs api
223
+ url_did = "https://api.d-id.com/talks"
224
+
225
+ payload = {
226
+ "script": {
227
+ "type": "audio",
228
+ "provider": {
229
+ "type": "microsoft",
230
+ "voice_id": "en-US-JennyNeural"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  },
232
+ "ssml": "false",
233
+ "audio_url": link_audio
234
+ },
235
+ "config": {
236
+ "fluent": "false",
237
+ "pad_audio": "0.0",
238
+ "stitch": True
239
+ },
240
+ "source_url": IMG_XAVY
241
+ }
242
+ headers = {
243
+ "accept": "application/json",
244
+ "content-type": "application/json",
245
+ "authorization": f"Basic {D_ID_KEY}"
246
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
+ request_create_talk = requests.post(url_did, json=payload, headers=headers)
249
+ resp_create_talk = request_create_talk.json()
 
 
 
 
250
 
251
+ talk_id = "None"
252
+ try:
253
+ talk_id = resp_create_talk['id']
254
+ except Exception as e:
255
+ print(e)
256
 
257
+ # Get url of video file
258
+ url_get_talk_id = f"https://api.d-id.com/talks/{talk_id}"
 
 
 
 
259
 
260
+
261
+ while True:
262
+ request_video_url = requests.get(url_get_talk_id, headers=headers)
263
+ resp_video_url = request_video_url.json()
264
+
265
+ if resp_video_url['status'] == 'done':
266
+ break
267
+ # Sleep until the video is ready
268
+ time.sleep(0.5)
269
+
270
+ result_url_video = resp_video_url['result_url']
271
+
272
+ # Saves the video into a file to later upload it to the GCP
273
+ source_video_file_name = f'./videos/video_final_{unique_id}.mp4'
274
+ request_video = requests.get(result_url_video)
275
+ if request_video.status_code == 200:
276
+ with open(source_video_file_name, 'wb') as outfile:
277
+ outfile.write(request_video.content)
278
+
279
+ # Save video file to the GCP
280
+ destination_blob_name_video = unique_id + '.mp4'
281
+
282
+ # Configure bucket
283
+ blob = bucket.blob(destination_blob_name_video)
284
+ try:
285
+ blob.upload_from_filename(source_video_file_name)
286
+ except Exception as e:
287
+ print(e)
288
+
289
+ signed_url_video = "None"
290
+ try:
291
+ url_expiration_video = timedelta(minutes=15)
292
+ signed_url_video = blob.generate_signed_url(expiration=url_expiration_video)
293
+ except Exception as e:
294
+ print(e)
295
 
296
  return signed_url_video
297
 
gcp.py CHANGED
@@ -15,7 +15,7 @@ def download_credentials() -> None:
15
  """
16
  Downloads the GCP credentials from Hugging Face Hub
17
  """
18
- assets_dir = './assets'
19
  credentials_file = os.path.join(assets_dir, "credentials.json")
20
  print(credentials_file)
21
 
 
15
  """
16
  Downloads the GCP credentials from Hugging Face Hub
17
  """
18
+ assets_dir = 'assets'
19
  credentials_file = os.path.join(assets_dir, "credentials.json")
20
  print(credentials_file)
21