Shatha2030 commited on
Commit
16354a0
·
verified ·
1 Parent(s): f2822c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
4
+ from moviepy.video.io.VideoFileClip import VideoFileClip
5
+ import librosa
6
+ import soundfile as sf
7
+ import numpy as np
8
+ import re
9
+ import os
10
+
11
+ # ======================
12
+ # 1. الإعدادات الأساسية
13
+ # ======================
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+
16
+ # ======================
17
+ # 2. تحميل النماذج
18
+ # ======================
19
+ pipe = pipeline("automatic-speech-recognition", model="openai/whisper-medium", device=0 if device=="cuda" else -1)
20
+ bart_model = AutoModelForSeq2SeqLM.from_pretrained("ahmedabdo/arabic-summarizer-bart")
21
+ bart_tokenizer = AutoTokenizer.from_pretrained("ahmedabdo/arabic-summarizer-bart")
22
+
23
+ # ======================
24
+ # 3. الدوال المساعدة
25
+ # ======================
26
+ def clean_text(text):
27
+ return re.sub(r'\s+', ' ', text).strip()
28
+
29
+ def convert_audio_to_text(uploaded_file):
30
+ try:
31
+ if not uploaded_file:
32
+ return "⛔ الرجاء رفع ملف أولاً"
33
+
34
+ input_path = uploaded_file if isinstance(uploaded_file, str) else uploaded_file.name
35
+ output_path = "/tmp/processed.wav"
36
+
37
+ # معالجة ملفات الفيديو
38
+ if input_path.split('.')[-1].lower() in ['mp4', 'avi', 'mov', 'mkv']:
39
+ video = VideoFileClip(input_path)
40
+ if video.audio:
41
+ video.audio.write_audiofile(output_path, codec='pcm_s16le')
42
+ else:
43
+ return "⛔ لا يوجد صوت في الفيديو!"
44
+ else:
45
+ output_path = input_path
46
+
47
+ audio, rate = librosa.load(output_path, sr=16000)
48
+ transcripts = []
49
+ # تقسيم الصوت إلى مقاطع للتعامل مع الملفات الكبيرة
50
+ for start in np.arange(0, len(audio)/rate, 30):
51
+ end = min(start + 30, len(audio)/rate)
52
+ segment = audio[int(start*rate):int(end*rate)]
53
+ sf.write(f"/tmp/segment_{int(start)}.wav", segment, rate)
54
+ transcripts.append(pipe(f"/tmp/segment_{int(start)}.wav")["text"])
55
+ return " ".join(transcripts)
56
+ except Exception as e:
57
+ return f"⛔ خطأ: {str(e)}"
58
+
59
+ def summarize_text(text):
60
+ cleaned_text = clean_text(text)
61
+ inputs = bart_tokenizer(cleaned_text, return_tensors="pt", max_length=1024, truncation=True).to(device)
62
+ summary_ids = bart_model.generate(
63
+ inputs.input_ids,
64
+ max_length=150,
65
+ num_beams=4,
66
+ early_stopping=True
67
+ )
68
+ return bart_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
69
+
70
+ # ======================
71
+ # 4. المثال التجريبي
72
+ # ======================
73
+ EXAMPLE_AUDIO_PATH = "_⁨ما_هو_الذكاء_الإصطناعي_؟_فِهموجرافيك_١⁩.m4a"
74
+
75
+ def process_example_audio():
76
+ if not os.path.exists(EXAMPLE_AUDIO_PATH):
77
+ return "⛔ الملف التجريبي غير موجود"
78
+ return convert_audio_to_text(EXAMPLE_AUDIO_PATH)
79
+
80
+ # ======================
81
+ # 5. واجهة المستخدم
82
+ # ======================
83
+ with gr.Blocks() as demo:
84
+ gr.Markdown("## 🎤 استخراج النص وتلخيصه")
85
+
86
+ gr.Markdown("### 🔊 مثال تجريبي")
87
+ gr.Audio(EXAMPLE_AUDIO_PATH)
88
+ example_btn = gr.Button("تجربة المثال ⚡", elem_classes="custom-button")
89
+
90
+ file_input = gr.File(file_types=[".wav", ".mp3", ".mp4"])
91
+ extract_btn = gr.Button("استخراج النص", elem_classes="custom-button")
92
+ extracted_text = gr.Textbox(label="📝 النص المستخرج", lines=8)
93
+ summarize_btn = gr.Button("تلخيص النص", elem_classes="custom-button")
94
+ summary_output = gr.Textbox(label="📌 الملخص", lines=6)
95
+
96
+ extract_btn.click(convert_audio_to_text, inputs=file_input, outputs=extracted_text)
97
+ summarize_btn.click(summarize_text, inputs=extracted_text, outputs=summary_output)
98
+ example_btn.click(process_example_audio, outputs=extracted_text)
99
+
100
+ if __name__ == "__main__":
101
+ demo.launch()