-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
140 lines (117 loc) · 4.41 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
import streamlit as st
import azure.cognitiveservices.speech as speechsdk
import pydub
from PIL import Image
import traceback
import time
from dotenv import load_dotenv
import os
# Page Configuration
st.set_page_config(
page_title="Speech-to-Text Web App",
page_icon="🎧",
layout="wide",
)
st.markdown(
"""
<style>
.main-header {
font-size: 36px;
font-weight: bold;
text-align: center;
margin-bottom: 10px;
color: #620E8A;
}
.sub-header {
font-size: 14px;
text-align: center;
margin-bottom: 20px;
color: #555;
}
.image {
justify-content: center;
text-align: center;
}
footer {visibility: hidden;}
</style>
<div class="main-header">Speech-to-Text Web App</div>
<div class="sub-header">Transcribe audio files with Azure Speech SDK</div>
""",
unsafe_allow_html=True,
)
load_dotenv()
azure_key = os.getenv("AZURE_SPEECH_KEY")
print("SDGDFGDFG")
print(azure_key)
def speech_recognize_from_file(sound, selected_language):
speech_config = speechsdk.SpeechConfig(subscription=azure_key, region="westeurope")
audio_config = speechsdk.audio.AudioConfig(filename=sound)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, language=selected_language, audio_config=audio_config)
result = speech_recognizer.recognize_once()
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
return result.text
elif result.reason == speechsdk.ResultReason.NoMatch:
return "No Speech Recognized: {}".format(result.no_match_details)
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
if cancellation_details.reason == speechsdk.CancellationReason.Error:
return "Speech Recognition canceled due to error: {}".format(cancellation_details.error_details)
return "Speech Recognition canceled: {}".format(cancellation_details.reason)
image = Image.open('./1.png')
st.image(image)
col1, col2 = st.columns([1, 2])
with col1:
# st.markdown("Select Language")
language_options = {
"English (US)": "en-US",
"French (FR)": "fr-FR",
"Spanish (ES)": "es-ES",
"German (DE)": "de-DE",
"Chinese (Mandarin)": "zh-CN"
}
selected_language = st.selectbox("Choose the language", list(language_options.keys()))
with col2:
# st.markdown("Upload Your Audio File")
uploaded_file = st.file_uploader("Choose a WAV file", type=["wav"])
if uploaded_file:
with open("temp_audio.wav", "wb") as f:
f.write(uploaded_file.getbuffer())
st.audio(uploaded_file, format="audio/wav")
# Transcription Process
if st.button("Transcribe Audio"):
with st.spinner("Transcribing..."):
transcription = speech_recognize_from_file("temp_audio.wav", language_options[selected_language])
if transcription:
st.success("Transcription Completed!")
st.text_area("Transcribed Text", transcription, height=200)
# Save Transcription History
with open("transcription_history.txt", "a") as history_file:
history_file.write(f"File: {uploaded_file.name}\nLanguage: {selected_language}\nTranscription: {transcription}\n---\n")
# Export Transcription
st.download_button(
label="Download Transcription as Text File",
data=transcription,
file_name="transcription.txt",
mime="text/plain",
)
else:
st.error("Transcription failed. Please try again.")
# Footer Section
st.caption(
"""
<style>
.footer {
text-align: center;
font-size: 14px;
margin-top: 50px;
color: #888;
}
</style>
<div class="footer">
Created with ❤️ by <a href="/~https://github.com/s-shahpouri" target="_blank">Sama</a><br>
This app uses <a href="https://ai.azure.com/explore/aiservices/speech" target="_blank">Azure Speech-to-Text SDK</a> to transcribe audio files into text.<br>
It is built with <a href="https://www.python.org" target="_blank">Python</a> and deployed on <a href="https://www.heroku.com" target="_blank">Heroku</a>, hosted on <a href="https://streamlit.io" target="_blank">Streamlit</a>.<br>
</div>
""",
unsafe_allow_html=True,
)