Skip to content

Commit 66e57d1

Browse files
authored
Merge pull request #59 from ks6088ts-labs/feature/issue-58_tts-app
add text to speech app
2 parents 499cd9f + a7d30f5 commit 66e57d1

File tree

8 files changed

+92
-4
lines changed

8 files changed

+92
-4
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ AZURE_OPENAI_API_KEY="<YOUR_API_KEY>"
44
AZURE_OPENAI_API_VERSION="2024-06-01"
55
AZURE_OPENAI_GPT_MODEL="gpt-4o"
66
AZURE_OPENAI_STT_MODEL="whisper"
7+
AZURE_OPENAI_TTS_VOICE="alloy"
8+
AZURE_OPENAI_TTS_MODEL="tts-hd"
79
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-large"
810
AZURE_OPENAI_DALLE_MODEL="dall-e-3"
911

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@ cython_debug/
162162
# Project
163163
*.env
164164
generated/
165+
*.mp3

apps/99_streamlit_examples/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ Access to http://localhost:8501 and select the sample you want to run from the s
5757

5858
![Speech to Text](../../docs/images/99_streamlit_examples.stt.png)
5959

60-
#### 7. Create image
60+
#### 7. Text to Speech
61+
62+
![Text to Speech](../../docs/images/99_streamlit_examples.tts.png)
63+
64+
#### 8. Create image
6165

6266
![Create image](../../docs/images/99_streamlit_examples.createimage.png)
6367

64-
#### 8. Visualize location
68+
#### 9. Visualize location
6569

6670
![Visualize location](../../docs/images/99_streamlit_examples.map.png)
6771

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from os import getenv
2+
3+
import streamlit as st
4+
from dotenv import load_dotenv
5+
from openai import AzureOpenAI
6+
7+
load_dotenv()
8+
9+
with st.sidebar:
10+
azure_openai_endpoint = st.text_input(
11+
label="AZURE_OPENAI_ENDPOINT",
12+
value=getenv("AZURE_OPENAI_ENDPOINT"),
13+
key="AZURE_OPENAI_ENDPOINT",
14+
type="default",
15+
)
16+
azure_openai_api_key = st.text_input(
17+
label="AZURE_OPENAI_API_KEY",
18+
key="AZURE_OPENAI_API_KEY",
19+
type="password",
20+
)
21+
azure_openai_api_version = st.text_input(
22+
label="AZURE_OPENAI_API_VERSION",
23+
value=getenv("AZURE_OPENAI_API_VERSION"),
24+
key="AZURE_OPENAI_API_VERSION",
25+
type="default",
26+
)
27+
azure_openai_tts_model = st.text_input(
28+
label="AZURE_OPENAI_TTS_MODEL",
29+
value=getenv("AZURE_OPENAI_TTS_MODEL"),
30+
key="AZURE_OPENAI_TTS_MODEL",
31+
type="default",
32+
)
33+
"[Azure Portal](https://portal.azure.com/)"
34+
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
35+
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/7_Text_to_speech.py)"
36+
37+
st.title("Text to speech")
38+
39+
if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_tts_model:
40+
st.warning("Please fill in the required fields at the sidebar.")
41+
st.stop()
42+
43+
st.info("This is a sample to convert text to speech.")
44+
45+
content = st.text_area(
46+
label="Text to speech",
47+
value="",
48+
)
49+
st.write(f"You wrote {len(content)} characters.")
50+
51+
if st.button("Convert"):
52+
client = AzureOpenAI(
53+
api_key=azure_openai_api_key,
54+
api_version=azure_openai_api_version,
55+
azure_endpoint=azure_openai_endpoint,
56+
)
57+
path_to_output = "output.mp3"
58+
59+
with st.spinner("Converting..."):
60+
response = client.audio.speech.create(
61+
input=content,
62+
voice=getenv("AZURE_OPENAI_TTS_VOICE"),
63+
model=azure_openai_tts_model,
64+
response_format="mp3",
65+
)
66+
response.write_to_file(path_to_output)
67+
68+
st.audio(
69+
data=path_to_output,
70+
format="audio/mpeg",
71+
loop=False,
72+
)

apps/99_streamlit_examples/pages/7_Create_image.py renamed to apps/99_streamlit_examples/pages/8_Create_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
)
3434
"[Azure Portal](https://portal.azure.com/)"
3535
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
36-
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/7_Create_image.py)"
36+
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/8_Create_image.py)"
3737

3838
st.title("Create image")
3939

apps/99_streamlit_examples/pages/8_Visualize_location.py renamed to apps/99_streamlit_examples/pages/9_Visualize_location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
with st.sidebar:
99
"[Azure Portal](https://portal.azure.com/)"
1010
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
11-
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/8_Visualize_location.py)"
11+
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/9_Visualize_location.py)"
1212

1313
st.title("Visualize location")
1414
st.info("This is a sample to visualize location.")
116 KB
Loading

tests/test_smoke.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ def test_smoke():
99
# "apps/7_streamlit_chat_rag/main.py",
1010
"apps/8_streamlit_azure_openai_batch/main.py",
1111
"apps/99_streamlit_examples/main.py",
12+
"apps/99_streamlit_examples/pages/1_File_Q&A.py",
13+
"apps/99_streamlit_examples/pages/2_Image_Q&A.py",
14+
"apps/99_streamlit_examples/pages/3_Camera_Q&A.py",
15+
"apps/99_streamlit_examples/pages/4_Translate_text.py",
16+
"apps/99_streamlit_examples/pages/5_Explain_data.py",
17+
"apps/99_streamlit_examples/pages/6_Speech_to_text.py",
18+
"apps/99_streamlit_examples/pages/7_Text_to_speech.py",
19+
"apps/99_streamlit_examples/pages/8_Create_image.py",
20+
"apps/99_streamlit_examples/pages/9_Visualize_location.py",
1221
]
1322
for path in paths:
1423
at = AppTest(

0 commit comments

Comments
 (0)