Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
|
|
| 1 |
import re
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
REPEAT_WORD = re.compile(r'\b(\w+)(?:\s+\1\b)+', re.IGNORECASE)
|
| 6 |
CHAR_STRETCH = re.compile(r'(.)\1{2,}')
|
| 7 |
REPEAT_SYLLABLE = re.compile(r'\b(\w{1,3})(?:\s+\1\b)+', re.IGNORECASE)
|
|
@@ -12,6 +24,7 @@ def is_filler(word):
|
|
| 12 |
if len(set(w)) == 1 and len(w) <= 4:
|
| 13 |
return True
|
| 14 |
|
|
|
|
| 15 |
if len(w) <= 2:
|
| 16 |
return True
|
| 17 |
|
|
@@ -20,13 +33,8 @@ def is_filler(word):
|
|
| 20 |
|
| 21 |
def clean_transcript(text):
|
| 22 |
|
| 23 |
-
# collapse stretched characters
|
| 24 |
text = CHAR_STRETCH.sub(r'\1', text)
|
| 25 |
-
|
| 26 |
-
# remove repeated words
|
| 27 |
text = REPEAT_WORD.sub(r'\1', text)
|
| 28 |
-
|
| 29 |
-
# remove repeated short syllables
|
| 30 |
text = REPEAT_SYLLABLE.sub(r'\1', text)
|
| 31 |
|
| 32 |
words = text.split()
|
|
@@ -39,22 +47,78 @@ def clean_transcript(text):
|
|
| 39 |
return " ".join(filtered)
|
| 40 |
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
demo = gr.Interface(
|
| 47 |
-
fn=process,
|
| 48 |
-
inputs=gr.Textbox(
|
| 49 |
-
lines=8,
|
| 50 |
-
placeholder="Paste transcript here..."
|
| 51 |
-
),
|
| 52 |
-
outputs=gr.Textbox(
|
| 53 |
-
lines=8,
|
| 54 |
-
label="Cleaned transcript"
|
| 55 |
-
),
|
| 56 |
-
title="Transcript Filler Cleaner",
|
| 57 |
-
description="Remove repeated words and speech fillers automatically"
|
| 58 |
-
)
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import re
|
| 3 |
+
import requests
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
+
# ElevenLabs Config
|
| 8 |
+
# =========================
|
| 9 |
+
|
| 10 |
+
ELEVENLABS_API_KEY = os.getenv("sk_43cdb8a03472dc061bc3468dc05777022870f95dc2c907e7")
|
| 11 |
+
STT_URL = "https://api.elevenlabs.io/v1/speech-to-text"
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# Regex Cleaning
|
| 15 |
+
# =========================
|
| 16 |
+
|
| 17 |
REPEAT_WORD = re.compile(r'\b(\w+)(?:\s+\1\b)+', re.IGNORECASE)
|
| 18 |
CHAR_STRETCH = re.compile(r'(.)\1{2,}')
|
| 19 |
REPEAT_SYLLABLE = re.compile(r'\b(\w{1,3})(?:\s+\1\b)+', re.IGNORECASE)
|
|
|
|
| 24 |
if len(set(w)) == 1 and len(w) <= 4:
|
| 25 |
return True
|
| 26 |
|
| 27 |
+
# مقطع قصير جداً
|
| 28 |
if len(w) <= 2:
|
| 29 |
return True
|
| 30 |
|
|
|
|
| 33 |
|
| 34 |
def clean_transcript(text):
|
| 35 |
|
|
|
|
| 36 |
text = CHAR_STRETCH.sub(r'\1', text)
|
|
|
|
|
|
|
| 37 |
text = REPEAT_WORD.sub(r'\1', text)
|
|
|
|
|
|
|
| 38 |
text = REPEAT_SYLLABLE.sub(r'\1', text)
|
| 39 |
|
| 40 |
words = text.split()
|
|
|
|
| 47 |
return " ".join(filtered)
|
| 48 |
|
| 49 |
|
| 50 |
+
# =========================
|
| 51 |
+
# Speech To Text
|
| 52 |
+
# =========================
|
| 53 |
+
|
| 54 |
+
def transcribe_audio(audio_file):
|
| 55 |
+
|
| 56 |
+
if audio_file is None:
|
| 57 |
+
return "No audio uploaded", ""
|
| 58 |
+
|
| 59 |
+
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 60 |
+
|
| 61 |
+
with open(audio_file, "rb") as f:
|
| 62 |
+
files = {"file": f}
|
| 63 |
+
data = {
|
| 64 |
+
"model_id": "scribe_v2",
|
| 65 |
+
"enable_logging": "false"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
response = requests.post(
|
| 69 |
+
STT_URL,
|
| 70 |
+
headers=headers,
|
| 71 |
+
files=files,
|
| 72 |
+
data=data
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if response.status_code != 200:
|
| 76 |
+
return f"Error: {response.text}", ""
|
| 77 |
+
|
| 78 |
+
result = response.json()
|
| 79 |
+
|
| 80 |
+
text = ""
|
| 81 |
+
|
| 82 |
+
if "segments" in result:
|
| 83 |
+
for seg in result["segments"]:
|
| 84 |
+
text += seg.get("text", "") + " "
|
| 85 |
+
else:
|
| 86 |
+
text = result.get("text", "")
|
| 87 |
+
|
| 88 |
+
cleaned = clean_transcript(text)
|
| 89 |
+
|
| 90 |
+
return text, cleaned
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# =========================
|
| 94 |
+
# Gradio UI
|
| 95 |
+
# =========================
|
| 96 |
+
|
| 97 |
+
with gr.Blocks() as demo:
|
| 98 |
+
|
| 99 |
+
gr.Markdown("# Speech To Text Cleaner")
|
| 100 |
+
gr.Markdown("Upload audio → convert to text → remove fillers")
|
| 101 |
+
|
| 102 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 103 |
+
|
| 104 |
+
raw_text = gr.Textbox(
|
| 105 |
+
label="Original Transcript",
|
| 106 |
+
lines=8
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
cleaned_text = gr.Textbox(
|
| 110 |
+
label="Cleaned Transcript",
|
| 111 |
+
lines=8
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
btn = gr.Button("Transcribe")
|
| 115 |
|
| 116 |
+
btn.click(
|
| 117 |
+
fn=transcribe_audio,
|
| 118 |
+
inputs=audio_input,
|
| 119 |
+
outputs=[raw_text, cleaned_text]
|
| 120 |
+
)
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
if __name__ == "__main__":
|
| 124 |
+
demo.launch()
|