Spaces:
Sleeping
Sleeping
usernameiskheejay
commited on
Commit
·
4e31ca1
1
Parent(s):
c7130f7
Document Summarizer with Gemini
Browse files- app.py +88 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
|
| 7 |
+
genai.configure(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 8 |
+
|
| 9 |
+
generation_config = {
|
| 10 |
+
"temperature": 2,
|
| 11 |
+
"top_p": 0.95,
|
| 12 |
+
"top_k": 64,
|
| 13 |
+
"max_output_tokens": 65536,
|
| 14 |
+
"response_mime_type": "text/plain",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
model = genai.GenerativeModel(
|
| 18 |
+
model_name="gemini-2.0-flash-thinking-exp-01-21",
|
| 19 |
+
generation_config=generation_config,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def extract_text_from_pdf(file):
|
| 23 |
+
"""Extract text from an uploaded PDF file."""
|
| 24 |
+
reader = PdfReader(file)
|
| 25 |
+
text = ""
|
| 26 |
+
for page in reader.pages:
|
| 27 |
+
text += page.extract_text() + "\n"
|
| 28 |
+
return text
|
| 29 |
+
|
| 30 |
+
def stream_response(prompt):
|
| 31 |
+
"""Stream response from the generative model."""
|
| 32 |
+
chat_session = model.start_chat(
|
| 33 |
+
history=[{"role": "user", "parts": [prompt]}]
|
| 34 |
+
)
|
| 35 |
+
for response_chunk in chat_session.send_message("Processing input...", stream=True):
|
| 36 |
+
if response_chunk.text:
|
| 37 |
+
yield response_chunk.text
|
| 38 |
+
|
| 39 |
+
def create_pdf(content):
|
| 40 |
+
"""Create a well-formatted PDF file from the given content."""
|
| 41 |
+
pdf = FPDF()
|
| 42 |
+
pdf.add_page()
|
| 43 |
+
pdf.set_font("Arial", size=12)
|
| 44 |
+
lines = content.split("\n")
|
| 45 |
+
for line in lines:
|
| 46 |
+
formatted_line = line.replace("**", "") # Remove markdown-style bolding
|
| 47 |
+
pdf.multi_cell(0, 10, formatted_line)
|
| 48 |
+
return pdf
|
| 49 |
+
|
| 50 |
+
# Streamlit UI
|
| 51 |
+
st.title("Text Summarization and Insight Extraction")
|
| 52 |
+
|
| 53 |
+
# Session state to persist generated text
|
| 54 |
+
if "generated_text" not in st.session_state:
|
| 55 |
+
st.session_state.generated_text = ""
|
| 56 |
+
|
| 57 |
+
uploaded_file = st.file_uploader("Upload a document (PDF) or paste text below:", type=["pdf"])
|
| 58 |
+
input_text = st.text_area("Or paste your article here:", placeholder="Paste your article or text here...")
|
| 59 |
+
|
| 60 |
+
if st.button("Generate Summary and Insights"):
|
| 61 |
+
if uploaded_file:
|
| 62 |
+
input_text = extract_text_from_pdf(uploaded_file)
|
| 63 |
+
|
| 64 |
+
if input_text.strip():
|
| 65 |
+
st.markdown("### Generating Results...")
|
| 66 |
+
response_container = st.empty()
|
| 67 |
+
st.session_state.generated_text = "" # Clear previous text
|
| 68 |
+
for chunk in stream_response(f"Summarize the following text and extract key insights:\n{input_text}"):
|
| 69 |
+
st.session_state.generated_text += chunk
|
| 70 |
+
response_container.markdown(st.session_state.generated_text)
|
| 71 |
+
else:
|
| 72 |
+
st.warning("Please upload a file or enter text to process.")
|
| 73 |
+
|
| 74 |
+
if st.session_state.generated_text:
|
| 75 |
+
st.markdown("### Summary and Insights")
|
| 76 |
+
st.text_area("Results:", st.session_state.generated_text, height=300)
|
| 77 |
+
|
| 78 |
+
# PDF Download
|
| 79 |
+
pdf = create_pdf(st.session_state.generated_text)
|
| 80 |
+
pdf_file = "summary_insights.pdf"
|
| 81 |
+
pdf.output(pdf_file)
|
| 82 |
+
with open(pdf_file, "rb") as f:
|
| 83 |
+
st.download_button(
|
| 84 |
+
label="Download PDF",
|
| 85 |
+
data=f,
|
| 86 |
+
file_name="summary_insights.pdf",
|
| 87 |
+
mime="application/pdf"
|
| 88 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
PyPDF2
|
| 3 |
+
fpdf
|
| 4 |
+
google-generativeai
|