fffiloni commited on
Commit
ac7edd1
β€’
1 Parent(s): 04a108f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -2
app.py CHANGED
@@ -8,6 +8,8 @@ from langchain.llms import OpenAI
8
  from gradio_client import Client
9
  eleven = Client("https://elevenlabs-tts.hf.space/")
10
 
 
 
11
  openai_api_key = os.environ.get("OPENAI_API_KEY")
12
 
13
  llm = OpenAI(temperature=0.9)
@@ -29,6 +31,21 @@ def split_text(text, max_length):
29
 
30
  return chunks
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def generate_story(text):
33
  """Generate a story using the langchain library and OpenAI's GPT-3 model."""
34
  prompt = PromptTemplate(
@@ -46,12 +63,24 @@ def generate_story(text):
46
  Cutting text in chunks
47
  β€”
48
  """)
 
49
  max_length = 250
50
  text_chunks = split_text(story_result, max_length)
51
  for chunk in text_chunks:
52
  print(chunk)
 
 
 
 
 
 
 
 
53
 
54
- return story_result
 
 
 
55
 
56
  def app(text):
57
  story = generate_story(text)
@@ -64,6 +93,6 @@ with gr.Blocks() as demo:
64
  audio = gr.Audio()
65
  story = gr.Textbox()
66
 
67
- submit_btn.click(fn=app, inputs=[text], outputs=[story])
68
 
69
  demo.launch()
 
8
  from gradio_client import Client
9
  eleven = Client("https://elevenlabs-tts.hf.space/")
10
 
11
+ import wave
12
+
13
  openai_api_key = os.environ.get("OPENAI_API_KEY")
14
 
15
  llm = OpenAI(temperature=0.9)
 
31
 
32
  return chunks
33
 
34
+ def join_wav_files(input_files, output_file):
35
+ # Open the first input file to get its parameters
36
+ with wave.open(input_files[0], 'rb') as first_file:
37
+ # Get the audio parameters from the first file
38
+ params = first_file.getparams()
39
+
40
+ # Create a new wave file for writing the joined audio
41
+ with wave.open(output_file, 'wb') as output:
42
+ output.setparams(params)
43
+
44
+ # Iterate over the input files and write their audio data to the output file
45
+ for input_file in input_files:
46
+ with wave.open(input_file, 'rb') as input:
47
+ output.writeframes(input.readframes(input.getnframes()))
48
+
49
  def generate_story(text):
50
  """Generate a story using the langchain library and OpenAI's GPT-3 model."""
51
  prompt = PromptTemplate(
 
63
  Cutting text in chunks
64
  β€”
65
  """)
66
+ input_waves = []
67
  max_length = 250
68
  text_chunks = split_text(story_result, max_length)
69
  for chunk in text_chunks:
70
  print(chunk)
71
+ result = eleven.predict(
72
+ chunk, # str representing input in 'Input Text (250 characters max)' Textbox component
73
+ "Bella", # str representing input in 'Voice' Dropdown component
74
+ "eleven_monolingual_v1", # str representing input in 'Model' Radio component
75
+ fn_index=0
76
+ )
77
+ print(result)
78
+ input_waves.append(result)
79
 
80
+ output_file = 'output.wav'
81
+
82
+ join_wav_files(input_waves, output_wav)
83
+ return story_result, 'output_wav'
84
 
85
  def app(text):
86
  story = generate_story(text)
 
93
  audio = gr.Audio()
94
  story = gr.Textbox()
95
 
96
+ submit_btn.click(fn=app, inputs=[text], outputs=[story, audio])
97
 
98
  demo.launch()