Realmeas commited on
Commit
5c6bee1
ยท
verified ยท
1 Parent(s): 045c2fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -3,22 +3,28 @@ import whisper
3
  from googletrans import Translator
4
  import srt
5
  import datetime
 
 
6
 
 
7
  model = whisper.load_model("base")
8
  translator = Translator()
9
 
10
  def transcribe(audio):
11
  try:
12
- # Transcribe Hindi audio
13
- result = model.transcribe(audio, task="transcribe")
14
- segments = result["segments"]
15
 
 
 
 
16
  subtitles = []
17
  full_text = ""
18
 
19
  for i, seg in enumerate(segments):
20
- start = datetime.timedelta(seconds=int(seg["start"]))
21
- end = datetime.timedelta(seconds=int(seg["end"]))
22
  text = seg["text"].strip()
23
 
24
  # Translate Hindi โ†’ Hinglish (pronunciation)
@@ -29,18 +35,18 @@ def transcribe(audio):
29
  subtitles.append(srt.Subtitle(index=i+1, start=start, end=end, content=translated))
30
  full_text += translated + " "
31
 
32
- # Create SRT file content
33
  srt_content = srt.compose(subtitles)
34
 
35
- # Save as temporary file
36
- srt_path = "output.srt"
37
  with open(srt_path, "w", encoding="utf-8") as f:
38
  f.write(srt_content)
39
 
40
  return full_text.strip(), srt_path
41
 
42
  except Exception as e:
43
- return f"โŒ Error: {str(e)}", None
44
 
45
 
46
  iface = gr.Interface(
@@ -50,8 +56,8 @@ iface = gr.Interface(
50
  gr.Textbox(label="๐Ÿ“ Hinglish Subtitles"),
51
  gr.File(label="โฌ‡๏ธ Download SRT File")
52
  ],
53
- title="Hinglish Subtitle Generator",
54
- description="Upload Hindi speech audio โ€” get Hinglish subtitles and SRT file instantly!"
55
  )
56
 
57
  iface.launch()
 
3
  from googletrans import Translator
4
  import srt
5
  import datetime
6
+ import tempfile
7
+ import os
8
 
9
+ # Load model once
10
  model = whisper.load_model("base")
11
  translator = Translator()
12
 
13
  def transcribe(audio):
14
  try:
15
+ # Check if audio file exists
16
+ if audio is None:
17
+ return "โŒ No audio uploaded!", None
18
 
19
+ # Transcribe audio
20
+ result = model.transcribe(audio)
21
+ segments = result.get("segments", [])
22
  subtitles = []
23
  full_text = ""
24
 
25
  for i, seg in enumerate(segments):
26
+ start = datetime.timedelta(seconds=float(seg["start"]))
27
+ end = datetime.timedelta(seconds=float(seg["end"]))
28
  text = seg["text"].strip()
29
 
30
  # Translate Hindi โ†’ Hinglish (pronunciation)
 
35
  subtitles.append(srt.Subtitle(index=i+1, start=start, end=end, content=translated))
36
  full_text += translated + " "
37
 
38
+ # Create SRT file
39
  srt_content = srt.compose(subtitles)
40
 
41
+ # Temporary file save
42
+ srt_path = os.path.join(tempfile.gettempdir(), "output.srt")
43
  with open(srt_path, "w", encoding="utf-8") as f:
44
  f.write(srt_content)
45
 
46
  return full_text.strip(), srt_path
47
 
48
  except Exception as e:
49
+ return f"โŒ Runtime Error: {str(e)}", None
50
 
51
 
52
  iface = gr.Interface(
 
56
  gr.Textbox(label="๐Ÿ“ Hinglish Subtitles"),
57
  gr.File(label="โฌ‡๏ธ Download SRT File")
58
  ],
59
+ title="Hinglish Subtitle Generator ๐Ÿ‡ฎ๐Ÿ‡ณ",
60
+ description="Upload Hindi speech audio โ€” get Hinglish subtitles (.srt) instantly!"
61
  )
62
 
63
  iface.launch()