-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
65 lines (55 loc) · 2.11 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
# src/babelscribe/utils.py
import os
import yaml
from datetime import datetime
from typing import Dict, Any, Union, Tuple, Optional
def load_config(config_path: str = "config.yaml") -> Dict[str, Any]:
"""
Load the configuration from the YAML file.
Parameters:
-----------
config_path : str, optional
Path to the configuration file (default is "config.yaml")
Returns:
--------
Dict[str, Any]
A dictionary containing the configuration settings
Raises:
-------
FileNotFoundError
If the configuration file is not found
yaml.YAMLError
If there's an error parsing the YAML file
"""
try:
with open(config_path, 'r') as config_file:
return yaml.safe_load(config_file)
except FileNotFoundError:
raise FileNotFoundError(f"Configuration file not found: {config_path}")
except yaml.YAMLError as e:
raise yaml.YAMLError(f"Error parsing the configuration file: {e}")
def ensure_folders_exist(config: Dict[str, Any]) -> None:
"""
Ensure that the required folders exist, creating them if necessary.
Parameters:
-----------
config : Dict[str, Any]
The configuration dictionary containing folder paths
"""
for folder in [config['input_folder'], config['output_folder'], config['transcription_folder']]:
os.makedirs(folder, exist_ok=True)
def save_results(file_name: str, transcription: str, summary: Optional[str], config: Dict[str, Any]) -> str:
base_name = os.path.splitext(file_name)[0]
output_file = f"{base_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
output_path = os.path.join(config['transcription_folder'], output_file)
with open(output_path, 'w') as f:
f.write(f"# Transcription and Summary of {file_name}\n\n")
f.write(f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
f.write("## Transcription\n\n")
f.write(transcription)
f.write("\n\n## Summary\n\n")
if summary:
f.write(summary)
else:
f.write("Summary generation failed.")
return output_path