File size: 2,159 Bytes
8071a04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()