-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (49 loc) · 1.66 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import configparser
import os
import re
from pathlib import Path
from urllib.request import urlopen
import tiktoken
from dotenv import load_dotenv
from mutagen.mp3 import MP3
encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
def count_tokens(phrase):
n = encoding.encode(phrase)
return len(n)
def get_audio_duration(url):
audio_file = urlopen(url)
audio = MP3(audio_file)
duration = audio.info.length
return duration
def load_config(env_name="DEVELOPMENT"):
ENV = os.getenv("ENV_WHATIA", env_name)
# Read the configuration file
config = configparser.ConfigParser()
config_file_path = Path(__file__).resolve().parent / "config.ini"
config.read(config_file_path)
env_path = Path(__file__).resolve().parent / config[ENV]["ENV_FILE_PATH"]
load_dotenv(dotenv_path=env_path)
return config
def split_long_string(text, max_len=1599):
"""
Split a long string into a list of strings of maximum length `max_len`.
Args:
text (str): The input text to be split.
max_len (int, optional): The maximum length of each chunk. Defaults to 1200.
Returns:
list[str]: A list of strings, each with a length not exceeding `max_len`.
"""
if len(text) <= max_len:
return [text]
sentences = re.split("(?<=[.!?]) +", text)
result = []
current_chunk = ""
for sentence in sentences:
if len(current_chunk) + len(sentence) + 1 <= max_len:
current_chunk += " " + sentence
else:
result.append(current_chunk.strip())
current_chunk = sentence
if current_chunk:
result.append(current_chunk.strip())
return result