Spaces:
Running
Running
feat: add transcript download, history list, and UI changes
Browse files- app/history.py +25 -0
- gradio_ui.py +64 -30
app/history.py
CHANGED
|
@@ -2,6 +2,7 @@ import json
|
|
| 2 |
import os
|
| 3 |
import datetime
|
| 4 |
import csv
|
|
|
|
| 5 |
|
| 6 |
HISTORY_FILE = "data/history.json"
|
| 7 |
|
|
@@ -31,6 +32,30 @@ def save_to_history(audio_filepath: str, transcript: str, language: str):
|
|
| 31 |
with open(HISTORY_FILE, "w") as f:
|
| 32 |
json.dump(history, f, indent=4)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def export_history(format: str = "csv"):
|
| 35 |
"""
|
| 36 |
Exports the saved history into a downloadable format.
|
|
|
|
| 2 |
import os
|
| 3 |
import datetime
|
| 4 |
import csv
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
HISTORY_FILE = "data/history.json"
|
| 8 |
|
|
|
|
| 32 |
with open(HISTORY_FILE, "w") as f:
|
| 33 |
json.dump(history, f, indent=4)
|
| 34 |
|
| 35 |
+
def get_history() -> list:
|
| 36 |
+
"""
|
| 37 |
+
Returns the list of past transcriptions for display in the UI.
|
| 38 |
+
Returns newest entries first.
|
| 39 |
+
"""
|
| 40 |
+
if not os.path.exists(HISTORY_FILE):
|
| 41 |
+
return []
|
| 42 |
+
try:
|
| 43 |
+
with open(HISTORY_FILE, "r") as f:
|
| 44 |
+
history = json.load(f)
|
| 45 |
+
return list(reversed(history))
|
| 46 |
+
except Exception:
|
| 47 |
+
return []
|
| 48 |
+
|
| 49 |
+
def save_transcript_as_txt(transcript: str) -> str:
|
| 50 |
+
"""
|
| 51 |
+
Saves a single transcript string to a temp .txt file and returns the path.
|
| 52 |
+
Used for the per-transcription download button.
|
| 53 |
+
"""
|
| 54 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8")
|
| 55 |
+
tmp.write(transcript)
|
| 56 |
+
tmp.close()
|
| 57 |
+
return tmp.name
|
| 58 |
+
|
| 59 |
def export_history(format: str = "csv"):
|
| 60 |
"""
|
| 61 |
Exports the saved history into a downloadable format.
|
gradio_ui.py
CHANGED
|
@@ -1,68 +1,102 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from app.asr_model import load_model, transcribe_audio
|
| 3 |
from app.language_detection import detect_language_from_text
|
| 4 |
-
from app.history import save_to_history, export_history
|
|
|
|
| 5 |
|
| 6 |
def process_audio(audio_path):
|
| 7 |
if audio_path is None:
|
| 8 |
-
return "No audio uploaded.", "Unknown"
|
| 9 |
-
|
| 10 |
print(f"\n--- New Request ---")
|
| 11 |
print(f"Processing audio: {audio_path}")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
print("Transcribing... (If this is the first time, it is downloading a 400MB model)")
|
| 15 |
transcript = transcribe_audio(audio_path)
|
| 16 |
-
print(f"Transcription complete: {transcript[:
|
| 17 |
-
|
| 18 |
-
# Detect Language from transcript
|
| 19 |
print("Detecting language...")
|
| 20 |
lang = detect_language_from_text(transcript)
|
| 21 |
-
|
| 22 |
-
# Save History
|
| 23 |
print("Saving to history...")
|
| 24 |
save_to_history(audio_path, transcript, lang)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
print("Done!\n")
|
| 27 |
-
return transcript, lang
|
| 28 |
|
| 29 |
def export_history_wrapper():
|
| 30 |
path = export_history("csv")
|
| 31 |
return path if path else None
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def create_ui():
|
| 34 |
with gr.Blocks(title="Multilingual ASR") as demo:
|
| 35 |
-
gr.Markdown("# Multilingual Automatic Speech Recognition")
|
| 36 |
-
|
| 37 |
with gr.Tabs():
|
| 38 |
-
with gr.TabItem("Transcribe"):
|
| 39 |
gr.Markdown("Upload an audio file to get a text transcription using Wav2Vec.")
|
| 40 |
-
|
| 41 |
with gr.Row():
|
| 42 |
with gr.Column():
|
| 43 |
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 44 |
transcribe_btn = gr.Button("Transcribe", variant="primary")
|
| 45 |
-
|
| 46 |
with gr.Column():
|
| 47 |
lang_output = gr.Textbox(label="Detected Language")
|
| 48 |
transcript_output = gr.Textbox(label="Transcription", lines=10)
|
| 49 |
-
|
|
|
|
| 50 |
transcribe_btn.click(
|
| 51 |
fn=process_audio,
|
| 52 |
inputs=audio_input,
|
| 53 |
-
outputs=[transcript_output, lang_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
-
|
| 56 |
-
with gr.TabItem("History"):
|
| 57 |
-
gr.Markdown("
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
-
|
| 66 |
return demo
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from app.asr_model import load_model, transcribe_audio
|
| 3 |
from app.language_detection import detect_language_from_text
|
| 4 |
+
from app.history import save_to_history, export_history, get_history, save_transcript_as_txt
|
| 5 |
+
import pandas as pd
|
| 6 |
|
| 7 |
def process_audio(audio_path):
|
| 8 |
if audio_path is None:
|
| 9 |
+
return "No audio uploaded.", "Unknown", None
|
| 10 |
+
|
| 11 |
print(f"\n--- New Request ---")
|
| 12 |
print(f"Processing audio: {audio_path}")
|
| 13 |
+
|
| 14 |
+
print("Transcribing...")
|
|
|
|
| 15 |
transcript = transcribe_audio(audio_path)
|
| 16 |
+
print(f"Transcription complete: {transcript[:80]}")
|
| 17 |
+
|
|
|
|
| 18 |
print("Detecting language...")
|
| 19 |
lang = detect_language_from_text(transcript)
|
| 20 |
+
|
|
|
|
| 21 |
print("Saving to history...")
|
| 22 |
save_to_history(audio_path, transcript, lang)
|
| 23 |
+
|
| 24 |
+
# Create a downloadable .txt file for the transcript
|
| 25 |
+
txt_path = save_transcript_as_txt(transcript)
|
| 26 |
+
|
| 27 |
print("Done!\n")
|
| 28 |
+
return transcript, lang, txt_path
|
| 29 |
|
| 30 |
def export_history_wrapper():
|
| 31 |
path = export_history("csv")
|
| 32 |
return path if path else None
|
| 33 |
|
| 34 |
+
def load_history_table():
|
| 35 |
+
"""Load history and return as a list of lists for gr.Dataframe."""
|
| 36 |
+
history = get_history()
|
| 37 |
+
if not history:
|
| 38 |
+
return []
|
| 39 |
+
rows = []
|
| 40 |
+
for entry in history:
|
| 41 |
+
rows.append([
|
| 42 |
+
entry.get("timestamp", "")[:19].replace("T", " "),
|
| 43 |
+
entry.get("audio_file", ""),
|
| 44 |
+
entry.get("language", ""),
|
| 45 |
+
entry.get("transcript", "")
|
| 46 |
+
])
|
| 47 |
+
return rows
|
| 48 |
+
|
| 49 |
def create_ui():
|
| 50 |
with gr.Blocks(title="Multilingual ASR") as demo:
|
| 51 |
+
gr.Markdown("# ๐๏ธ Multilingual Automatic Speech Recognition")
|
| 52 |
+
|
| 53 |
with gr.Tabs():
|
| 54 |
+
with gr.TabItem("๐ Transcribe"):
|
| 55 |
gr.Markdown("Upload an audio file to get a text transcription using Wav2Vec.")
|
| 56 |
+
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 60 |
transcribe_btn = gr.Button("Transcribe", variant="primary")
|
| 61 |
+
|
| 62 |
with gr.Column():
|
| 63 |
lang_output = gr.Textbox(label="Detected Language")
|
| 64 |
transcript_output = gr.Textbox(label="Transcription", lines=10)
|
| 65 |
+
download_txt = gr.File(label="โฌ๏ธ Download Transcript (.txt)", visible=False)
|
| 66 |
+
|
| 67 |
transcribe_btn.click(
|
| 68 |
fn=process_audio,
|
| 69 |
inputs=audio_input,
|
| 70 |
+
outputs=[transcript_output, lang_output, download_txt]
|
| 71 |
+
).then(
|
| 72 |
+
fn=lambda path: gr.File(value=path, visible=path is not None),
|
| 73 |
+
inputs=download_txt,
|
| 74 |
+
outputs=download_txt
|
| 75 |
)
|
| 76 |
+
|
| 77 |
+
with gr.TabItem("๐ History"):
|
| 78 |
+
gr.Markdown("Your past transcriptions (newest first).")
|
| 79 |
+
|
| 80 |
+
with gr.Row():
|
| 81 |
+
refresh_btn = gr.Button("๐ Refresh History")
|
| 82 |
+
export_btn = gr.Button("โฌ๏ธ Export as CSV")
|
| 83 |
+
|
| 84 |
+
history_table = gr.Dataframe(
|
| 85 |
+
headers=["Timestamp", "Audio File", "Language", "Transcript"],
|
| 86 |
+
datatype=["str", "str", "str", "str"],
|
| 87 |
+
value=load_history_table,
|
| 88 |
+
wrap=True,
|
| 89 |
+
label="Transcription History"
|
| 90 |
+
)
|
| 91 |
+
csv_file_output = gr.File(label="Download CSV", visible=False)
|
| 92 |
+
|
| 93 |
+
refresh_btn.click(fn=load_history_table, outputs=history_table)
|
| 94 |
+
export_btn.click(fn=export_history_wrapper, outputs=csv_file_output).then(
|
| 95 |
+
fn=lambda path: gr.File(value=path, visible=path is not None),
|
| 96 |
+
inputs=csv_file_output,
|
| 97 |
+
outputs=csv_file_output
|
| 98 |
)
|
| 99 |
+
|
| 100 |
return demo
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|