Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +60 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import whisper
|
| 4 |
+
from transformers import MT5Tokenizer, AutoModelForSeq2SeqLM
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
| 6 |
+
import graphviz
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# تحويل الصوت إلى نص
|
| 10 |
+
model_whisper = whisper.load_model("base")
|
| 11 |
+
|
| 12 |
+
# تلخيص النص
|
| 13 |
+
tokenizer = MT5Tokenizer.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")
|
| 14 |
+
model_summarizer = AutoModelForSeq2SeqLM.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")
|
| 15 |
+
|
| 16 |
+
# تحميل نموذج AraGPT2 العربي الخفيف
|
| 17 |
+
aragpt_tokenizer = AutoTokenizer.from_pretrained("aubmindlab/aragpt2-base")
|
| 18 |
+
aragpt_model = AutoModelWithLMHead.from_pretrained("aubmindlab/aragpt2-base")
|
| 19 |
+
aragpt_model.eval()
|
| 20 |
+
|
| 21 |
+
def generate_arabic_explanation(prompt):
|
| 22 |
+
inputs = aragpt_tokenizer(prompt, return_tensors="pt")
|
| 23 |
+
outputs = aragpt_model.generate(**inputs, max_new_tokens=120, do_sample=True, temperature=0.8)
|
| 24 |
+
return aragpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
def transcribe_summarize_answer(audio_path):
|
| 27 |
+
result = model_whisper.transcribe(audio_path)
|
| 28 |
+
text = result["text"]
|
| 29 |
+
|
| 30 |
+
inputs = tokenizer("summarize: " + text, return_tensors="pt", max_length=512, truncation=True)
|
| 31 |
+
summary_ids = model_summarizer.generate(inputs["input_ids"], max_length=150, min_length=40, length_penalty=2.0, num_beams=4)
|
| 32 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
dot = graphviz.Digraph(comment="Mind Map", format='png')
|
| 35 |
+
dot.node('central', '🧠 فكرة الدرس')
|
| 36 |
+
for i, sentence in enumerate(summary.split(".")):
|
| 37 |
+
if sentence.strip():
|
| 38 |
+
node_id = f"n{i}"
|
| 39 |
+
dot.node(node_id, sentence.strip())
|
| 40 |
+
dot.edge('central', node_id)
|
| 41 |
+
mindmap_path = "/tmp/mindmap"
|
| 42 |
+
dot.render(mindmap_path, cleanup=True)
|
| 43 |
+
|
| 44 |
+
prompt = summary + "\n\nاشرح هذه الأفكار بشكل مبسط:"
|
| 45 |
+
answer = generate_arabic_explanation(prompt)
|
| 46 |
+
|
| 47 |
+
return text, summary, mindmap_path + ".png", answer
|
| 48 |
+
|
| 49 |
+
gr.Interface(
|
| 50 |
+
fn=transcribe_summarize_answer,
|
| 51 |
+
inputs=gr.Audio(source="upload", type="filepath", label="🎙️ ارفع ملف صوتي للدرس"),
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.Textbox(label="📜 النص الكامل"),
|
| 54 |
+
gr.Textbox(label="✂️ الملخص"),
|
| 55 |
+
gr.Image(label="🧠 الخريطة الذهنية المرئية"),
|
| 56 |
+
gr.Textbox(label="🤖 شرح AraGPT2 باللغة العربية")
|
| 57 |
+
],
|
| 58 |
+
title="SmartLessonMap + AraGPT2 🤖🧠",
|
| 59 |
+
description="تطبيق ذكي يحول الدروس الصوتية إلى نص، ملخص، خريطة ذهنية، وشرح باللغة العربية باستخدام نموذج خفيف."
|
| 60 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
openai-whisper
|
| 5 |
+
sentencepiece
|
| 6 |
+
graphviz
|