Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
import pdfplumber
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
from dotenv import python_dotenv
|
6 |
+
|
7 |
+
python_dotenv()
|
8 |
+
|
9 |
+
# Replace with your API key
|
10 |
+
GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY")
|
11 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
12 |
+
|
13 |
+
# Load Gemini Pro model
|
14 |
+
model = genai.GenerativeModel('gemini-pro')
|
15 |
+
|
16 |
+
def extract_text_from_pdf(pdf_file):
|
17 |
+
text = ""
|
18 |
+
with pdfplumber.open(pdf_file) as pdf:
|
19 |
+
for page in pdf.pages:
|
20 |
+
page_text = page.extract_text()
|
21 |
+
if page_text:
|
22 |
+
text += page_text
|
23 |
+
return text
|
24 |
+
|
25 |
+
def summarize_pdf(pdf_file):
|
26 |
+
text = extract_text_from_pdf(pdf_file)
|
27 |
+
if not text.strip():
|
28 |
+
return "No extractable text found in the PDF."
|
29 |
+
|
30 |
+
# Limit text size if needed (Gemini handles long input, but it's safer)
|
31 |
+
text = text[:15000]
|
32 |
+
|
33 |
+
prompt = f"Summarize the following PDF content:\n\n{text}"
|
34 |
+
|
35 |
+
try:
|
36 |
+
response = model.generate_content(prompt)
|
37 |
+
return response.text.strip()
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error during summarization: {e}"
|
40 |
+
|
41 |
+
# Gradio interface
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=summarize_pdf,
|
44 |
+
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|
45 |
+
outputs="text",
|
46 |
+
title="PDF Summarizer with Gemini",
|
47 |
+
description="Upload a PDF and get a summary using Google's Gemini Pro model."
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
iface.launch()
|