Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,24 +67,26 @@ def convert_audio_to_text(uploaded_file):
|
|
| 67 |
|
| 68 |
# معالجة ملفات الفيديو
|
| 69 |
if input_path.split('.')[-1].lower() in ['mp4', 'avi', 'mov', 'mkv']:
|
| 70 |
-
VideoFileClip(input_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
output_path = input_path
|
| 73 |
|
| 74 |
audio, rate = librosa.load(output_path, sr=16000)
|
| 75 |
transcripts = []
|
| 76 |
-
|
| 77 |
-
# معالجة الصوت على شكل مقاطع
|
| 78 |
for start in np.arange(0, len(audio)/rate, 30):
|
| 79 |
end = min(start + 30, len(audio)/rate)
|
| 80 |
segment = audio[int(start*rate):int(end*rate)]
|
| 81 |
sf.write(f"/tmp/segment_{int(start)}.wav", segment, rate)
|
| 82 |
transcripts.append(pipe(f"/tmp/segment_{int(start)}.wav")["text"])
|
| 83 |
-
|
| 84 |
return " ".join(transcripts)
|
| 85 |
except Exception as e:
|
| 86 |
return f"⛔ خطأ: {str(e)}"
|
| 87 |
-
|
| 88 |
def process_example_audio():
|
| 89 |
try:
|
| 90 |
if not os.path.exists(EXAMPLE_AUDIO_PATH):
|
|
@@ -106,10 +108,25 @@ def summarize_text(text):
|
|
| 106 |
|
| 107 |
def answer_question(text, question):
|
| 108 |
if not question.strip() or not text.strip():
|
| 109 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
result = qa_pipeline({'question': question, 'context': context})
|
| 112 |
-
return result['answer']
|
| 113 |
|
| 114 |
def text_to_speech(text):
|
| 115 |
if not text.strip():
|
|
|
|
| 67 |
|
| 68 |
# معالجة ملفات الفيديو
|
| 69 |
if input_path.split('.')[-1].lower() in ['mp4', 'avi', 'mov', 'mkv']:
|
| 70 |
+
video = VideoFileClip(input_path)
|
| 71 |
+
if video.audio:
|
| 72 |
+
video.audio.write_audiofile(output_path, codec='pcm_s16le')
|
| 73 |
+
else:
|
| 74 |
+
return "⛔ لا يوجد صوت في الفيديو!"
|
| 75 |
else:
|
| 76 |
output_path = input_path
|
| 77 |
|
| 78 |
audio, rate = librosa.load(output_path, sr=16000)
|
| 79 |
transcripts = []
|
| 80 |
+
# تقسيم الصوت إلى مقاطع للتعامل مع الملفات الكبيرة
|
|
|
|
| 81 |
for start in np.arange(0, len(audio)/rate, 30):
|
| 82 |
end = min(start + 30, len(audio)/rate)
|
| 83 |
segment = audio[int(start*rate):int(end*rate)]
|
| 84 |
sf.write(f"/tmp/segment_{int(start)}.wav", segment, rate)
|
| 85 |
transcripts.append(pipe(f"/tmp/segment_{int(start)}.wav")["text"])
|
|
|
|
| 86 |
return " ".join(transcripts)
|
| 87 |
except Exception as e:
|
| 88 |
return f"⛔ خطأ: {str(e)}"
|
| 89 |
+
|
| 90 |
def process_example_audio():
|
| 91 |
try:
|
| 92 |
if not os.path.exists(EXAMPLE_AUDIO_PATH):
|
|
|
|
| 108 |
|
| 109 |
def answer_question(text, question):
|
| 110 |
if not question.strip() or not text.strip():
|
| 111 |
+
return "⛔️ الرجاء إدخال النص والسؤال بشكل صحيح"
|
| 112 |
+
|
| 113 |
+
# تقسيم النص إلى شرائح صغيرة بحيث لا تزيد كل شريحة عن 256 كلمة
|
| 114 |
+
words = text.split()
|
| 115 |
+
chunk_size = 256
|
| 116 |
+
segments = [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 117 |
+
|
| 118 |
+
best_answer = None
|
| 119 |
+
best_score = -1
|
| 120 |
+
|
| 121 |
+
# تطبيق نموذج الإجابة على كل شريحة واختيار الإجابة ذات أعلى score
|
| 122 |
+
for seg in segments:
|
| 123 |
+
result = qa_pipeline({'question': question, 'context': seg})
|
| 124 |
+
if result['score'] > best_score:
|
| 125 |
+
best_score = result['score']
|
| 126 |
+
best_answer = result['answer']
|
| 127 |
+
|
| 128 |
+
return best_answer
|
| 129 |
|
|
|
|
|
|
|
| 130 |
|
| 131 |
def text_to_speech(text):
|
| 132 |
if not text.strip():
|