Spaces:
Running
Running
File size: 2,808 Bytes
55c61cc 9c7cb99 5421e82 55c61cc 5421e82 55c61cc e83050a 5421e82 55c61cc 5b1e694 55c61cc 5421e82 56a2dd0 5b1e694 56a2dd0 5421e82 5b1e694 55c61cc 5b1e694 55c61cc 5421e82 5b1e694 55c61cc 5421e82 55c61cc 5421e82 55c61cc e824bc9 55c61cc 5421e82 55c61cc 5421e82 55c61cc |
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 |
import os
import time
import requests
import streamlit as st
# ---------- Environment Variables ----------
ASR_API_URL = os.getenv("ASR_API_URL")
AUTH_TOKEN = os.getenv("AUTH_TOKEN")
if not ASR_API_URL or not AUTH_TOKEN:
st.warning("β οΈ ASR_API_URL or AUTH_TOKEN is not set. API calls will fail.")
# ---------- Core Transcription Function ----------
def transcribe_audio(file_obj):
if not ASR_API_URL or not AUTH_TOKEN:
return "β Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
headers = {
"accept": "application/json",
"Authorization": f"Bearer {AUTH_TOKEN}",
}
start = time.time()
try:
files = {"file": ("audio.wav", file_obj, "audio/wav")}
resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
except Exception as e:
return f"β Error while calling ASR API: {e}", ""
elapsed = time.time() - start
if resp.status_code == 200:
data = resp.json()
text = data.get("transcription", "No transcription returned.")
return text, f"{data.get('time', elapsed):.2f} s"
return f"β Error: {resp.status_code}, {resp.text}", ""
# ---------- UI ----------
st.set_page_config(page_title="Gooya ASR v1.4", layout="centered")
VIOLET_MAIN = "#7F3FBF"
VIOLET_LIGHT = "#C3A6FF"
st.markdown(
f"""
<h1 style="background: linear-gradient(90deg, {VIOLET_MAIN}, {VIOLET_LIGHT}); color: white; padding: 20px; border-radius: 12px; text-align: center;">
Gooya ASR v1.4
</h1>
""",
unsafe_allow_html=True
)
tab1, tab2 = st.tabs(["π€ Record from Microphone", "π Upload Audio File"])
with tab1:
audio_file = st.audio_input("ποΈ Record audio from microphone")
with tab2:
uploaded_file = st.file_uploader("π Upload audio file (wav/mp3)", type=["wav", "mp3"])
col1, col2 = st.columns(2)
with col1:
btn_transcribe = st.button("Transcribe", type="primary")
with col2:
btn_clear = st.button("Clear")
# ---------- Main Logic ----------
if btn_transcribe:
file_to_process = uploaded_file if uploaded_file else audio_file
if file_to_process:
with st.spinner("β³ Processing..."):
transcription, elapsed = transcribe_audio(file_to_process)
st.text_area("π Transcription", transcription, height=150)
if elapsed:
st.info(f"β±οΈ Processing Time: {elapsed}")
else:
st.warning("Please upload or record an audio file first.")
if btn_clear:
st.experimental_rerun()
st.markdown("""
---
### Guidelines
- Maximum audio length: 30 seconds
- Audio content should be in Persian
- Both transcription and processing time will be displayed
π [View the Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
""")
|