Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from gradio_client import Client
|
3 |
+
|
4 |
+
st.title("Whisper-JAX Speech-to-Text App")
|
5 |
+
|
6 |
+
# Specify the API URL
|
7 |
+
API_URL = "https://sanchit-gandhi-whisper-jax.hf.space"
|
8 |
+
|
9 |
+
# Initialize the Gradio client with the API URL
|
10 |
+
client = Client(API_URL)
|
11 |
+
|
12 |
+
# Function to transcribe audio using the specified API endpoint
|
13 |
+
def transcribe_audio(audio_path="temp.mp3", task="transcription", return_timestamps=False):
|
14 |
+
"""Function to transcribe an audio file using the Whisper-JAX endpoint."""
|
15 |
+
# Making a synchronous call to the predict method
|
16 |
+
# Note that file needs to be passed as a tuple with the format: (filename, filedata)
|
17 |
+
text, runtime = client.predict(
|
18 |
+
("file", open(audio_path, "rb")), # Opening file in binary read mode
|
19 |
+
task,
|
20 |
+
return_timestamps,
|
21 |
+
api_name="/predict_1" # Ensure this is the correct endpoint
|
22 |
+
)
|
23 |
+
return text, runtime
|
24 |
+
|
25 |
+
# Streamlit widget to upload an audio file
|
26 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=['mp3', 'wav', 'ogg'])
|
27 |
+
|
28 |
+
# Options for the task and timestamp inclusion
|
29 |
+
task = st.radio("Choose a task", ["Transcription", "Translation"], index=0)
|
30 |
+
return_timestamps = st.checkbox("Return timestamps with transcription")
|
31 |
+
|
32 |
+
# Button to process the audio file
|
33 |
+
if st.button("Transcribe Audio"):
|
34 |
+
if uploaded_file is not None:
|
35 |
+
# Save uploaded file temporarily
|
36 |
+
file_path = f"temp.mp3"
|
37 |
+
with open(file_path, "wb") as f:
|
38 |
+
f.write(uploaded_file.getbuffer())
|
39 |
+
|
40 |
+
# Call the transcribe function
|
41 |
+
try:
|
42 |
+
transcription = transcribe_audio()
|
43 |
+
st.write("Transcription:", transcription)
|
44 |
+
except Exception as e:
|
45 |
+
st.error(f"An error occurred during transcription: {str(e)}")
|
46 |
+
finally:
|
47 |
+
# Clean up the temporary file
|
48 |
+
import os
|
49 |
+
os.remove(file_path)
|
50 |
+
else:
|
51 |
+
st.error("Please upload an audio file to proceed.")
|