Shatha2030 commited on
Commit
e281787
·
verified ·
1 Parent(s): 8ec36ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -31
app.py CHANGED
@@ -22,28 +22,18 @@ pipe = pipeline("automatic-speech-recognition", model="openai/whisper-medium", d
22
  bart_model = AutoModelForSeq2SeqLM.from_pretrained("ahmedabdo/arabic-summarizer-bart")
23
  bart_tokenizer = AutoTokenizer.from_pretrained("ahmedabdo/arabic-summarizer-bart")
24
 
25
- # تحميل نماذج الترجمة باستخدام نموذج Helsinki-NLP
26
- translate_ar_to_en = pipeline(
27
- "translation",
28
- model="Helsinki-NLP/opus-mt-ar-en"
29
- )
30
- translate_en_to_ar = pipeline(
31
- "translation",
32
- model="Helsinki-NLP/opus-mt-en-ar"
33
- )
34
-
35
- # تحميل نموذج الإجابة الإنجليزي الأصغر
36
- qa_model_name = "deepset/bert-base-cased-squad2"
37
- qa_pipeline = pipeline("question-answering", model=qa_model_name, tokenizer=qa_model_name)
38
 
39
  # ======================
40
  # 3. التخصيصات
41
  # ======================
42
  PRIMARY_COLOR = "#2A4D6E" # اللون الكحلي
43
- EXAMPLE_AUDIO_PATH = "_⁨ما_هو_الذكاء_الإصطناعي_؟_فِهموجرافيك_١⁩.m4a"
44
 
45
  # ======================
46
- # 4. الدوال المساعدة (الإصدار المحسّن)
47
  # ======================
48
  def clean_text_advanced(text):
49
  """تنظيف النص مع الحفاظ على الهيكل الأساسي"""
@@ -118,21 +108,8 @@ def answer_question(text, question):
118
  if not question.strip() or not text.strip():
119
  return "⛔ الرجاء إدخال النص والسؤال بشكل صحيح"
120
 
121
- # ترجمة النص والسؤال إلى الإنجليزية لإجراء الإجابة
122
- try:
123
- translated_context = translate_ar_to_en(text)[0]['translation_text']
124
- translated_question = translate_ar_to_en(question)[0]['translation_text']
125
- except Exception as e:
126
- return f"⛔ خطأ في الترجمة: {str(e)}"
127
-
128
- try:
129
- results = qa_pipeline({'question': translated_question, 'context': translated_context}, top_k=3)
130
- best_result = max(results, key=lambda res: res['score'])
131
- # ترجمة الإجابة من الإنجليزية إلى العربية
132
- translated_answer = translate_en_to_ar(best_result['answer'])[0]['translation_text']
133
- return f"🔍 {translated_answer}"
134
- except Exception as e:
135
- return f"⛔ خطأ تقني: {str(e)}"
136
 
137
  def text_to_speech(text):
138
  if not text.strip():
@@ -253,4 +230,4 @@ audio {{
253
  clear_answer_btn.click(lambda: "", outputs=answer_output)
254
 
255
  if __name__ == "__main__":
256
- demo.launch()
 
22
  bart_model = AutoModelForSeq2SeqLM.from_pretrained("ahmedabdo/arabic-summarizer-bart")
23
  bart_tokenizer = AutoTokenizer.from_pretrained("ahmedabdo/arabic-summarizer-bart")
24
 
25
+ # تحميل نموذج الإجابة بالعربية
26
+ qa_model_name = "ZeyadAhmed/AraElectra-Arabic-SQuADv2-QA"
27
+ qa_pipeline = pipeline("question-answering", model=qa_model_name, tokenizer=qa_model_name, device=0 if device == "cuda" else -1)
 
 
 
 
 
 
 
 
 
 
28
 
29
  # ======================
30
  # 3. التخصيصات
31
  # ======================
32
  PRIMARY_COLOR = "#2A4D6E" # اللون الكحلي
33
+ EXAMPLE_AUDIO_PATH = "_⁨ما_هو_الذكاء_الإصطناعي_؟_فِهموجرافيك_١⁩.m4a"
34
 
35
  # ======================
36
+ # 4. الدوال المساعدة
37
  # ======================
38
  def clean_text_advanced(text):
39
  """تنظيف النص مع الحفاظ على الهيكل الأساسي"""
 
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():
 
230
  clear_answer_btn.click(lambda: "", outputs=answer_output)
231
 
232
  if __name__ == "__main__":
233
+ demo.launch()