Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import pytz
|
3 |
+
import streamlit as st
|
4 |
+
from datetime import datetime
|
5 |
+
from streamlit_audio_recorder import AudioRecorder
|
6 |
+
|
7 |
+
API_URL = "https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud"
|
8 |
+
key = 'test-public-anonymous-T4-Whisper-Small-En'
|
9 |
+
headers = {
|
10 |
+
"Authorization": "Bearer {key}",
|
11 |
+
"Content-Type": "audio/wav"
|
12 |
+
}
|
13 |
+
|
14 |
+
def generate_filename(prompt, file_type):
|
15 |
+
central = pytz.timezone('US/Central')
|
16 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
17 |
+
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
18 |
+
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
|
19 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
20 |
+
|
21 |
+
def query(filename):
|
22 |
+
with open(filename, "rb") as f:
|
23 |
+
data = f.read()
|
24 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
25 |
+
return response.json()
|
26 |
+
|
27 |
+
def save_and_play_audio(audio_recorder):
|
28 |
+
audio_bytes = audio_recorder.get_audio()
|
29 |
+
if audio_bytes:
|
30 |
+
filename = generate_filename("Recording", "wav")
|
31 |
+
with open(filename, 'wb') as f:
|
32 |
+
f.write(audio_bytes)
|
33 |
+
st.audio(audio_bytes, format="audio/wav")
|
34 |
+
return filename
|
35 |
+
return None
|
36 |
+
|
37 |
+
st.title("Speech to Text")
|
38 |
+
st.write("Record your speech and get the text.")
|
39 |
+
|
40 |
+
audio_recorder = AudioRecorder()
|
41 |
+
audio_recorder.start()
|
42 |
+
if st.button("Stop recording"):
|
43 |
+
audio_recorder.stop()
|
44 |
+
filename = save_and_play_audio(audio_recorder)
|
45 |
+
if filename:
|
46 |
+
output = query(filename)
|
47 |
+
st.write("Transcription:")
|
48 |
+
st.write(output)
|