Spaces:
Running
Running
File size: 5,470 Bytes
bef155a 3dc74fc 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d bef155a 08fcc8d 3dc74fc 08fcc8d bef155a 2ceeff6 08fcc8d e520d07 08fcc8d 46d6488 08fcc8d bfe5fed 58918fd ec0ee8d f1fb0b3 e99d163 baa88e2 6fc6258 b755808 2611745 3654aae 2611745 bfc2e76 3654aae 77663f7 2611745 a2b41af 2414efa 7fe699e 31e6bfe 2414efa de8c14f 615b50c 2611745 2414efa a2b41af baa88e2 58e1ccf b3ffb7d f1fb0b3 fb7a309 2d374b1 72d3fc8 3ab6b75 d432926 08fcc8d 44f9042 1751b9e efd5596 1f9a382 57c8d80 914ce5e 71a8fd3 2ceeff6 44f9042 08fcc8d 554f4be |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
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
# write summarizer alert function
def sumAlert():
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'>
""")
gr.HTML("""
<style>
.prompt-title {
font-family: "JetBrains Mono",monospace;
color: "white";
}
.prompt-popup {
background: #646566;
color:white;
border-radius:15px;
}
</style>
<div style="text-align:center;">
<button onclick=infoAlert()
style="padding: 15px; background-color: #646566; font-family: 'JetBrains Mono',monospace; color: white; border: none; border-radius: 15px; cursor: pointer;">
Info
</button>
<script>
function infoAlert(){
Swal.fire({
text: "Summarizing. . .",
confirmButtonColor: "black",
position: "bottom-right",
customClass: {
title: "prompt-title",
popup: "prompt-popup"
},
timer: 7000
});
}
</script>
</div>
""")
with gr.Blocks(
theme="upsatwal/mlsc_tiet",
analytics_enabled=True,
fill_height=True,
css="footer {display: none !important;}"
) as app:
gr.HTML("<div style='text-align:center;overflow:hidden;'><h2>📓 PDF Summarizer 📓</h2></div>")
with gr.Sidebar():
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("""
<style>
.prompt-title {
font-family: "JetBrains Mono",monospace;
color: "white";
}
.prompt-popup {
background: #646566;
color:white;
border-radius:15px;
}
</style>
<div style="text-align:center;">
<button onclick=infoAlert()
style="padding: 15px; background-color: #646566; font-family: 'JetBrains Mono',monospace; color: white; border: none; border-radius: 15px; cursor: pointer;">
Info
</button>
<script>
function infoAlert(){
Swal.fire({
title: "How To Use",
text: "Enter your Gemini API key, upload PDF document, select summary kind, then hit the summarize button",
confirmButtonColor: "black",
customClass: {
title: "prompt-title",
popup: "prompt-popup"
},
timer: 7000
});
}
</script>
</div>
""")
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)
btn.click(fn=sumAlert,inputs="",outputs="")
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)
|