import gradio as gr from transformers import pipeline from pydub import AudioSegment import os import tempfile # יצירת pipeline לתמלול ולסיכום transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base") summarizer = pipeline("summarization", model="facebook/bart-large-cnn") def summarize_audio_or_video(file_path): try: # בדיקה אם הקובץ הוא וידאו והמרת וידאו לאודיו במידת הצורך if file_path.endswith((".mp4", ".mov", ".avi", ".mkv")): audio_file = convert_video_to_audio(file_path) else: audio_file = file_path # חלוקת האודיו לקטעים של 30 שניות segments = split_audio(audio_file) # תמלול כל קטע והצטרפות התמלול הסופי transcript = "" for segment in segments: segment_text = transcriber(segment, return_timestamps=False)["text"] transcript += " " + segment_text # סיכום התמלול summary = summarizer(transcript, max_length=50, min_length=25, do_sample=False)[0]["summary_text"] # מחיקת קובץ האודיו במידת הצורך (אם היה וידאו) if audio_file != file_path: os.remove(audio_file) return 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 def split_audio(audio_file, segment_length=30 * 1000): # 30 שניות במילישניות audio = AudioSegment.from_file(audio_file) segments = [] for i in range(0, len(audio), segment_length): segment = audio[i:i + segment_length] temp_segment = tempfile.mktemp(suffix=".wav") segment.export(temp_segment, format="wav") segments.append(temp_segment) return segments # הגדרת ממשק Gradio interface = gr.Interface( fn=summarize_audio_or_video, inputs=gr.Audio(type="filepath"), outputs="text", title="ממיר אודיו/וידאו לסיכום", description="העלה קובץ אודיו או וידאו של מרצה וקבל סיכום קצר של התוכן." ) if __name__ == "__main__": interface.launch()