Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install gradio transformers torch graphviz pillow
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
4 |
+
import graphviz
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# تحميل موديل التلخيص
|
8 |
+
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# تحميل موديل توليد الأسئلة
|
13 |
+
question_generator = pipeline("text2text-generation", model="valhalla/t5-small-e2e-qg")
|
14 |
+
|
15 |
+
# دالة تلخيص النص
|
16 |
+
def summarize_text(text, src_lang):
|
17 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True)
|
18 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
19 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
20 |
+
return summary
|
21 |
+
|
22 |
+
# دالة توليد الأسئلة
|
23 |
+
def generate_questions(summary):
|
24 |
+
questions = question_generator(summary, max_length=64, num_return_sequences=5)
|
25 |
+
return [q['generated_text'] for q in questions]
|
26 |
+
|
27 |
+
# دالة توليد خريطة مفاهيم
|
28 |
+
def generate_concept_map(summary, questions):
|
29 |
+
dot = graphviz.Digraph(comment='Concept Map')
|
30 |
+
dot.node('A', summary)
|
31 |
+
for i, question in enumerate(questions):
|
32 |
+
dot.node(f'Q{i}', question)
|
33 |
+
dot.edge('A', f'Q{i}')
|
34 |
+
dot.render('concept_map', format='png')
|
35 |
+
return Image.open('concept_map.png')
|
36 |
+
|
37 |
+
# دالة التحليل الكامل
|
38 |
+
def analyze_text(text, lang):
|
39 |
+
summary = summarize_text(text, lang)
|
40 |
+
questions = generate_questions(summary)
|
41 |
+
concept_map_image = generate_concept_map(summary, questions)
|
42 |
+
return summary, questions, concept_map_image
|
43 |
+
|
44 |
+
# أمثلة للنصوص
|
45 |
+
examples = [
|
46 |
+
["الذكاء الاصطناعي هو فرع من علوم الكمبيوتر يهدف إلى إنشاء آلات ذكية تعمل وتتفاعل مثل البشر. بعض الأنشطة التي صممت أجهزة الكمبيوتر الذكية للقيام بها تشمل: التعرف على الصوت، التعلم، التخطيط، وحل المشاكل.", "ar"],
|
47 |
+
["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"]
|
48 |
+
]
|
49 |
+
|
50 |
+
# واجهة Gradio
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=analyze_text,
|
53 |
+
inputs=[gr.Textbox(lines=10, placeholder="Enter text here........"), gr.Dropdown(["ar", "en"], label="Language")],
|
54 |
+
outputs=[gr.Textbox(label="Summary"), gr.Textbox(label="Questions"), gr.Image(label="Concept Map")],
|
55 |
+
examples=examples,
|
56 |
+
title="AI Study Assistant",
|
57 |
+
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."
|
58 |
+
)
|
59 |
+
|
60 |
+
# تشغيل التطبيق
|
61 |
+
if __name__ == "__main__":
|
62 |
+
iface.launch()
|