Spaces:
Running
Running
import google.generativeai as genai | |
import gradio as gr | |
from pypdf import PdfReader | |
def pdfSummarizer(gemini_api_key, pdf_file, kind): | |
gemini_api_key = gemini_api_key | |
genai.configure(api_key = gemini_api_key) | |
model = genai.GenerativeModel("gemini-1.5-pro") | |
pdf_file = PdfReader(pdf_file) | |
pdf_text = "" | |
for page in pdf_file.pages: | |
pdf_text += page.extract_text() | |
if kind == "5 Bullet Points": | |
response = model.generate_content([pdf_text, "can you summarize this document in 5 bullet points? Use bullet points and not asterisks"]) | |
elif kind == "10 Bullet Points": | |
response = model.generate_content([pdf_text, "can you summarize this document in 10 bullet points? Use bullet points and not asterisks"]) | |
elif kind == "Paragraph": | |
response = model.generate_content([pdf_text, "can you summarize this document as a paragraph?"]) | |
elif kind == "Sentence": | |
response = model.generate_content([pdf_text, "can you summarize this document in one sentence?"]) | |
return response.text | |
with gr.Blocks( | |
theme="gstaff/whiteboard", | |
analytics_enabled=True, | |
fill_height=True | |
) as app: | |
gr.HTML("<div style='text-align:center;overflow:hidden;'><h2>📓 PDF Summarizer 📓</h2></div>") | |
with gr.Sidebar(visible=False): | |
gr.HTML("<div style='text-align:center;overflow:hidden;'><h3>📓 PDF Summarizer 📓</h3></div>") | |
gr.HTML("<br>") | |
# load sweet alert module from cdn | |
gr.HTML(""" | |
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js'></script> | |
<link href='https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.min.css' rel='stylesheet'> | |
""") | |
# Button with Javascript Event | |
gr.HTML(""" | |
<button class = 'button-27' role = 'button' onclick="Swal.fire({ | |
title: 'How To Use', | |
text: 'Enter your Gemini API key, upload PDF document, select summary kind, then hit the summarize button', | |
timer: 5000});" | |
style= | |
.button-27 { | |
appearance: none; | |
background-color: #000000; | |
border: 2px solid #1A1A1A; | |
border-radius: 15px; | |
box-sizing: border-box; | |
color: #FFFFFF; | |
cursor: pointer; | |
display: inline-block; | |
font-family: Roobert,-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"; | |
font-size: 16px; | |
font-weight: 600; | |
line-height: normal; | |
margin: 0; | |
min-height: 60px; | |
min-width: 0; | |
outline: none; | |
padding: 16px 24px; | |
text-align: center; | |
text-decoration: none; | |
transition: all 300ms cubic-bezier(.23, 1, 0.32, 1); | |
user-select: none; | |
-webkit-user-select: none; | |
touch-action: manipulation; | |
width: 100%; | |
will-change: transform; | |
} | |
.button-27:disabled { | |
pointer-events: none; | |
} | |
.button-27:hover { | |
box-shadow: rgba(0, 0, 0, 0.25) 0 8px 15px; | |
transform: translateY(-2px); | |
} | |
.button-27:active { | |
box-shadow: none; | |
transform: translateY(0); | |
> | |
Info | |
</button> | |
""") | |
gr.HTML("<div style='text-align:center;overflow:hidden;'><h4>Gemini API Key</h4></div>") | |
api_key = gr.Text(label = "",placeholder="Enter your Google Gemini API key here") | |
with gr.Row(): | |
pdf_input = gr.File(file_types=[".pdf"]) | |
summary_type = gr.Radio(["5 Bullet Points","10 Bullet Points", "Paragraph", "Sentence"],value = "5 Bullet Points", label="Select Summary Kind") | |
with gr.Row(): | |
btn = gr.Button("Summarize") | |
clear_btn = gr.ClearButton(value="Clear") | |
with gr.Column(): | |
gr.HTML("<div style='text-align:center;overflow:hidden;'><h2>Summary Output</h2></div>") | |
out = gr.Markdown() | |
btn.click(fn=pdfSummarizer, inputs=[api_key, pdf_input, summary_type], outputs=out) | |
clear_btn.click(lambda: [None,None,"5 Bullet Points",""],inputs=None,outputs=[api_key, pdf_input, summary_type, out],queue=False) | |
app.launch(pwa=True) | |