Update utils.py
Browse files
utils.py
CHANGED
|
@@ -6,15 +6,15 @@ import tiktoken
|
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
import tempfile
|
| 9 |
-
import
|
| 10 |
from bs4 import BeautifulSoup
|
|
|
|
| 11 |
|
| 12 |
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 13 |
tokenizer = tiktoken.get_encoding("cl100k_base")
|
| 14 |
-
VOICERSS_API_KEY = os.environ["VOICERSS_API_KEY"]
|
| 15 |
|
| 16 |
class DialogueItem(BaseModel):
|
| 17 |
-
speaker: Literal["
|
| 18 |
text: str
|
| 19 |
|
| 20 |
class Dialogue(BaseModel):
|
|
@@ -57,9 +57,14 @@ def generate_script(system_prompt: str, input_text: str, tone: str, target_lengt
|
|
| 57 |
Generate a complete, well-structured podcast script that:
|
| 58 |
1. Starts with a proper introduction
|
| 59 |
2. Covers the main points from the input text
|
| 60 |
-
3. Has a natural flow of conversation between
|
| 61 |
4. Concludes with a summary and sign-off
|
| 62 |
5. Fits within the {word_limit} word limit for the target length of {target_length}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
Ensure the script is not abruptly cut off and forms a complete conversation.
|
| 65 |
"""
|
|
@@ -95,14 +100,8 @@ def generate_script(system_prompt: str, input_text: str, tone: str, target_lengt
|
|
| 95 |
return dialogue
|
| 96 |
|
| 97 |
def generate_audio(text: str, speaker: str) -> str:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
response = requests.get(url)
|
| 102 |
-
if response.status_code != 200:
|
| 103 |
-
raise Exception(f"Error generating audio: {response.text}")
|
| 104 |
-
|
| 105 |
-
# Save the raw audio data
|
| 106 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
| 107 |
-
|
| 108 |
return temp_audio.name
|
|
|
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
import tempfile
|
| 9 |
+
from gtts import gTTS
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
+
import requests
|
| 12 |
|
| 13 |
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 14 |
tokenizer = tiktoken.get_encoding("cl100k_base")
|
|
|
|
| 15 |
|
| 16 |
class DialogueItem(BaseModel):
|
| 17 |
+
speaker: Literal["Sarah", "Maria"]
|
| 18 |
text: str
|
| 19 |
|
| 20 |
class Dialogue(BaseModel):
|
|
|
|
| 57 |
Generate a complete, well-structured podcast script that:
|
| 58 |
1. Starts with a proper introduction
|
| 59 |
2. Covers the main points from the input text
|
| 60 |
+
3. Has a natural flow of conversation between Sarah (American accent) and Maria (British accent)
|
| 61 |
4. Concludes with a summary and sign-off
|
| 62 |
5. Fits within the {word_limit} word limit for the target length of {target_length}
|
| 63 |
+
6. Strongly emphasizes the {tone} tone throughout the conversation
|
| 64 |
+
|
| 65 |
+
For a humorous tone, include jokes, puns, and playful banter.
|
| 66 |
+
For a casual tone, use colloquial language and make it sound like a conversation between college students.
|
| 67 |
+
For a formal tone, maintain a professional podcast style with well-structured arguments and formal language.
|
| 68 |
|
| 69 |
Ensure the script is not abruptly cut off and forms a complete conversation.
|
| 70 |
"""
|
|
|
|
| 100 |
return dialogue
|
| 101 |
|
| 102 |
def generate_audio(text: str, speaker: str) -> str:
|
| 103 |
+
tld = 'com' if speaker == "Sarah" else 'co.uk'
|
| 104 |
+
tts = gTTS(text=text, lang='en', tld=tld)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
| 106 |
+
tts.save(temp_audio.name)
|
| 107 |
return temp_audio.name
|