Offex commited on
Commit
f5712db
ยท
verified ยท
1 Parent(s): e643855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -5,8 +5,12 @@ import shutil
5
  import subprocess
6
  from faster_whisper import WhisperModel
7
 
 
 
 
 
8
  # ===============================
9
- # 1. Whisper Model
10
  # ===============================
11
  model = None
12
 
@@ -21,12 +25,11 @@ def load_model():
21
  # ===============================
22
  # 2. FFmpeg Path
23
  # ===============================
24
- def get_ffmpeg_path():
25
- path = shutil.which("ffmpeg")
26
- return path if path else "/usr/bin/ffmpeg"
27
 
28
  # ===============================
29
- # 3. Extract Audio
30
  # ===============================
31
  def extract_audio(video_path):
32
  audio_path = "uploaded_audio.wav"
@@ -34,7 +37,7 @@ def extract_audio(video_path):
34
  os.remove(audio_path)
35
 
36
  cmd = [
37
- get_ffmpeg_path(),
38
  "-i", video_path,
39
  "-vn",
40
  "-ac", "1",
@@ -46,15 +49,14 @@ def extract_audio(video_path):
46
  return audio_path
47
 
48
  # ===============================
49
- # 4. Download Audio
50
  # ===============================
51
  def download_audio_from_url(url):
52
- output = "url_audio.%(ext)s"
53
 
54
  ydl_opts = {
55
  "format": "bestaudio/best",
56
  "outtmpl": output,
57
- "ffmpeg_location": os.path.dirname(get_ffmpeg_path()),
58
  "postprocessors": [{
59
  "key": "FFmpegExtractAudio",
60
  "preferredcodec": "wav",
@@ -69,7 +71,18 @@ def download_audio_from_url(url):
69
  return "url_audio.wav"
70
 
71
  # ===============================
72
- # 5. Transcribe Function
 
 
 
 
 
 
 
 
 
 
 
73
  # ===============================
74
  def transcribe_media(url_input, file_input, language_choice):
75
 
@@ -89,14 +102,14 @@ def transcribe_media(url_input, file_input, language_choice):
89
  audio_path = download_audio_from_url(url_input)
90
 
91
  else:
92
- return "โš ๏ธ Please provide URL or Upload file."
93
 
94
  if not os.path.exists(audio_path):
95
  return "โŒ Audio processing failed."
96
 
97
  model = load_model()
98
 
99
- # Language Handling
100
  language = None if language_choice == "Auto Detect" else language_choice
101
 
102
  segments, info = model.transcribe(
@@ -107,16 +120,16 @@ def transcribe_media(url_input, file_input, language_choice):
107
  )
108
 
109
  detected_lang = info.language
 
 
110
 
111
- text = " ".join(seg.text for seg in segments)
112
-
113
- return f"๐ŸŒ Detected Language: {detected_lang}\n\n{text.strip()}"
114
 
115
  except Exception as e:
116
  return f"โŒ Error: {str(e)}"
117
 
118
  # ===============================
119
- # 6. UI
120
  # ===============================
121
  css = """
122
  .container {max-width: 900px; margin: auto;}
@@ -130,7 +143,10 @@ css = """
130
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
131
  with gr.Column(elem_classes="container"):
132
  gr.Markdown("## ๐Ÿš€ Universal Transcript Tool")
133
- gr.Markdown("Supports YouTube, TikTok, Instagram, Facebook, Twitter/X")
 
 
 
134
 
135
  with gr.Tabs():
136
  with gr.TabItem("๐Ÿ”— Paste Link"):
@@ -148,16 +164,16 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
148
  language_selector = gr.Dropdown(
149
  choices=[
150
  "Auto Detect",
151
- "en", # English
152
- "hi", # Hindi
153
  "ur", # Urdu
154
- "ar", # Arabic
155
- "fr", # French
156
- "de", # German
157
- "es", # Spanish
158
- "ru", # Russian
159
- "ja", # Japanese
160
- "zh" # Chinese
 
161
  ],
162
  value="Auto Detect",
163
  label="๐ŸŒ Select Transcript Language"
 
5
  import subprocess
6
  from faster_whisper import WhisperModel
7
 
8
+ # ๐Ÿ”ค Hindi Script Fix
9
+ from indic_transliteration import sanscript
10
+ from indic_transliteration.sanscript import transliterate
11
+
12
  # ===============================
13
+ # 1. Whisper Model (Lazy Load)
14
  # ===============================
15
  model = None
16
 
 
25
  # ===============================
26
  # 2. FFmpeg Path
27
  # ===============================
28
+ def get_ffmpeg():
29
+ return shutil.which("ffmpeg") or "/usr/bin/ffmpeg"
 
30
 
31
  # ===============================
32
+ # 3. Video โ†’ Audio
33
  # ===============================
34
  def extract_audio(video_path):
35
  audio_path = "uploaded_audio.wav"
 
37
  os.remove(audio_path)
38
 
39
  cmd = [
40
+ get_ffmpeg(),
41
  "-i", video_path,
42
  "-vn",
43
  "-ac", "1",
 
49
  return audio_path
50
 
51
  # ===============================
52
+ # 4. Download Audio from URL
53
  # ===============================
54
  def download_audio_from_url(url):
55
+ output = "url_audio"
56
 
57
  ydl_opts = {
58
  "format": "bestaudio/best",
59
  "outtmpl": output,
 
60
  "postprocessors": [{
61
  "key": "FFmpegExtractAudio",
62
  "preferredcodec": "wav",
 
71
  return "url_audio.wav"
72
 
73
  # ===============================
74
+ # 5. Hindi Script Normalizer
75
+ # ===============================
76
+ def normalize_script(text, lang):
77
+ if lang == "hi":
78
+ try:
79
+ return transliterate(text, sanscript.ARABIC, sanscript.DEVANAGARI)
80
+ except:
81
+ return text
82
+ return text
83
+
84
+ # ===============================
85
+ # 6. Main Transcribe Logic
86
  # ===============================
87
  def transcribe_media(url_input, file_input, language_choice):
88
 
 
102
  audio_path = download_audio_from_url(url_input)
103
 
104
  else:
105
+ return "โš ๏ธ Please paste a link or upload a file."
106
 
107
  if not os.path.exists(audio_path):
108
  return "โŒ Audio processing failed."
109
 
110
  model = load_model()
111
 
112
+ # Language handling
113
  language = None if language_choice == "Auto Detect" else language_choice
114
 
115
  segments, info = model.transcribe(
 
120
  )
121
 
122
  detected_lang = info.language
123
+ raw_text = " ".join(seg.text for seg in segments)
124
+ final_text = normalize_script(raw_text, detected_lang)
125
 
126
+ return f"๐ŸŒ Detected Language: {detected_lang}\n\n{final_text.strip()}"
 
 
127
 
128
  except Exception as e:
129
  return f"โŒ Error: {str(e)}"
130
 
131
  # ===============================
132
+ # 7. UI
133
  # ===============================
134
  css = """
135
  .container {max-width: 900px; margin: auto;}
 
143
  with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
144
  with gr.Column(elem_classes="container"):
145
  gr.Markdown("## ๐Ÿš€ Universal Transcript Tool")
146
+ gr.Markdown(
147
+ "Supports **YouTube, TikTok, Instagram, Facebook, Twitter/X**\n\n"
148
+ "Hindi output is always **Devanagari** ๐Ÿ‡ฎ๐Ÿ‡ณ"
149
+ )
150
 
151
  with gr.Tabs():
152
  with gr.TabItem("๐Ÿ”— Paste Link"):
 
164
  language_selector = gr.Dropdown(
165
  choices=[
166
  "Auto Detect",
167
+ "hi", # Hindi (Devanagari)
 
168
  "ur", # Urdu
169
+ "en", # English
170
+ "ar",
171
+ "fr",
172
+ "de",
173
+ "es",
174
+ "ru",
175
+ "ja",
176
+ "zh"
177
  ],
178
  value="Auto Detect",
179
  label="๐ŸŒ Select Transcript Language"