-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShadowingAudioGen.py
184 lines (149 loc) · 5.94 KB
/
ShadowingAudioGen.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# import os
# import re
# import json
# from pathlib import Path
# from openai import OpenAI
# from dotenv import load_dotenv
# from tqdm import tqdm
# # Load OpenAI API key
# load_dotenv()
# client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
# def load_json_file(filename):
# try:
# with open(filename, 'r', encoding='utf-8') as file:
# return json.load(file)
# except FileNotFoundError:
# print(f"File {filename} not found.")
# return None
# except json.JSONDecodeError:
# print(f"Error decoding JSON from {filename}.")
# return None
# def extract_modified_response(feedback):
# try:
# # Extract the modified response using regex
# match = re.search(r"\*\*Revised Version:\*\*\s*(.*)", feedback, re.DOTALL)
# if match:
# return match.group(1).strip()
# except Exception as e:
# print(f"Error extracting modified response: {e}")
# return None
# def generate_audio_for_response(text, voice, task_number, student_name):
# try:
# speech_file_path = Path(f"task{task_number}_modified_audios") / f"task{task_number}_{student_name}_shadowing.wav"
# speech_file_path.parent.mkdir(parents=True, exist_ok=True)
# response = client.audio.speech.create(
# model="tts-1",
# voice=voice,
# input=text
# )
# response.stream_to_file(speech_file_path)
# print(f"Generated audio for {student_name} at {speech_file_path}")
# except Exception as e:
# print(f"Error generating audio for {student_name}: {e}")
# def main():
# while True:
# print("Enter task number to process (1-4) or 'q' to quit:")
# task_number = input().strip()
# if task_number not in ['1', '2', '3', '4']:
# print("Exiting the program.")
# break
# task_file = f"task{task_number}_responses.json"
# gender_file = "student_gender_map.json"
# # Load JSON data
# task_data = load_json_file(task_file)
# gender_data = load_json_file(gender_file)
# if not task_data or not gender_data:
# continue
# for student_name, feedback_data in tqdm(task_data.items(), desc=f"Processing Task {task_number}"):
# modified_response = extract_modified_response(feedback_data["feedback"])
# if not modified_response:
# print(f"No modified response found for {student_name}")
# continue
# # Get the student's gender and select the appropriate voice
# gender = gender_data.get(student_name, "unknown").lower()
# if gender == "male":
# voice = "alloy"
# elif gender == "female":
# voice = "nova"
# else:
# print(f"Gender unknown for {student_name}, skipping...")
# continue
# # Generate the audio
# generate_audio_for_response(modified_response, voice, task_number, student_name)
# if __name__ == "__main__":
# main()
import os
import re
import json
from pathlib import Path
from openai import OpenAI
from dotenv import load_dotenv
from tqdm import tqdm
# Load OpenAI API key
load_dotenv()
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def load_json_file(filename):
try:
with open(filename, 'r', encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
print(f"File {filename} not found.")
return None
except json.JSONDecodeError:
print(f"Error decoding JSON from {filename}.")
return None
def extract_modified_response(feedback):
try:
# Extract the modified response using regex
match = re.search(r"\*\*Revised Version:\*\*\s*(.*)", feedback, re.DOTALL)
if match:
return match.group(1).strip()
except Exception as e:
print(f"Error extracting modified response: {e}")
return None
def generate_audio_for_response(text, voice, task_number, student_name):
try:
speech_file_path = Path(f"task{task_number}_modified_audios") / f"task{task_number}_{student_name}_shadowing.wav"
speech_file_path.parent.mkdir(parents=True, exist_ok=True)
response = client.audio.speech.create(
model="tts-1",
voice=voice,
input=text
)
response.stream_to_file(speech_file_path)
print(f"Generated audio for {student_name} at {speech_file_path}")
except Exception as e:
print(f"Error generating audio for {student_name}: {e}")
def process_responses(task_number):
task_file = f"task{task_number}_responses.json"
gender_file = "student_gender_map.json"
# Load JSON data
task_data = load_json_file(task_file)
gender_data = load_json_file(gender_file)
if not task_data or not gender_data:
return
for student_name, feedback_data in tqdm(task_data.items(), desc=f"Processing Task {task_number}"):
modified_response = extract_modified_response(feedback_data["feedback"])
if not modified_response:
print(f"No modified response found for {student_name}")
continue
# Get the student's gender and select the appropriate voice
gender = gender_data.get(student_name, "unknown").lower()
if gender == "male":
voice = "alloy"
elif gender == "female":
voice = "nova"
else:
print(f"Gender unknown for {student_name}, skipping...")
continue
# Generate the audio
generate_audio_for_response(modified_response, voice, task_number, student_name)
def main():
while True:
task_number = input("Select the task number to process responses (1, 2, 3, or 4), or type any other input to quit:").strip()
if task_number not in ['1', '2', '3', '4']:
print("Exiting the program.")
break
process_responses(task_number)
if __name__ == "__main__":
main()