navidved commited on
Commit
55c61cc
Β·
verified Β·
1 Parent(s): 9943abb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -98
app.py CHANGED
@@ -1,20 +1,19 @@
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 | None):
13
- if file_path is None:
14
- return "Audio cleared.", "", None
15
-
16
  if not ASR_API_URL or not AUTH_TOKEN:
17
- return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", "", file_path
18
 
19
  headers = {
20
  "accept": "application/json",
@@ -23,101 +22,67 @@ def transcribe_audio(file_path: str | None):
23
 
24
  start = time.time()
25
  try:
26
- with open(file_path, "rb") as f:
27
- file_name = os.path.basename(file_path)
28
- # Adjusted mime type slightly - more general
29
- files = {"file": (file_name, f, "audio/wav")} # Use audio/wav or audio/mpeg as appropriate for your API
30
- resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
31
- except requests.exceptions.Timeout:
32
- return f"❌ Error: Request timed out after 120 seconds.", "", file_path
33
  except Exception as e:
34
- return f"❌ Error during API call or file handling: {e}", "", file_path
35
 
36
  elapsed = time.time() - start
37
  if resp.status_code == 200:
38
- try:
39
- data = resp.json()
40
- text = data.get("transcription", "No transcription returned.")
41
- processing_time = data.get('time', elapsed)
42
- return text, f"{processing_time:.2f} s", file_path
43
- except requests.exceptions.JSONDecodeError:
44
- return f"❌ Error: Could not decode JSON response. Status: {resp.status_code}, Response: {resp.text}", "", file_path
45
- else:
46
- return f"❌ Error: API returned status {resp.status_code}. Response: {resp.text}", "", file_path
47
 
 
 
48
 
49
- # ---------- Styling ----------
50
  VIOLET_MAIN = "#7F3FBF"
51
  VIOLET_LIGHT = "#C3A6FF"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- custom_css = f"""
54
- #gooya-title {{
55
- color:#fff;
56
- background:linear-gradient(90deg,{VIOLET_MAIN} 0%,{VIOLET_LIGHT} 100%);
57
- border-radius:12px;padding:20px 10px;margin-bottom:12px;
58
- }}
59
- .gooya-badge {{
60
- display:inline-block;background:{VIOLET_MAIN};color:#fff;
61
- border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;
62
- }}
63
- """
64
 
65
- # ---------- UI ----------
66
- with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
67
- gr.Markdown("# Gooya ASR v1.4 Transcription", elem_id="gooya-title")
68
- with gr.Row():
69
- with gr.Column():
70
- audio_input = gr.Audio(
71
- label="Audio Input (upload or record, up to 30 s)",
72
- type="filepath",
73
- sources=["upload", "microphone"],
74
- )
75
- with gr.Column():
76
- processing_time_tb = gr.Textbox(
77
- label="⏱️ Processing Time",
78
- interactive=False,
79
- elem_classes="gooya-badge",
80
- )
81
- transcription_tb = gr.Textbox(
82
- label="πŸ“ Transcription",
83
- lines=5,
84
- show_copy_button=True,
85
- placeholder="The transcription will appear here...",
86
- elem_id="gooya-textbox",
87
- )
88
-
89
- with gr.Row():
90
- btn_transcribe = gr.Button("Transcribe", variant="primary")
91
- btn_clear = gr.Button("Clear", variant="secondary")
92
-
93
- gr.Markdown(
94
- """
95
- **Guidelines**
96
- - Maximum audio length: **30 seconds**
97
- - Audio content should be in Persian.
98
- - Both transcription and processing time are displayed upon completion.
99
- - See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
100
- """
101
- )
102
-
103
- # ---------- Callbacks ----------
104
- btn_transcribe.click(
105
- fn=transcribe_audio,
106
- inputs=[audio_input],
107
- outputs=[transcription_tb, processing_time_tb, audio_input],
108
- )
109
-
110
- def clear_all():
111
- return "", "", None
112
-
113
- btn_clear.click(
114
- fn=clear_all,
115
- inputs=None,
116
- outputs=[transcription_tb, processing_time_tb, audio_input],
117
- )
118
-
119
- # ---------- Launch ----------
120
- if __name__ == "__main__":
121
- # On Hugging Face Spaces, share=True is not needed and causes a warning.
122
- # debug=True might also cause issues in some restricted environments, remove if necessary.
123
- demo.queue().launch(debug=False) # <-- Reverted share=True, optionally removed debug=True
 
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
 
18
  headers = {
19
  "accept": "application/json",
 
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
 
30
  elapsed = time.time() - start
31
  if resp.status_code == 200:
32
+ data = resp.json()
33
+ text = data.get("transcription", "No transcription returned.")
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", type="wav")
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
+ """)