Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,132 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def clean_arabic_text(text):
|
| 5 |
if not text:
|
| 6 |
return ""
|
| 7 |
-
|
|
|
|
| 8 |
tashkeel_pattern = re.compile(r'[\u0617-\u061A\u064B-\u0652]')
|
| 9 |
text = re.sub(tashkeel_pattern, '', text)
|
| 10 |
-
|
|
|
|
| 11 |
text = re.sub(r'[أإآ]', 'ا', text)
|
| 12 |
-
|
|
|
|
| 13 |
text = re.sub(r'ة\b', 'ه', text)
|
| 14 |
-
|
|
|
|
| 15 |
text = re.sub(r'ى\b', 'ي', text)
|
| 16 |
-
|
|
|
|
| 17 |
text = re.sub(r'[^\w\s]', '', text)
|
| 18 |
-
|
|
|
|
| 19 |
text = " ".join(text.split())
|
|
|
|
| 20 |
return text
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 23 |
-
|
| 24 |
-
gr.Markdown("
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import re
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# ElevenLabs Configuration
|
| 7 |
+
# =========================
|
| 8 |
+
|
| 9 |
+
ELEVENLABS_API_KEY = "YOUR_API_KEY_HERE"
|
| 10 |
+
|
| 11 |
+
STT_URL = "https://api.elevenlabs.io/v1/speech-to-text"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# =========================
|
| 15 |
+
# Arabic Post Processing
|
| 16 |
+
# =========================
|
| 17 |
|
| 18 |
def clean_arabic_text(text):
|
| 19 |
if not text:
|
| 20 |
return ""
|
| 21 |
+
|
| 22 |
+
# Remove tashkeel
|
| 23 |
tashkeel_pattern = re.compile(r'[\u0617-\u061A\u064B-\u0652]')
|
| 24 |
text = re.sub(tashkeel_pattern, '', text)
|
| 25 |
+
|
| 26 |
+
# Normalize Hamza
|
| 27 |
text = re.sub(r'[أإآ]', 'ا', text)
|
| 28 |
+
|
| 29 |
+
# ة → ه
|
| 30 |
text = re.sub(r'ة\b', 'ه', text)
|
| 31 |
+
|
| 32 |
+
# ى → ي
|
| 33 |
text = re.sub(r'ى\b', 'ي', text)
|
| 34 |
+
|
| 35 |
+
# Remove symbols
|
| 36 |
text = re.sub(r'[^\w\s]', '', text)
|
| 37 |
+
|
| 38 |
+
# Remove extra spaces
|
| 39 |
text = " ".join(text.split())
|
| 40 |
+
|
| 41 |
return text
|
| 42 |
|
| 43 |
+
|
| 44 |
+
# =========================
|
| 45 |
+
# ElevenLabs Speech To Text
|
| 46 |
+
# =========================
|
| 47 |
+
|
| 48 |
+
def transcribe_audio(audio_file):
|
| 49 |
+
|
| 50 |
+
if audio_file is None:
|
| 51 |
+
return "No audio uploaded", ""
|
| 52 |
+
|
| 53 |
+
headers = {
|
| 54 |
+
"xi-api-key": ELEVENLABS_API_KEY
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
files = {
|
| 58 |
+
"file": open(audio_file, "rb")
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
data = {
|
| 62 |
+
"model_id": "scribe_v2",
|
| 63 |
+
"language_code": "auto", # Arabic + English
|
| 64 |
+
"enable_logging": "false"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
response = requests.post(
|
| 68 |
+
STT_URL,
|
| 69 |
+
headers=headers,
|
| 70 |
+
files=files,
|
| 71 |
+
data=data
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if response.status_code != 200:
|
| 75 |
+
return "Error: " + response.text, ""
|
| 76 |
+
|
| 77 |
+
result = response.json()
|
| 78 |
+
|
| 79 |
+
# Extract speaker_0 text
|
| 80 |
+
text = ""
|
| 81 |
+
|
| 82 |
+
if "segments" in result:
|
| 83 |
+
for segment in result["segments"]:
|
| 84 |
+
if segment.get("speaker") == "speaker_0":
|
| 85 |
+
text += segment.get("text", "") + " "
|
| 86 |
+
|
| 87 |
+
else:
|
| 88 |
+
text = result.get("text", "")
|
| 89 |
+
|
| 90 |
+
cleaned = clean_arabic_text(text)
|
| 91 |
+
|
| 92 |
+
return text, cleaned
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# =========================
|
| 96 |
+
# Gradio UI
|
| 97 |
+
# =========================
|
| 98 |
+
|
| 99 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 100 |
+
|
| 101 |
+
gr.Markdown("# ElevenLabs Speech To Text + Post Process")
|
| 102 |
+
|
| 103 |
+
gr.Markdown(
|
| 104 |
+
"ارفع ملف صوتي (wav) وسيتم تحويله إلى نص عربي أو إنجليزي مع تنظيف النص."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
audio_input = gr.Audio(
|
| 108 |
+
type="filepath",
|
| 109 |
+
label="Upload audio.wav"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
raw_text = gr.Textbox(
|
| 113 |
+
label="Original Text",
|
| 114 |
+
lines=8
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
clean_text = gr.Textbox(
|
| 118 |
+
label="Cleaned Text",
|
| 119 |
+
lines=8
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
btn = gr.Button("Transcribe")
|
| 123 |
+
|
| 124 |
+
btn.click(
|
| 125 |
+
fn=transcribe_audio,
|
| 126 |
+
inputs=audio_input,
|
| 127 |
+
outputs=[raw_text, clean_text]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|