File size: 3,035 Bytes
17e4cef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
import graphviz
from PIL import Image

# تحميل موديل التلخيص
model_name = "csebuetnlp/mT5_multilingual_XLSum"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

# تحميل موديل توليد الأسئلة
question_generator = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")

# دالة تلخيص النص
def summarize_text(text, src_lang):
    inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
    summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary

# دالة توليد الأسئلة
def generate_questions(summary):
    questions = question_generator(summary, max_length=64, num_return_sequences=5)
    return [q['generated_text'] for q in questions]

# دالة توليد خريطة مفاهيم
def generate_concept_map(summary, questions):
    dot = graphviz.Digraph(comment='Concept Map')
    dot.node('A', summary)
    for i, question in enumerate(questions):
        dot.node(f'Q{i}', question)
        dot.edge('A', f'Q{i}')
    dot.render('concept_map', format='png')
    return Image.open('concept_map.png')

# دالة التحليل الكامل
def analyze_text(text, lang):
    summary = summarize_text(text, lang)
    questions = generate_questions(summary)
    concept_map_image = generate_concept_map(summary, questions)
    return summary, questions, concept_map_image

# أمثلة للنصوص
examples = [
    ["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
    ["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"]
]

# واجهة Gradio
iface = gr.Interface(
    fn=analyze_text,
    inputs=[gr.Textbox(lines=10, placeholder="Enter text here........"), gr.Dropdown(["ar", "en"], label="Language")],
    outputs=[gr.Textbox(label="Summary"), gr.Textbox(label="Questions"), gr.Image(label="Concept Map")],
    examples=examples,
    title="AI Study Assistant",
    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."
)

# تشغيل التطبيق
if __name__ == "__main__":
    iface.launch()