Spaces:
Running
Running
| import google.generativeai as genai | |
| import gradio as gr | |
| import os | |
| import fitz # PyMuPDF | |
| # 🔑 Configure Gemini API | |
| api_key = os.getenv("YOUR_GEMINI_API_KEY") | |
| genai.configure(api_key=api_key) | |
| # اختر موديل Gemini | |
| model = genai.GenerativeModel("gemini-1.5-flash") | |
| # ---- Helper: استخراج النصوص من PDF ---- | |
| def extract_text_from_pdf(pdf_file): | |
| text = "" | |
| doc = fitz.open(pdf_file.name) | |
| for page in doc: | |
| text += page.get_text("text") + "\n" | |
| return text.strip() | |
| # ---- Core function: Summarize with Gemini ---- | |
| def summarise_pdf(pdf_file): | |
| if pdf_file is None: | |
| return "⚠️ Please upload a PDF file." | |
| # 1. استخراج النص | |
| pdf_text = extract_text_from_pdf(pdf_file) | |
| # 2. تجهيز البرومبت | |
| prompt = f""" | |
| You are NotebookLM Quick Synthesiser inside Google AI Lab. | |
| The user uploaded a subsidy-regulation document. | |
| Please summarise **the key rules and important points** clearly. | |
| Text of document: | |
| {pdf_text[:5000]} | |
| Format the summary as **Markdown** with: | |
| - Main Rules | |
| - Obligations | |
| - Exceptions / Notes | |
| """ | |
| # 3. استدعاء Gemini | |
| response = model.generate_content(prompt) | |
| summary = response.text.strip() | |
| return summary | |
| # ---- Gradio UI ---- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 📝 Micro-Lab – NotebookLM Quick Synthesiser") | |
| gr.Markdown("Upload a subsidy-regulation PDF → Gemini will summarise key rules in Markdown.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| pdf_input = gr.File(label="📂 Upload PDF", file_types=[".pdf"]) | |
| summarise_btn = gr.Button("⚡ Summarise with NotebookLM") | |
| with gr.Column(): | |
| output_md = gr.Markdown(value="⬅️ Upload a PDF and click Summarise.", elem_id="output-area") | |
| # ✅ نعرض رسالة loading قبل التنفيذ | |
| summarise_btn.click( | |
| fn=lambda x: "⏳ Summarising... please wait", | |
| inputs=pdf_input, | |
| outputs=output_md | |
| ).then( | |
| summarise_pdf, | |
| inputs=pdf_input, | |
| outputs=output_md | |
| ) | |
| demo.launch() | |