Ofiroz91's picture
Update app.py
c2e4987 verified
raw
history blame
2.15 kB
import gradio as gr
from faster_whisper import WhisperModel
from pydub import AudioSegment
import os
import tempfile
from transformers import pipeline
# ื”ื’ื“ืจืช ื”ืžื•ื“ืœ ืœืชืžืœื•ืœ
model = WhisperModel("ivrit-ai/faster-whisper-v2-d4")
# ื”ื’ื“ืจืช pipeline ืœืกื™ื›ื•ื
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
def transcribe_and_summarize(file_path):
try:
# ื‘ื“ื™ืงื” ืื ื”ืงื•ื‘ืฅ ื”ื•ื ื•ื™ื“ืื• ื•ื”ืžืจืช ื•ื™ื“ืื• ืœืื•ื“ื™ื• ื‘ืžื™ื“ืช ื”ืฆื•ืจืš
if file_path.endswith((".mp4", ".mov", ".avi", ".mkv")):
audio_file = convert_video_to_audio(file_path)
else:
audio_file = file_path
# ืชืžืœื•ืœ ื”ืื•ื“ื™ื•
segments, _ = model.transcribe(audio_file, language="he")
transcript = " ".join([segment.text for segment in segments])
# ืกื™ื›ื•ื ื”ืชืžืœื•ืœ
summary = summarizer(transcript, max_length=50, min_length=25, do_sample=False)[0]["summary_text"]
prompt_text = f"ืกื›ื ืืช ื”ืชืžืœื•ืœ ื”ื‘ื ื›ืฉื™ืขื•ืจ ืืงื“ืžื™ ื‘ืขื‘ืจื™ืช:\n{transcript}"
# ืžื—ื™ืงืช ืงื•ื‘ืฅ ื”ืื•ื“ื™ื• ื‘ืžื™ื“ืช ื”ืฆื•ืจืš (ืื ื”ื™ื” ื•ื™ื“ืื•)
if audio_file != file_path:
os.remove(audio_file)
return transcript, summary
except Exception as e:
return f"ืฉื’ื™ืื” ื‘ืขื™ื‘ื•ื“ ื”ืงื•ื‘ืฅ: {str(e)}", ""
def convert_video_to_audio(video_file):
# ื™ืฆื™ืจืช ืงื•ื‘ืฅ ืื•ื“ื™ื• ื–ืžื ื™
temp_audio = tempfile.mktemp(suffix=".wav")
video = AudioSegment.from_file(video_file)
video.export(temp_audio, format="wav")
return temp_audio
# ื”ื’ื“ืจืช ืžืžืฉืง Gradio
interface = gr.Interface(
fn=transcribe_and_summarize,
inputs=gr.Audio(type="filepath"),
outputs=[
gr.Textbox(label="ืชืžืœื•ืœ"),
gr.Textbox(label="ืกื™ื›ื•ื")
],
title="ืžืžื™ืจ ืื•ื“ื™ื•/ื•ื™ื“ืื• ืœืชืžืœื•ืœ ื•ืกื™ื›ื•ื",
description="ื”ืขืœื” ืงื•ื‘ืฅ ืื•ื“ื™ื• ืื• ื•ื™ื“ืื• ืฉืœ ืžืจืฆื” ื•ืงื‘ืœ ืชืžืœื•ืœ ืžืœื ื•ืกื™ื›ื•ื ืงืฆืจ ืฉืœ ื”ืชื•ื›ืŸ."
)
if __name__ == "__main__":
interface.launch()