Ofiroz91 commited on
Commit
aa6143f
ยท
verified ยท
1 Parent(s): 3305e68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -11
app.py CHANGED
@@ -1,24 +1,50 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
3
 
4
- # ื™ืฆื™ืจืช pipe ืœืชืžืœื•ืœ ื•ืœืกื™ื›ื•ื
5
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
6
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
 
8
- def summarize_audio(audio_file):
9
- # ืชืžืœื•ืœ ื”ืื•ื“ื™ื•
10
- transcript = transcriber(audio_file)["text"]
11
- # ื™ืฆื™ืจืช ืกื™ื›ื•ื ืฉืœ ื”ืชืžืœื•ืœ
12
- summary = summarizer(transcript, max_length=50, min_length=25, do_sample=False)[0]["summary_text"]
13
- return summary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # ื”ื’ื“ืจืช ืžืžืฉืง Gradio
16
  interface = gr.Interface(
17
- fn=summarize_audio,
18
- inputs=gr.Audio(type="filepath"), # ืœืœื source
19
  outputs="text",
20
- title="ืžืžื™ืจ ืื•ื“ื™ื• ืœืกื™ื›ื•ื",
21
- description="ื”ืขืœื” ืงื•ื‘ืฅ ืื•ื“ื™ื• ืฉืœ ืžืจืฆื” ื•ืงื‘ืœ ืกื™ื›ื•ื ืงืฆืจ ืฉืœ ื”ืชื•ื›ืŸ."
22
  )
23
 
24
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from pydub import AudioSegment
4
+ import os
5
+ import tempfile
6
 
7
+ # ื™ืฆื™ืจืช pipeline ืœืชืžืœื•ืœ ื•ืœืกื™ื›ื•ื
8
  transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base")
9
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
 
11
+ def summarize_audio_or_video(file_path):
12
+ try:
13
+ # ื‘ื“ื™ืงื” ืื ื”ืงื•ื‘ืฅ ื”ื•ื ื•ื™ื“ืื• ื•ื”ืžืจืช ื•ื™ื“ืื• ืœืื•ื“ื™ื• ื‘ืžื™ื“ืช ื”ืฆื•ืจืš
14
+ if file_path.endswith((".mp4", ".mov", ".avi", ".mkv")):
15
+ audio_file = convert_video_to_audio(file_path)
16
+ else:
17
+ audio_file = file_path
18
+
19
+ # ืชืžืœื•ืœ ื”ืื•ื“ื™ื•
20
+ transcript = transcriber(audio_file)["text"]
21
+
22
+ # ื™ืฆื™ืจืช ืกื™ื›ื•ื ืฉืœ ื”ืชืžืœื•ืœ
23
+ summary = summarizer(transcript, max_length=50, min_length=25, do_sample=False)[0]["summary_text"]
24
+
25
+ # ืžื—ื™ืงืช ืงื•ื‘ืฅ ื”ืื•ื“ื™ื• ื‘ืžื™ื“ืช ื”ืฆื•ืจืš (ืื ื”ื™ื” ื•ื™ื“ืื•)
26
+ if audio_file != file_path:
27
+ os.remove(audio_file)
28
+
29
+ return summary
30
+
31
+ except Exception as e:
32
+ return f"ืฉื’ื™ืื” ื‘ืขื™ื‘ื•ื“ ื”ืงื•ื‘ืฅ: {str(e)}"
33
+
34
+ def convert_video_to_audio(video_file):
35
+ # ื™ืฆื™ืจืช ืงื•ื‘ืฅ ืื•ื“ื™ื• ื–ืžื ื™
36
+ temp_audio = tempfile.mktemp(suffix=".wav")
37
+ video = AudioSegment.from_file(video_file)
38
+ video.export(temp_audio, format="wav")
39
+ return temp_audio
40
 
41
  # ื”ื’ื“ืจืช ืžืžืฉืง Gradio
42
  interface = gr.Interface(
43
+ fn=summarize_audio_or_video,
44
+ inputs=gr.Audio(type="filepath"), # ืžืชืื™ื ืœืื•ื“ื™ื• ื•ื•ื™ื“ืื•
45
  outputs="text",
46
+ title="ืžืžื™ืจ ืื•ื“ื™ื•/ื•ื™ื“ืื• ืœืกื™ื›ื•ื",
47
+ description="ื”ืขืœื” ืงื•ื‘ืฅ ืื•ื“ื™ื• ืื• ื•ื™ื“ืื• ืฉืœ ืžืจืฆื” ื•ืงื‘ืœ ืกื™ื›ื•ื ืงืฆืจ ืฉืœ ื”ืชื•ื›ืŸ."
48
  )
49
 
50
  if __name__ == "__main__":