Mohammed Ali Taher Mohammed commited on
Commit
815a2fa
·
verified ·
1 Parent(s): 4fa8371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -1,30 +1,26 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  from gtts import gTTS
4
  import os
5
 
6
- # 1. تحميل النموذج والـ Tokenizer بشكل صريح ومستقل لضمان التوافق الكامل
7
  print("Loading model and tokenizer...")
8
  model_name = "facebook/bart-large-cnn"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
 
12
- # بناء الـ pipeline بتمرير النموذج والـ Tokenizer مباشرة
13
- summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
14
-
15
  def process_text(text):
16
  if not text.strip():
17
  return "الرجاء إدخال نص صالح للتلخيص.", None
18
 
19
- input_length = len(text.split())
20
- max_len = min(150, int(input_length * 0.5))
21
- min_len = min(50, int(input_length * 0.2))
22
 
23
- # توليد التلخيص
24
- summary_result = summarizer(text, max_length=max_len, min_length=min_len, do_sample=False)
25
- summary_text = summary_result[0]['summary_text']
26
 
27
- # تحويل التلخيص النصي إلى ملف صوتي
28
  tts = gTTS(text=summary_text, lang='en')
29
  audio_path = "summary_audio.mp3"
30
  tts.save(audio_path)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  from gtts import gTTS
4
  import os
5
 
6
+ # 1. تحميل النموذج والـ Tokenizer بشكل مباشر
7
  print("Loading model and tokenizer...")
8
  model_name = "facebook/bart-large-cnn"
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
11
 
 
 
 
12
  def process_text(text):
13
  if not text.strip():
14
  return "الرجاء إدخال نص صالح للتلخيص.", None
15
 
16
+ # 2. تحويل النص إلى تنسيق يفهمه النموذج (Tokenization)
17
+ inputs = tokenizer([text], max_length=1024, return_tensors="pt", truncation=True)
 
18
 
19
+ # 3. توليد التلخيص مباشرة من النموذج بدون استخدام pipeline
20
+ summary_ids = model.generate(inputs["input_ids"], num_beams=4, max_length=150, min_length=40, early_stopping=True)
21
+ summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
22
 
23
+ # 4. تحويل التلخيص النصي إلى ملف صوتي
24
  tts = gTTS(text=summary_text, lang='en')
25
  audio_path = "summary_audio.mp3"
26
  tts.save(audio_path)