navidved commited on
Commit
f118118
Β·
verified Β·
1 Parent(s): 0234931

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -58
app.py CHANGED
@@ -1,17 +1,15 @@
1
- import os
2
- import time
3
- import requests
4
- import streamlit as st
5
 
6
  # ---------- Environment Variables ----------
7
  ASR_API_URL = os.getenv("ASR_API_URL")
8
- AUTH_TOKEN = os.getenv("AUTH_TOKEN")
9
-
10
  if not ASR_API_URL or not AUTH_TOKEN:
11
- st.warning("⚠️ ASR_API_URL or AUTH_TOKEN is not set. API calls will fail.")
12
 
13
  # ---------- Core Transcription Function ----------
14
- def transcribe_audio(file_obj):
15
  if not ASR_API_URL or not AUTH_TOKEN:
16
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
17
 
@@ -22,8 +20,9 @@ def transcribe_audio(file_obj):
22
 
23
  start = time.time()
24
  try:
25
- files = {"file": ("audio.wav", file_obj, "audio/wav")}
26
- resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
 
27
  except Exception as e:
28
  return f"❌ Error while calling ASR API: {e}", ""
29
 
@@ -34,55 +33,73 @@ def transcribe_audio(file_obj):
34
  return text, f"{data.get('time', elapsed):.2f} s"
35
  return f"❌ Error: {resp.status_code}, {resp.text}", ""
36
 
37
- # ---------- UI ----------
38
- st.set_page_config(page_title="Gooya ASR v1.4", layout="centered")
39
-
40
- VIOLET_MAIN = "#7F3FBF"
41
  VIOLET_LIGHT = "#C3A6FF"
42
- st.markdown(
43
- f"""
44
- <h1 style="background: linear-gradient(90deg, {VIOLET_MAIN}, {VIOLET_LIGHT}); color: white; padding: 20px; border-radius: 12px; text-align: center;">
45
- Gooya ASR v1.4
46
- </h1>
47
- """,
48
- unsafe_allow_html=True
49
- )
50
-
51
- tab1, tab2 = st.tabs(["🎀 Record from Microphone", "πŸ“ Upload Audio File"])
52
-
53
- with tab1:
54
- audio_file = st.audio_input("πŸŽ™οΈ Record audio from microphone")
55
 
56
- with tab2:
57
- uploaded_file = st.file_uploader("πŸ“‚ Upload audio file (wav/mp3)", type=["wav", "mp3"])
 
 
 
 
 
 
 
 
 
58
 
59
- col1, col2 = st.columns(2)
60
- with col1:
61
- btn_transcribe = st.button("Transcribe", type="primary")
62
- with col2:
63
- btn_clear = st.button("Clear")
64
-
65
- # ---------- Main Logic ----------
66
- if btn_transcribe:
67
- file_to_process = uploaded_file if uploaded_file else audio_file
68
- if file_to_process:
69
- with st.spinner("⏳ Processing..."):
70
- transcription, elapsed = transcribe_audio(file_to_process)
71
- st.text_area("πŸ“ Transcription", transcription, height=150)
72
- if elapsed:
73
- st.info(f"⏱️ Processing Time: {elapsed}")
74
- else:
75
- st.warning("Please upload or record an audio file first.")
76
-
77
- if btn_clear:
78
- st.experimental_rerun()
79
-
80
- st.markdown("""
81
- ---
82
- ### Guidelines
 
 
 
 
 
 
83
  - Maximum audio length: 30 seconds
84
- - Audio content should be in Persian
85
- - Both transcription and processing time will be displayed
86
-
87
- πŸ”— [View the Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
88
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, time, requests, gradio as gr
2
+
3
+ print("Gradio version:", gr.version)
 
4
 
5
  # ---------- Environment Variables ----------
6
  ASR_API_URL = os.getenv("ASR_API_URL")
7
+ AUTH_TOKEN = os.getenv("AUTH_TOKEN")
 
8
  if not ASR_API_URL or not AUTH_TOKEN:
9
+ print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
10
 
11
  # ---------- Core Transcription Function ----------
12
+ def transcribe_audio(file_path: str):
13
  if not ASR_API_URL or not AUTH_TOKEN:
14
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
15
 
 
20
 
21
  start = time.time()
22
  try:
23
+ with open(file_path, "rb") as f:
24
+ files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
25
+ resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
26
  except Exception as e:
27
  return f"❌ Error while calling ASR API: {e}", ""
28
 
 
33
  return text, f"{data.get('time', elapsed):.2f} s"
34
  return f"❌ Error: {resp.status_code}, {resp.text}", ""
35
 
36
+ # ---------- Styling ----------
37
+ VIOLET_MAIN = "#7F3FBF"
 
 
38
  VIOLET_LIGHT = "#C3A6FF"
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ custom_css = f"""
41
+ #gooya-title {{
42
+ color:#fff;
43
+ background:linear-gradient(90deg,{VIOLET_MAIN} 0%,{VIOLET_LIGHT} 100%);
44
+ border-radius:12px;padding:20px 10px;margin-bottom:12px;
45
+ }}
46
+ .gooya-badge {{
47
+ display:inline-block;background:{VIOLET_MAIN};color:#fff;
48
+ border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;
49
+ }}
50
+ """
51
 
52
+ # ---------- UI ----------
53
+ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
54
+ with gr.Row():
55
+ with gr.Column():
56
+ audio_input = gr.Audio(
57
+ label="Audio Input (upload or record, up to 30 s)",
58
+ type="filepath",
59
+ sources=["upload", "microphone"],
60
+ )
61
+ with gr.Column():
62
+ processing_time_tb = gr.Textbox(
63
+ label="⏱️ Processing Time",
64
+ interactive=False,
65
+ elem_classes="gooya-badge",
66
+ )
67
+ transcription_tb = gr.Textbox(
68
+ label="πŸ“ Transcription",
69
+ lines=5,
70
+ show_copy_button=True,
71
+ placeholder="The transcription will appear here...",
72
+ elem_id="gooya-textbox",
73
+ )
74
+
75
+ with gr.Row():
76
+ btn_transcribe = gr.Button("Transcribe", variant="primary")
77
+ btn_clear = gr.Button("Clear", variant="secondary")
78
+
79
+ gr.Markdown(
80
+ """
81
+ Guidelines
82
  - Maximum audio length: 30 seconds
83
+ - Audio content should be in Persian.
84
+ - Both transcription and processing time are displayed immediately.
85
+
86
+ See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
87
+ """
88
+ )
89
+
90
+ # ---------- Callbacks ----------
91
+ btn_transcribe.click(
92
+ fn=transcribe_audio,
93
+ inputs=[audio_input],
94
+ outputs=[transcription_tb, processing_time_tb],
95
+ )
96
+
97
+ btn_clear.click(
98
+ lambda: ("", "", None),
99
+ inputs=None,
100
+ outputs=[transcription_tb, processing_time_tb, audio_input],
101
+ )
102
+
103
+ # ---------- Launch ----------
104
+ if name == "main":
105
+ demo.queue().launch(debug=True, share=False)