-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
187 lines (157 loc) · 6.45 KB
/
Copy pathapp.py
File metadata and controls
187 lines (157 loc) · 6.45 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import tempfile
import streamlit as st
import markdown
import platform
from PyPDF2 import PdfReader
from dotenv import load_dotenv
import google.generativeai as genai
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
# Load environment variables
load_dotenv()
# Retrieve API key from environment variables
API_KEY = os.getenv("GENAI_API_KEY")
# Check if API key is loaded correctly
if API_KEY is None or API_KEY.strip() == "":
st.error("⚠️ API key not found in .env file. Please add GENAI_API_KEY to your .env file.")
else:
# Setup Gemini API
genai.configure(api_key=API_KEY)
# Streamlit page config
st.set_page_config(page_title="Gemini PDF Editor", layout="wide")
# Sidebar Info
st.sidebar.title("ℹ️ App Info")
st.sidebar.markdown("**Model**: `Gemini 2.0 Flash` via Google Generative AI API")
st.sidebar.markdown("**Powered by**: Streamlit, PyPDF2, ReportLab, Gemini 2.0 Flash")
st.title("📄 AI PDF Editor using Gemini 2.0 Flash ✨")
st.markdown("Upload a PDF ➡️ Provide a prompt ➡️ Generate and download an AI-edited PDF.")
# Function: Extract text from uploaded PDF
def extract_text_from_pdf(pdf_file):
pdf_reader = PdfReader(pdf_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() or ""
return text.strip()
# Function: Edit content using Gemini 2.0 Flash API
def edit_with_gemini(text, prompt):
try:
inputs = f"{prompt}: {text}"
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(inputs)
if not response or not hasattr(response, 'text'):
return None, "❌ No response or empty content from Gemini API."
return response.text, None
except Exception as e:
st.write("Exception details:", str(e))
return None, f"⚠️ Exception: {str(e)}"
# Function: Convert Markdown to PDF using ReportLab
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Preformatted
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
import markdown
import re
def markdown_to_pdf(markdown_text, output_path):
try:
# Split markdown manually by line for better formatting control
lines = markdown_text.split('\n')
doc = SimpleDocTemplate(output_path, pagesize=letter)
styles = getSampleStyleSheet()
# Add custom styles
styles.add(ParagraphStyle(
name='CustomBody',
fontName='Helvetica',
fontSize=12,
leading=14,
spaceBefore=6,
spaceAfter=6
))
styles.add(ParagraphStyle(
name='CodeBlock',
fontName='Courier',
fontSize=10,
leading=12,
backColor=colors.whitesmoke,
leftIndent=10,
rightIndent=10,
spaceBefore=6,
spaceAfter=6
))
story = []
code_block = []
inside_code = False
for line in lines:
if line.strip().startswith("```"):
# Toggle code block mode
if inside_code:
# End code block
code = "\n".join(code_block)
story.append(Preformatted(code, styles['CodeBlock']))
story.append(Spacer(1, 12))
code_block = []
inside_code = False
else:
inside_code = True
elif inside_code:
code_block.append(line)
elif line.strip():
story.append(Paragraph(line.strip(), styles['CustomBody']))
story.append(Spacer(1, 12))
doc.build(story)
return True, None
except Exception as e:
return False, f"❌ PDF conversion error: {str(e)}"
# Function: Check if API key is configured
def is_api_key_configured():
return API_KEY is not None and API_KEY.strip() != ""
# Upload PDF
pdf_file = st.file_uploader("📤 Upload a PDF file", type=["pdf"])
if pdf_file:
extracted_text = extract_text_from_pdf(pdf_file)
st.subheader("📃 Extracted Text")
st.text_area("Text from PDF", extracted_text, height=200)
if not is_api_key_configured():
st.error("⚠️ API key not configured. Please add GENAI_API_KEY to your .env file.")
else:
st.subheader("✍️ Enter Instructions")
prompt = st.text_input("e.g., Summarize, Fix grammar, Translate to Hindi")
with st.expander("💡 Prompt Examples"):
st.markdown("""
- Summarize this document
- Fix grammatical errors
- Translate to Hindi
- Make it more formal
- Rephrase for 5th grade reading level
- Replace all instances of 'Akanksha' with 'Mohit'
""")
if st.button("✨ Edit with Gemini 2.0 Flash"):
if not prompt:
st.error("Please enter processing instructions.")
else:
with st.spinner("Generating..."):
edited_text, error = edit_with_gemini(extracted_text, prompt)
if error:
st.error(error)
elif edited_text:
st.session_state.edited_text = edited_text
st.success("✅ Successfully edited using Gemini 2.0 Flash!")
else:
st.error("❌ No text was returned from the API.")
if "edited_text" in st.session_state and st.session_state.edited_text.strip():
st.subheader("📝 Edited Output")
st.text_area("Gemini 2.0 Flash Output", st.session_state.edited_text, height=300)
if st.button("Download PDF"):
if "edited_text" in st.session_state and st.session_state.edited_text.strip():
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
output_path = tmp_pdf.name
success, error = markdown_to_pdf(st.session_state.edited_text, output_path)
if success:
with open(output_path, "rb") as f:
st.download_button("Click to Download PDF", f, file_name="edited_output.pdf")
else:
st.error(f"PDF generation failed: {error}")
else:
st.warning("⚠️ No AI-edited content available to convert. Please edit the PDF first.")