Maram-almasary commited on
Commit
3ae2319
·
verified ·
1 Parent(s): ead7e95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -9
app.py CHANGED
@@ -3,27 +3,22 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
3
  import graphviz
4
  from PIL import Image
5
 
6
- # تحميل موديل التلخيص
7
  model_name = "csebuetnlp/mT5_multilingual_XLSum"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
10
 
11
- # تحميل موديل توليد الأسئلة
12
  question_generator = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
13
 
14
- # دالة تلخيص النص
15
  def summarize_text(text, src_lang):
16
  inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
17
  summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
18
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
19
  return summary
20
 
21
- # دالة توليد الأسئلة
22
  def generate_questions(summary):
23
  questions = question_generator(summary, max_length=64, num_return_sequences=5)
24
  return [q['generated_text'] for q in questions]
25
 
26
- # دالة توليد خريطة مفاهيم
27
  def generate_concept_map(summary, questions):
28
  dot = graphviz.Digraph(comment='Concept Map')
29
  dot.node('A', summary)
@@ -33,20 +28,18 @@ def generate_concept_map(summary, questions):
33
  dot.render('concept_map', format='png')
34
  return Image.open('concept_map.png')
35
 
36
- # دالة التحليل الكامل
37
  def analyze_text(text, lang):
38
  summary = summarize_text(text, lang)
39
  questions = generate_questions(summary)
40
  concept_map_image = generate_concept_map(summary, questions)
41
  return summary, questions, concept_map_image
42
 
43
- # أمثلة للنصوص
44
  examples = [
45
  ["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
46
  ["Artificial intelligence is a branch of computer science that aims to create intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include: Speech recognition, learning, planning, and problem-solving.", "en"]
47
  ]
48
 
49
- # واجهة Gradio
50
  iface = gr.Interface(
51
  fn=analyze_text,
52
  inputs=[gr.Textbox(lines=10, placeholder="Enter text here........"), gr.Dropdown(["ar", "en"], label="Language")],
@@ -56,6 +49,5 @@ iface = gr.Interface(
56
  description="Enter a text in Arabic or English and the model will summarize it and generate various questions about it in addition to generating a concept map, or you can choose one of the examples."
57
  )
58
 
59
- # تشغيل التطبيق
60
  if __name__ == "__main__":
61
  iface.launch()
 
3
  import graphviz
4
  from PIL import Image
5
 
 
6
  model_name = "csebuetnlp/mT5_multilingual_XLSum"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
 
10
  question_generator = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
11
 
 
12
  def summarize_text(text, src_lang):
13
  inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
14
  summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
15
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
16
  return summary
17
 
 
18
  def generate_questions(summary):
19
  questions = question_generator(summary, max_length=64, num_return_sequences=5)
20
  return [q['generated_text'] for q in questions]
21
 
 
22
  def generate_concept_map(summary, questions):
23
  dot = graphviz.Digraph(comment='Concept Map')
24
  dot.node('A', summary)
 
28
  dot.render('concept_map', format='png')
29
  return Image.open('concept_map.png')
30
 
 
31
  def analyze_text(text, lang):
32
  summary = summarize_text(text, lang)
33
  questions = generate_questions(summary)
34
  concept_map_image = generate_concept_map(summary, questions)
35
  return summary, questions, concept_map_image
36
 
 
37
  examples = [
38
  ["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
39
  ["Artificial intelligence is a branch of computer science that aims to create intelligent machines that work and react like humans. Some of the activities computers with artificial intelligence are designed for include: Speech recognition, learning, planning, and problem-solving.", "en"]
40
  ]
41
 
42
+
43
  iface = gr.Interface(
44
  fn=analyze_text,
45
  inputs=[gr.Textbox(lines=10, placeholder="Enter text here........"), gr.Dropdown(["ar", "en"], label="Language")],
 
49
  description="Enter a text in Arabic or English and the model will summarize it and generate various questions about it in addition to generating a concept map, or you can choose one of the examples."
50
  )
51
 
 
52
  if __name__ == "__main__":
53
  iface.launch()