Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,18 +1,65 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from groq import Groq
|
| 2 |
+
from pydantic import BaseModel, ValidationError
|
| 3 |
+
from typing import List, Literal
|
| 4 |
+
import os
|
| 5 |
+
import tiktoken
|
| 6 |
+
import json
|
| 7 |
+
import re
|
| 8 |
+
from gtts import gTTS
|
| 9 |
+
import tempfile
|
| 10 |
+
|
| 11 |
+
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 12 |
+
tokenizer = tiktoken.get_encoding("cl100k_base")
|
| 13 |
+
|
| 14 |
+
class DialogueItem(BaseModel):
|
| 15 |
+
speaker: Literal["John", "Sarah"]
|
| 16 |
+
text: str
|
| 17 |
+
|
| 18 |
+
class Dialogue(BaseModel):
|
| 19 |
+
dialogue: List[DialogueItem]
|
| 20 |
+
|
| 21 |
+
def truncate_text(text, max_tokens=2048):
|
| 22 |
+
tokens = tokenizer.encode(text)
|
| 23 |
+
if len(tokens) > max_tokens:
|
| 24 |
+
return tokenizer.decode(tokens[:max_tokens])
|
| 25 |
+
return text
|
| 26 |
+
|
| 27 |
+
def generate_script(system_prompt: str, input_text: str, tone: str):
|
| 28 |
+
input_text = truncate_text(input_text)
|
| 29 |
+
prompt = f"{system_prompt}\nTONE: {tone}\nINPUT TEXT: {input_text}"
|
| 30 |
+
|
| 31 |
+
response = groq_client.chat.completions.create(
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": prompt},
|
| 34 |
+
],
|
| 35 |
+
model="llama-3.1-70b-versatile",
|
| 36 |
+
max_tokens=2048,
|
| 37 |
+
temperature=0.7
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
content = response.choices[0].message.content
|
| 41 |
+
content = re.sub(r'```json\s*|\s*```', '', content)
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
json_data = json.loads(content)
|
| 45 |
+
dialogue = Dialogue.model_validate(json_data)
|
| 46 |
+
except json.JSONDecodeError as json_error:
|
| 47 |
+
match = re.search(r'\{.*\}', content, re.DOTALL)
|
| 48 |
+
if match:
|
| 49 |
+
try:
|
| 50 |
+
json_data = json.loads(match.group())
|
| 51 |
+
dialogue = Dialogue.model_validate(json_data)
|
| 52 |
+
except (json.JSONDecodeError, ValidationError) as e:
|
| 53 |
+
raise ValueError(f"Failed to parse dialogue JSON: {e}\nContent: {content}")
|
| 54 |
+
else:
|
| 55 |
+
raise ValueError(f"Failed to find valid JSON in the response: {content}")
|
| 56 |
+
except ValidationError as e:
|
| 57 |
+
raise ValueError(f"Failed to validate dialogue structure: {e}\nContent: {content}")
|
| 58 |
+
|
| 59 |
+
return dialogue
|
| 60 |
+
|
| 61 |
+
def generate_audio(text: str, speaker: str) -> str:
|
| 62 |
+
tts = gTTS(text=text, lang='en', tld='com' if speaker == "John" else 'co.uk')
|
| 63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
| 64 |
+
tts.save(temp_audio.name)
|
| 65 |
+
return temp_audio.name
|