Sadem-12 commited on
Commit
f780317
·
verified ·
1 Parent(s): 93b0683

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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)
30
+ for i, question in enumerate(questions):
31
+ dot.node(f'Q{i}', question)
32
+ dot.edge('A', f'Q{i}')
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")],
53
+ outputs=[gr.Textbox(label="Summary"), gr.Textbox(label="Questions"), gr.Image(label="Concept Map")],
54
+ examples=examples,
55
+ title="AI Study Assistant",
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()