forked from TrungThanhTran/11LabHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
182 lines (160 loc) · 6.16 KB
/
app.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
import os
import json
import requests
import streamlit as st
from utils import write_audio_file, generate_speech_11lab
from recommendation import *
ELEVEN_LAB_URL = "https://api.elevenlabs.io/v1/text-to-speech/"
# importing required modules
def read_text_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
return content
def get_metadata(input):
# metadata:{genre, title, author, description/summary}
# user_data:{nationality, gender, age, voice favor in history, accent}
st.title("Book's metadata")
option = st.selectbox(
'Select the example:',
('<select>', 'book1', 'book2', 'book3'))
if option == 'book1':
input['genre'] = 'Adventure'
input['title'] = 'The Adventures of Tom Sawyer'
input['author']= 'Mark Twain'
input['description'] = 'A classic tale of a young boy named Tom Sawyer and his adventures along the Mississippi River.'
elif option == 'book2':
input['genre'] = 'Adventure'
input['title'] = 'The Adventures of Tom Sawyer'
input['author'] = 'Mark Twain'
input['description'] = 'A classic tale of a young boy named Tom Sawyer and his adventures along the Mississippi River.'
elif option == 'book3':
input['genre'] = 'Adventure'
input['title'] = 'The Adventures of Tom Sawyer'
input['author'] = 'Mark Twain'
input['description'] = 'A classic tale of a young boy named Tom Sawyer and his adventures along the Mississippi River.'
return input
def get_user(input):
# user_data:{nationality, gender, age, voice favor in history, accent}
st.title("User's metadata")
option = st.selectbox(
'Select the example:',
('<select>', 'user1', 'user2', 'user3'))
if option == 'user1':
input['nationality'] = 'Bristish'
input['gender'] = 'male'
input['age'] = '25'
input['voice favor in history'] = 'male'
input['accent'] = 'Bristish'
elif option == 'user2':
input['nationality'] = 'American'
input['gender'] = 'female'
input['age'] = '50'
input['voice favor in history'] = 'female'
input['accent']= 'Bristish'
elif option == 'user3':
input['nationality'] = 'Indian'
input['gender'] = 'female'
input['age'] = '15'
input['voice favor in history'] = 'female'
input['accent'] = 'American'
return input
def select_settings_by_rulebase(level_rule, data):
"""
This is a baseline version for voice-settings recommendation to generate audiobooks
"""
# Level1: Rulebase
if level_rule == 1:
suggetor = RuleBase()
rem_voice = suggetor.pairse_rule(data)
# Level2: Semantic Similarity
elif level_rule == 2:
rem_voice = {}
suggetor = SemanticCompare()
voice_ids = suggetor.get_all_voices_embedding()
book_info = [data[k] for k in data.keys() if k != 'file_path' and k != 'title']
ids, list_voices = suggetor.compare(suggetor.get_embedding(' '.join(book_info)), voice_ids)
num_voice_settings = len(list_voices)
index_voice_settings = random.randint(0, num_voice_settings - 1)
for id in voice_ids.keys():
if id == list_voices[index_voice_settings]:
rem_voice["voice_id"] = list_voices[index_voice_settings]
rem_voice["metadata"] = voice_ids[id]["metadata"]
break
# Level3: Recommendation system
elif level_rule == 3:
st.write(
"under construction"
)
return rem_voice
def main():
examples = [
{
"title": "The Subtle Art of Not Giving a Fuck A Counterintuitive Approach to Living a Good Life _Mark Manson",
"genere": "Self-Help",
"favor_gender": "male",
"favor_age": "middle age",
"favor_accent": "bristish",
"file_path": "data/The Subtle Art of Not Giving a Fuck A Counterintuitive Approach to Living a Good Life _Mark Manson.txt"
},
{
"title": "The Summer I Turned Pretty",
"genere": "Romance",
"favor_gender": "female",
"favor_age": "young",
"favor_accent": "bristish",
"file_path": "data/The Summer I Turned Pretty_Jenny Han.txt"
},
{
"title": "Greenlights",
"genere": "Autobiography",
"favor_gender": "male",
"favor_age": "old",
"favor_accent": "american",
"file_path": "data/Greenlights_Matthew McConaughey.txt"
}
]
# Select example
st.title("Book's metadata")
option = st.selectbox(
'Select the example:',
('','example1', 'example2', 'example3'))
if '1' in option:
input = examples[0]
elif '2' in option:
input = examples[1]
elif '3' in option:
input = examples[2]
else:
input = None
st.title('system input: ')
st.write(input)
level_rule = 2
button_generate = st.button("generate voice settings")
if button_generate and input != None:
rem_voice = select_settings_by_rulebase(level_rule, input)
st.title('recommend voice setting: ')
st.write(rem_voice)
voice_id = None
if rem_voice != None:
voice_id = rem_voice['voice_id']
text_book = read_text_file(input['file_path'])
with st.expander("the sample of the book"):
st.write(text_book)
voice_settings = {
"text": text_book,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5,
"style": 0.5,
"use_speaker_boost": True
}
}
save_path = 'test_audio.wav'
with st.spinner("Waiting for the audio book"):
ret = generate_speech_11lab(voice_settings, voice_id, save_path)
if 'successful' in ret:
st.write("The audio generating after suggestion")
st.audio(save_path)
if __name__ == '__main__':
main()