siddhartharya commited on
Commit
1df1eb4
1 Parent(s): 40926b4

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +65 -18
utils.py CHANGED
@@ -1,18 +1,65 @@
1
- runtime error
2
- Exit code: 1. Reason: Traceback (most recent call last):
3
- File "/home/user/app/app.py", line 2, in <module>
4
- from utils import generate_script, generate_audio, truncate_text
5
- File "/home/user/app/utils.py", line 17, in <module>
6
- tts_male = pipeline("text-to-speech", model="microsoft/speecht5_tts", device="cpu")
7
- File "/usr/local/lib/python3.10/site-packages/transformers/pipelines/__init__.py", line 999, in pipeline
8
- tokenizer = AutoTokenizer.from_pretrained(
9
- File "/usr/local/lib/python3.10/site-packages/transformers/models/auto/tokenization_auto.py", line 907, in from_pretrained
10
- return tokenizer_class.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
11
- File "/usr/local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1637, in __getattribute__
12
- requires_backends(cls, cls._backends)
13
- File "/usr/local/lib/python3.10/site-packages/transformers/utils/import_utils.py", line 1625, in requires_backends
14
- raise ImportError("".join(failed))
15
- ImportError:
16
- SpeechT5Tokenizer requires the SentencePiece library but it was not found in your environment. Checkout the instructions on the
17
- installation page of its repo: https://github.com/google/sentencepiece#installation and follow the ones
18
- that match your environment. Please note that you may need to restart your runtime after installation.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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