knowsuchagency commited on
Commit
ffbd52c
1 Parent(s): f6a8bf7

fetch audio in parallel

Browse files
Files changed (1) hide show
  1. main.py +16 -13
main.py CHANGED
@@ -1,16 +1,16 @@
 
1
  import io
2
  import os
3
- from typing import List, Literal
4
  from pathlib import Path
 
5
 
6
  import gradio as gr
7
  from loguru import logger
8
  from openai import OpenAI
9
  from promptic import llm
10
- from pydantic import BaseModel
11
  from pypdf import PdfReader
12
  from tenacity import retry, retry_if_exception_type
13
- from pydantic import ValidationError
14
 
15
 
16
  class DialogueItem(BaseModel):
@@ -95,16 +95,19 @@ def generate_audio(file: str, openai_api_key: str = None) -> bytes:
95
 
96
  characters = 0
97
 
98
- for line in llm_output.dialogue:
99
- transcript_line = f"{line.speaker}: {line.text}"
100
-
101
- logger.info(transcript_line)
102
-
103
- audio_chunk = get_mp3(line.text, line.voice, openai_api_key)
104
-
105
- audio += audio_chunk
106
- characters += len(line.text)
107
- transcript += transcript_line + "\n\n"
 
 
 
108
 
109
  logger.info(f"Generated {characters} characters of audio")
110
 
 
1
+ import concurrent.futures as cf
2
  import io
3
  import os
 
4
  from pathlib import Path
5
+ from typing import List, Literal
6
 
7
  import gradio as gr
8
  from loguru import logger
9
  from openai import OpenAI
10
  from promptic import llm
11
+ from pydantic import BaseModel, ValidationError
12
  from pypdf import PdfReader
13
  from tenacity import retry, retry_if_exception_type
 
14
 
15
 
16
  class DialogueItem(BaseModel):
 
95
 
96
  characters = 0
97
 
98
+ with cf.ThreadPoolExecutor() as executor:
99
+ futures = []
100
+ for line in llm_output.dialogue:
101
+ transcript_line = f"{line.speaker}: {line.text}"
102
+ logger.info(transcript_line)
103
+ future = executor.submit(get_mp3, line.text, line.voice, openai_api_key)
104
+ futures.append((future, transcript_line))
105
+ characters += len(line.text)
106
+
107
+ for future, transcript_line in futures:
108
+ audio_chunk = future.result()
109
+ audio += audio_chunk
110
+ transcript += transcript_line + "\n\n"
111
 
112
  logger.info(f"Generated {characters} characters of audio")
113