Download app.py from Bombog944/Admin: direct link, hf CLI and curl.
- Browser
- Download file 5.08 kB
-
https://huggingface.co/spaces/Bombog944/Admin/resolve/main/app.py
- Command line
-
hf download hf://spaces/Bombog944/Admin/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Bombog944/Admin/resolve/main/app.py
5.08 kB
| 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() |