Sammaali commited on
Commit
778f5dc
·
verified ·
1 Parent(s): b23bcf3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -112
app.py CHANGED
@@ -1,131 +1,62 @@
1
- import gradio as gr
2
  import re
3
- import requests
4
-
5
- # =========================
6
- # ElevenLabs Configuration
7
- # =========================
8
-
9
- ELEVENLABS_API_KEY = "c92a87a2ebb5f51ee9fe90cc421e836e32780c188f4e0056d77ce69803008ae9"
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
- "enable_logging": "false"
64
- }
65
-
66
- response = requests.post(
67
- STT_URL,
68
- headers=headers,
69
- files=files,
70
- data=data
71
- )
72
-
73
- if response.status_code != 200:
74
- return "Error: " + response.text, ""
75
 
76
- result = response.json()
 
 
 
77
 
78
- # Extract speaker_0 text
79
- text = ""
80
 
81
- if "segments" in result:
82
- for segment in result["segments"]:
83
- if segment.get("speaker") == "speaker_0":
84
- text += segment.get("text", "") + " "
85
 
86
- else:
87
- text = result.get("text", "")
 
88
 
89
- cleaned = clean_arabic_text(text)
90
 
91
- return text, cleaned
92
 
 
93
 
94
- # =========================
95
- # Gradio UI
96
- # =========================
97
 
98
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
99
 
100
- gr.Markdown("# ElevenLabs Speech To Text + Post Process")
 
101
 
102
- gr.Markdown(
103
- "ارفع ملف صوتي (wav) وسيتم تحويله إلى نص عربي أو إنجليزي مع تنظيف النص."
104
- )
105
 
106
- audio_input = gr.Audio(
107
- type="filepath",
108
- label="Upload audio.wav"
109
- )
110
 
111
- raw_text = gr.Textbox(
112
- label="Original Text",
113
- lines=8
114
- )
115
 
116
- clean_text = gr.Textbox(
117
- label="Cleaned Text",
118
- lines=8
119
- )
120
 
121
- btn = gr.Button("Transcribe")
 
122
 
123
- btn.click(
124
- fn=transcribe_audio,
125
- inputs=audio_input,
126
- outputs=[raw_text, clean_text]
127
- )
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
- if __name__ == "__main__":
131
- demo.launch()
 
 
1
  import re
2
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # regex patterns
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)
8
 
9
+ def is_filler(word):
10
+ w = word.lower()
11
 
12
+ # حرف واحد مكرر (ممم، ووو، ااا)
13
+ if len(set(w)) == 1 and len(w) <= 4:
14
+ return True
 
15
 
16
+ # مقطع قصير جدا
17
+ if len(w) <= 2:
18
+ return True
19
 
20
+ return False
21
 
 
22
 
23
+ def clean_transcript(text):
24
 
25
+ # collapse stretched characters
26
+ text = CHAR_STRETCH.sub(r'\1', text)
 
27
 
28
+ # remove repeated words
29
+ text = REPEAT_WORD.sub(r'\1', text)
30
 
31
+ # remove repeated short syllables
32
+ text = REPEAT_SYLLABLE.sub(r'\1', text)
33
 
34
+ words = text.split()
 
 
35
 
36
+ filtered = []
37
+ for w in words:
38
+ if not is_filler(w):
39
+ filtered.append(w)
40
 
41
+ return " ".join(filtered)
 
 
 
42
 
 
 
 
 
43
 
44
+ def process(text):
45
+ return clean_transcript(text)
46
 
 
 
 
 
 
47
 
48
+ demo = gr.Interface(
49
+ fn=process,
50
+ inputs=gr.Textbox(
51
+ lines=8,
52
+ placeholder="Paste transcript here..."
53
+ ),
54
+ outputs=gr.Textbox(
55
+ lines=8,
56
+ label="Cleaned transcript"
57
+ ),
58
+ title="Transcript Filler Cleaner",
59
+ description="Remove repeated words and speech fillers automatically"
60
+ )
61
 
62
+ demo.launch()