knowsuchagency commited on
Commit
5c2ba64
1 Parent(s): a9922ff

text works

Browse files
Files changed (2) hide show
  1. main.py +98 -37
  2. requirements.txt +5 -2
main.py CHANGED
@@ -1,44 +1,105 @@
1
- from elevenlabs.client import ElevenLabs
 
 
2
  import gradio as gr
3
- import os
4
- from elevenlabs import play
 
 
5
 
6
- client = ElevenLabs(
7
- # api_key="YOUR_API_KEY", # Defaults to ELEVEN_API_KEY
8
- )
9
 
10
- female_voice = "nDJIICjR9zfJExIFeSCN"
11
- male_voice = "1m3E2x7boso3AU9J3woJ"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- def talk():
14
 
15
- # Define the dialogue text and assign different voices
16
- dialogue = [
17
- {"text": "Hello! How are you today?", "voice": female_voice},
18
- {"text": "I'm doing well, thank you! How about you?", "voice": male_voice},
19
- {"text": "I'm great, thanks for asking!", "voice": female_voice},
20
- ]
 
 
 
 
 
 
 
 
 
21
 
22
  # Generate and play the dialogue
23
- for line in dialogue:
24
- audio = client.generate(
25
- text=line["text"],
26
- voice=line["voice"],
27
- model="eleven_monolingual_v1",
28
- )
29
- # play(audio)
30
- yield audio
31
-
32
- def speak():
33
- clips = [b"".join(audio) for audio in talk()]
34
- return b"".join(clips)
35
-
36
- def get_interface():
37
- with gr.Blocks() as blocks:
38
- gr.Audio(value=speak)
39
- return blocks
40
-
41
-
42
- if __name__ == '__main__':
43
- demo = get_interface()
44
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ from typing import List, Literal
3
+
4
  import gradio as gr
5
+ from loguru import logger
6
+ from openai import OpenAI
7
+ from promptic import llm
8
+ from pydantic import BaseModel
9
 
 
 
 
10
 
11
+ def get_mp3(text: str, voice: str) -> bytes:
12
+ client = OpenAI()
13
+
14
+ with client.audio.speech.with_streaming_response.create(
15
+ model="tts-1",
16
+ voice=voice,
17
+ input=text,
18
+ ) as response:
19
+ with io.BytesIO() as file:
20
+ for chunk in response.iter_bytes():
21
+ file.write(chunk)
22
+ return file.getvalue()
23
+
24
+
25
+ class DialogueItem(BaseModel):
26
+ text: str
27
+ voice: Literal["alloy", "onyx", "fable"]
28
+
29
+
30
+ class Dialogue(BaseModel):
31
+ scratchpad: str
32
+ dialogue: List[DialogueItem]
33
+
34
+
35
+ @llm(model="gemini/gemini-1.5-pro-latest")
36
+ def generate_dialogue(text: str) -> Dialogue:
37
+ """
38
+ Your task is to take the input text provided and turn it into an engaging, informative podcast dialogue. The input text may be messy or unstructured, as it could come from a variety of sources like PDFs or web pages.
39
+
40
+ Here is the input text you will be working with:
41
+
42
+ ```
43
+ {text}
44
+ ```
45
+
46
+ First, carefully read through the input text and identify the main topics, key points, and any interesting facts or anecdotes. Think about how you could present this information in a fun, engaging way that would be suitable for an audio podcast.
47
+
48
+ Brainstorm creative ways to discuss the main topics and key points you identified in the input text. Consider using analogies, storytelling techniques, or hypothetical scenarios to make the content more relatable and engaging for listeners.
49
+
50
+ Keep in mind that your podcast should be accessible to a general audience, so avoid using too much jargon or assuming prior knowledge of the topic. If necessary, think of ways to briefly explain any complex concepts in simple terms.
51
 
52
+ Use your imagination to fill in any gaps in the input text or to come up with thought-provoking questions that could be explored in the podcast. The goal is to create an informative and entertaining dialogue, so feel free to be creative in your approach.
53
 
54
+ Write your brainstorming ideas and a rough outline for the podcast dialogue in a scratchpad.
55
+
56
+ Now that you have brainstormed ideas and created a rough outline, it's time to write the actual podcast dialogue. Aim for a natural, conversational flow between the host and any guest speakers. Incorporate the best ideas from your brainstorming session and make sure to explain any complex topics in an easy-to-understand way.
57
+
58
+ Write your engaging, informative podcast dialogue based on the key points and creative ideas you came up with during the brainstorming session. Use a conversational tone and include any necessary context or explanations to make the content accessible to a general audience.
59
+ """
60
+
61
+
62
+ def generate_audio(text: str) -> bytes:
63
+
64
+ llm_output = generate_dialogue(text)
65
+ logger.info(llm_output)
66
+
67
+ result = b""
68
+ characters = 0
69
 
70
  # Generate and play the dialogue
71
+ for line in llm_output.dialogue:
72
+ logger.info(line.text)
73
+ logger.info(line.voice)
74
+
75
+ audio = get_mp3(line.text, line.voice)
76
+ result += audio
77
+ characters += len(line.text)
78
+
79
+ logger.info(f"Generated {characters} characters of audio")
80
+
81
+ return result
82
+
83
+
84
+ demo = gr.Interface(
85
+ fn=generate_audio,
86
+ inputs=[
87
+ gr.Textbox(
88
+ label="Input Text",
89
+ placeholder="Enter text here",
90
+ ),
91
+ # gr.Textbox(
92
+ # label="Male Voice",
93
+ # value="1m3E2x7boso3AU9J3woJ",
94
+ # ),
95
+ # gr.Textbox(
96
+ # label="Female Voice",
97
+ # value="uCGnCVg8g9Lwl9wocoHE",
98
+ # ),
99
+ ],
100
+ outputs=[
101
+ gr.Audio(format="mp3"),
102
+ ],
103
+ )
104
+
105
+ demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,5 @@
1
- gradio
2
- elevenlabs
 
 
 
 
1
+ gradio~=4.36
2
+ promptic==0.7.3
3
+ pydantic~=2.7
4
+ google-generativeai~=0.6
5
+ loguru~=0.7