Skip to content

Commit

Permalink
Merge pull request #59 from ks6088ts-labs/feature/issue-58_tts-app
Browse files Browse the repository at this point in the history
add text to speech app
  • Loading branch information
ks6088ts authored Aug 12, 2024
2 parents 499cd9f + a7d30f5 commit 66e57d1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ AZURE_OPENAI_API_KEY="<YOUR_API_KEY>"
AZURE_OPENAI_API_VERSION="2024-06-01"
AZURE_OPENAI_GPT_MODEL="gpt-4o"
AZURE_OPENAI_STT_MODEL="whisper"
AZURE_OPENAI_TTS_VOICE="alloy"
AZURE_OPENAI_TTS_MODEL="tts-hd"
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-large"
AZURE_OPENAI_DALLE_MODEL="dall-e-3"

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ cython_debug/
# Project
*.env
generated/
*.mp3
8 changes: 6 additions & 2 deletions apps/99_streamlit_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,15 @@ Access to http://localhost:8501 and select the sample you want to run from the s

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

#### 7. Create image
#### 7. Text to Speech

![Text to Speech](../../docs/images/99_streamlit_examples.tts.png)

#### 8. Create image

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

#### 8. Visualize location
#### 9. Visualize location

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

Expand Down
72 changes: 72 additions & 0 deletions apps/99_streamlit_examples/pages/7_Text_to_speech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from os import getenv

import streamlit as st
from dotenv import load_dotenv
from openai import AzureOpenAI

load_dotenv()

with st.sidebar:
azure_openai_endpoint = st.text_input(
label="AZURE_OPENAI_ENDPOINT",
value=getenv("AZURE_OPENAI_ENDPOINT"),
key="AZURE_OPENAI_ENDPOINT",
type="default",
)
azure_openai_api_key = st.text_input(
label="AZURE_OPENAI_API_KEY",
key="AZURE_OPENAI_API_KEY",
type="password",
)
azure_openai_api_version = st.text_input(
label="AZURE_OPENAI_API_VERSION",
value=getenv("AZURE_OPENAI_API_VERSION"),
key="AZURE_OPENAI_API_VERSION",
type="default",
)
azure_openai_tts_model = st.text_input(
label="AZURE_OPENAI_TTS_MODEL",
value=getenv("AZURE_OPENAI_TTS_MODEL"),
key="AZURE_OPENAI_TTS_MODEL",
type="default",
)
"[Azure Portal](https://portal.azure.com/)"
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/7_Text_to_speech.py)"

st.title("Text to speech")

if not azure_openai_api_key or not azure_openai_endpoint or not azure_openai_api_version or not azure_openai_tts_model:
st.warning("Please fill in the required fields at the sidebar.")
st.stop()

st.info("This is a sample to convert text to speech.")

content = st.text_area(
label="Text to speech",
value="",
)
st.write(f"You wrote {len(content)} characters.")

if st.button("Convert"):
client = AzureOpenAI(
api_key=azure_openai_api_key,
api_version=azure_openai_api_version,
azure_endpoint=azure_openai_endpoint,
)
path_to_output = "output.mp3"

with st.spinner("Converting..."):
response = client.audio.speech.create(
input=content,
voice=getenv("AZURE_OPENAI_TTS_VOICE"),
model=azure_openai_tts_model,
response_format="mp3",
)
response.write_to_file(path_to_output)

st.audio(
data=path_to_output,
format="audio/mpeg",
loop=False,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
"[Azure Portal](https://portal.azure.com/)"
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/7_Create_image.py)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/8_Create_image.py)"

st.title("Create image")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
with st.sidebar:
"[Azure Portal](https://portal.azure.com/)"
"[Azure OpenAI Studio](https://oai.azure.com/resource/overview)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/8_Visualize_location.py)"
"[View the source code](https://github.com/ks6088ts-labs/workshop-azure-openai/blob/main/apps/99_streamlit_examples/pages/9_Visualize_location.py)"

st.title("Visualize location")
st.info("This is a sample to visualize location.")
Expand Down
Binary file added docs/images/99_streamlit_examples.tts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ def test_smoke():
# "apps/7_streamlit_chat_rag/main.py",
"apps/8_streamlit_azure_openai_batch/main.py",
"apps/99_streamlit_examples/main.py",
"apps/99_streamlit_examples/pages/1_File_Q&A.py",
"apps/99_streamlit_examples/pages/2_Image_Q&A.py",
"apps/99_streamlit_examples/pages/3_Camera_Q&A.py",
"apps/99_streamlit_examples/pages/4_Translate_text.py",
"apps/99_streamlit_examples/pages/5_Explain_data.py",
"apps/99_streamlit_examples/pages/6_Speech_to_text.py",
"apps/99_streamlit_examples/pages/7_Text_to_speech.py",
"apps/99_streamlit_examples/pages/8_Create_image.py",
"apps/99_streamlit_examples/pages/9_Visualize_location.py",
]
for path in paths:
at = AppTest(
Expand Down

0 comments on commit 66e57d1

Please sign in to comment.