myyim commited on
Commit
bd4b687
ยท
verified ยท
1 Parent(s): 7ef9b7b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +74 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,76 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from kokoro import KPipeline
3
+ import soundfile as sf
4
+ import tempfile
5
 
6
+ languages = ['American English','British English','Spanish','French','Italian']
7
+ flags = ['๐Ÿ‡บ๐Ÿ‡ธ','๐Ÿ‡ฌ๐Ÿ‡ง','๐Ÿ‡ช๐Ÿ‡ธ','๐Ÿ‡ซ๐Ÿ‡ท','๐Ÿ‡ฎ๐Ÿ‡น']
8
+ voices = ['af_heart','am_adam','bf_isabella','bm_lewis','ef_dora','em_alex','ff_siwis','if_sara','im_nicola']
9
+
10
+ @st.cache_resource
11
+ def model_setup(lang_code='a'):
12
+ return KPipeline(lang_code=lang_code)
13
+
14
+ ### Title
15
+ st.title("Convert your text into audio")
16
+ st.subheader("with your options of language, speed and voice")
17
+
18
+ # Text
19
+ input_text = st.text_area(
20
+ "Your text:"
21
+ )
22
+
23
+ if input_text:
24
+ st.write(f"{len(input_text)} characters")
25
+ language = st.radio(
26
+ "Language:",
27
+ ['']+[flag+lang for flag,lang in zip(flags,languages)],
28
+ )
29
+
30
+ # Language
31
+ if language:
32
+ match language[2:]:
33
+ case 'American English':
34
+ lang_code = 'a'
35
+ case 'British English':
36
+ lang_code = 'b'
37
+ case 'Spanish':
38
+ lang_code = 'e'
39
+ case 'French':
40
+ lang_code = 'f'
41
+ case 'Italian':
42
+ lang_code = 'i'
43
+
44
+ # Speed
45
+ speed = st.radio(
46
+ "Speed:",
47
+ ['slow','normal','fast'],
48
+ index = 1
49
+ )
50
+ if speed == 'slow':
51
+ sp = 0.8
52
+ elif speed == 'normal':
53
+ sp = 1
54
+ elif speed == 'fast':
55
+ sp = 1.4
56
+
57
+ # Voice
58
+ voices_l = [voice for voice in voices if voice[0]==lang_code]
59
+ voices_gender = ['Female' if voice[1]=='f' else 'Male' for voice in voices_l]
60
+ voice = st.radio(
61
+ "Voice:",
62
+ ['']+voices_gender,
63
+ )
64
+ if voice:
65
+ if voice == 'Female':
66
+ voice = voices_l[0]
67
+ else:
68
+ voice = voices_l[1]
69
+
70
+ pipeline = KPipeline(lang_code=lang_code)
71
+ generator = pipeline(input_text, voice=voice, speed=sp, split_pattern=r'')
72
+
73
+ for i,(_, _, audio) in enumerate(generator):
74
+ with tempfile.TemporaryDirectory() as temp_dir:
75
+ sf.write(temp_dir+'temp.wav', audio, 24000)
76
+ st.audio(temp_dir+'temp.wav')