Spaces:
Sleeping

File size: 5,076 Bytes
fb63b91
 
 
 
 
 
 
a421b43
 
fb63b91
a421b43
 
 
 
 
 
 
 
fb63b91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a421b43
fb63b91
a421b43
fb63b91
 
 
 
 
 
 
a421b43
 
fb63b91
a421b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb63b91
a421b43
 
 
 
 
 
 
 
 
 
 
fb63b91
 
a421b43
 
 
 
 
 
 
 
 
 
 
 
 
 
fb63b91
a421b43
 
fb63b91
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gradio as gr
import fitz
from docx import Document
from pptx import Presentation
from transformers import pipeline
import os, shutil, zipfile

# 🌟 أخذ الـ Hugging Face Token من Secrets
HF_TOKEN = os.environ.get("HF_TOKEN")

# نموذج لتوليد الشرح
generator = pipeline(
    "text-generation",
    model="tiiuae/falcon-7b-instruct",
    use_auth_token=HF_TOKEN
)

# 📝 استخراج النصوص
def extract_text(file_path):
    text_blocks = []
    if file_path.endswith('.pdf'):
        doc = fitz.open(file_path)
        for page in doc:
            text_blocks.append(page.get_text())
    elif file_path.endswith('.docx'):
        doc = Document(file_path)
        for para in doc.paragraphs:
            text_blocks.append(para.text)
    elif file_path.endswith('.pptx'):
        prs = Presentation(file_path)
        for slide in prs.slides:
            for shape in slide.shapes:
                if hasattr(shape, "text") and shape.text.strip():
                    text_blocks.append(shape.text)
    return [t for t in text_blocks if t.strip()]

# 💡 توليد شرح أكاديمي
def generate_explanation(text):
    result = generator(f"اشرح الفقرة التالية شرحًا أكاديميًا عمليًا: {text}", max_length=300)
    return result[0]['generated_text']

# 📝 توليد كويز ديناميكي
def generate_quiz(text, q_num):
    return f"السؤال {q_num}: ما الفكرة الأساسية للفقرة؟\n1. خيار أ\n2. خيار ب\n3. خيار ج\nالإجابة الصحيحة: 1"

# 🌐 معالجة الملف وإنشاء Tabs HTML + ZIP
def process_file(file):
    file_path = file.name
    content = extract_text(file_path)
    tabs_html = []
    tab_size = 5
    tabs = [content[i:i+tab_size] for i in range(0,len(content),tab_size)]
    
    for i, tab in enumerate(tabs,1):
        html = f'<div id="tab{i}" class="tab">\n'
        html += f'<h3><img src="https://undraw.co/api/illustrations/undraw_education_f8ru.svg" width="40"> Tab {i}</h3>\n'
        for j, para in enumerate(tab,1):
            explanation = generate_explanation(para)
            html += f'<p class="ar">{para}</p>\n<p class="en">{explanation}</p>\n'
            html += f'<p>{generate_quiz(para,j)}</p>\n'
        html += '</div>'
        tabs_html.append(html)

    # إنشاء مجلد المشروع
    os.makedirs("project", exist_ok=True)
    
    # إنشاء ملف CSS
    css_content = """
    body { font-family: 'Cairo', sans-serif; background-color:#f5f7fa; color:#333; padding:20px; }
    .tab { display:none; margin-bottom:40px; padding:20px; background:#e3f2fd; border-radius:10px; animation: fadein 1s; }
    .ar { text-align:right; font-family:'Cairo', sans-serif; margin:10px 0; }
    .en { text-align:left; font-family:'Open Sans', sans-serif; margin:5px 0 15px 0; color:#555; }
    .tab-buttons { margin-bottom:20px; }
    .tab-buttons button { margin-right:5px; padding:10px 15px; border:none; border-radius:5px; background:#90caf9; color:#fff; cursor:pointer; }
    .tab-buttons button.active { background:#42a5f5; }
    @keyframes fadein { from {opacity:0;} to {opacity:1;} }
    """
    with open("project/style.css","w", encoding="utf-8") as f:
        f.write(css_content)

    # إنشاء ملف HTML
    with open("project/index.html","w", encoding='utf-8') as f:
        f.write("""<html><head>
        <meta charset='utf-8'>
        <title>Academic Project</title>
        <link href="https://fonts.googleapis.com/css2?family=Cairo&family=Open+Sans&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="style.css">
        </head><body>
        <div class="tab-buttons">
        """)
        for i in range(1,len(tabs)+1):
            f.write(f'<button onclick="showTab({i})" id="btn{i}">Tab {i}</button>')
        f.write("</div>\n")
        for t in tabs_html:
            f.write(t)
        
        # سكريبت للتنقل بين Tabs
        f.write("""
        <script>
        function showTab(n){
            var tabs = document.getElementsByClassName('tab');
            var buttons = document.querySelectorAll('.tab-buttons button');
            for(var i=0;i<tabs.length;i++){ tabs[i].style.display='none'; buttons[i].classList.remove('active'); }
            document.getElementById('tab'+n).style.display='block';
            document.getElementById('btn'+n).classList.add('active');
        }
        showTab(1); // عرض Tab الأول افتراضياً
        </script>
        """)
        f.write("</body></html>")

    # ضغط المشروع
    shutil.make_archive("academic_project","zip","project")
    return "academic_project.zip"

# 🔧 واجهة Gradio
iface = gr.Interface(
    fn=process_file,
    inputs=gr.File(file_types=[".pdf",".docx",".pptx"]),
    outputs=gr.File(label="Download Project ZIP"),
    title="Academic Interactive Project Generator",
    description="ارفع ملف PDF أو Word أو PPT وسيتم توليد مشروع HTML تفاعلي مع Tabs، شروح، وكويز."
)

iface.launch()