|
import google.generativeai as genai |
|
import pdfplumber |
|
import gradio as gr |
|
import os |
|
from dotenv import python_dotenv |
|
|
|
python_dotenv() |
|
|
|
|
|
GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY") |
|
genai.configure(api_key=GOOGLE_API_KEY) |
|
|
|
|
|
model = genai.GenerativeModel('gemini-pro') |
|
|
|
def extract_text_from_pdf(pdf_file): |
|
text = "" |
|
with pdfplumber.open(pdf_file) as pdf: |
|
for page in pdf.pages: |
|
page_text = page.extract_text() |
|
if page_text: |
|
text += page_text |
|
return text |
|
|
|
def summarize_pdf(pdf_file): |
|
text = extract_text_from_pdf(pdf_file) |
|
if not text.strip(): |
|
return "No extractable text found in the PDF." |
|
|
|
|
|
text = text[:15000] |
|
|
|
prompt = f"Summarize the following PDF content:\n\n{text}" |
|
|
|
try: |
|
response = model.generate_content(prompt) |
|
return response.text.strip() |
|
except Exception as e: |
|
return f"Error during summarization: {e}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=summarize_pdf, |
|
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]), |
|
outputs="text", |
|
title="PDF Summarizer with Gemini", |
|
description="Upload a PDF and get a summary using Google's Gemini Pro model." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|