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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import PyPDF2
|
3 |
+
import io
|
4 |
+
import os
|
5 |
+
from groq import Groq
|
6 |
+
import tempfile
|
7 |
+
import traceback
|
8 |
+
|
9 |
+
def extract_text_from_pdf(pdf_file):
|
10 |
+
"""Extract text from uploaded PDF file"""
|
11 |
+
try:
|
12 |
+
if pdf_file is None:
|
13 |
+
return None, "No PDF file provided"
|
14 |
+
|
15 |
+
# Read the PDF file
|
16 |
+
with open(pdf_file.name, 'rb') as file:
|
17 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
18 |
+
text = ""
|
19 |
+
|
20 |
+
# Extract text from all pages
|
21 |
+
for page_num in range(len(pdf_reader.pages)):
|
22 |
+
page = pdf_reader.pages[page_num]
|
23 |
+
text += page.extract_text() + "\n"
|
24 |
+
|
25 |
+
if not text.strip():
|
26 |
+
return None, "Could not extract text from PDF. The PDF might be image-based or encrypted."
|
27 |
+
|
28 |
+
return text, None
|
29 |
+
|
30 |
+
except Exception as e:
|
31 |
+
return None, f"Error reading PDF: {str(e)}"
|
32 |
+
|
33 |
+
def summarize_with_groq(api_key, text):
|
34 |
+
"""Generate summary using Groq API"""
|
35 |
+
try:
|
36 |
+
if not api_key or not api_key.strip():
|
37 |
+
return "Please enter your Groq API key"
|
38 |
+
|
39 |
+
if not text or not text.strip():
|
40 |
+
return "No text to summarize"
|
41 |
+
|
42 |
+
# Initialize Groq client
|
43 |
+
client = Groq(api_key=api_key.strip())
|
44 |
+
|
45 |
+
# System prompt for summarization
|
46 |
+
system_prompt = """You are a highly capable language model specialized in document summarization. Your task is to read and understand the full content of a multi-page PDF document and generate a clear, accurate, and detailed summary of the entire document.
|
47 |
+
Focus on capturing all main ideas, key points, arguments, findings, and conclusions presented throughout the document. If the document is technical, legal, academic, or contains structured sections (e.g., introduction, methods, results, discussion), maintain the logical flow and structure while expressing the content in a comprehensive and accessible manner.
|
48 |
+
Avoid unnecessary simplification. Include important details, supporting evidence, and nuanced insights that reflect the depth of the original material. Do not copy the text verbatim.
|
49 |
+
Output only the summary. Do not explain your process. Use a neutral, professional, and informative tone. The summary should provide a full understanding of the document to someone who has not read it."""
|
50 |
+
|
51 |
+
# Create completion with optimal hyperparameters
|
52 |
+
completion = client.chat.completions.create(
|
53 |
+
model="llama-3.3-70b-versatile",
|
54 |
+
messages=[
|
55 |
+
{
|
56 |
+
"role": "system",
|
57 |
+
"content": system_prompt
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"role": "user",
|
61 |
+
"content": f"Please summarize the following document:\n\n{text}"
|
62 |
+
}
|
63 |
+
],
|
64 |
+
temperature=0.3, # Low randomness for factual, focused summaries
|
65 |
+
max_completion_tokens=2048, # Increased to allow longer summaries
|
66 |
+
top_p=0.9, # Allows some diversity while still grounded
|
67 |
+
stream=False,
|
68 |
+
stop=None,
|
69 |
+
)
|
70 |
+
|
71 |
+
summary = completion.choices[0].message.content
|
72 |
+
return summary
|
73 |
+
|
74 |
+
except Exception as e:
|
75 |
+
error_msg = f"Error generating summary: {str(e)}"
|
76 |
+
if "authentication" in str(e).lower() or "api" in str(e).lower():
|
77 |
+
error_msg += "\n\nPlease check your Groq API key and ensure it's valid."
|
78 |
+
return error_msg
|
79 |
+
|
80 |
+
def process_pdf_and_summarize(api_key, pdf_file, progress=gr.Progress()):
|
81 |
+
"""Main function to process PDF and generate summary"""
|
82 |
+
try:
|
83 |
+
if not api_key or not api_key.strip():
|
84 |
+
return "β Please enter your Groq API key", "", ""
|
85 |
+
|
86 |
+
if pdf_file is None:
|
87 |
+
return "β Please upload a PDF file", "", ""
|
88 |
+
|
89 |
+
progress(0.1, desc="Reading PDF file...")
|
90 |
+
|
91 |
+
# Extract text from PDF
|
92 |
+
text, error = extract_text_from_pdf(pdf_file)
|
93 |
+
if error:
|
94 |
+
return f"β {error}", "", ""
|
95 |
+
|
96 |
+
progress(0.4, desc="Text extracted successfully...")
|
97 |
+
|
98 |
+
# Show preview of extracted text
|
99 |
+
text_preview = text[:500] + "..." if len(text) > 500 else text
|
100 |
+
|
101 |
+
progress(0.6, desc="Generating summary with Groq AI...")
|
102 |
+
|
103 |
+
# Generate summary
|
104 |
+
summary = summarize_with_groq(api_key, text)
|
105 |
+
|
106 |
+
progress(1.0, desc="Summary generated successfully!")
|
107 |
+
|
108 |
+
return "β
Summary generated successfully!", text_preview, summary
|
109 |
+
|
110 |
+
except Exception as e:
|
111 |
+
error_traceback = traceback.format_exc()
|
112 |
+
return f"β Unexpected error: {str(e)}\n\nTraceback:\n{error_traceback}", "", ""
|
113 |
+
|
114 |
+
def clear_all():
|
115 |
+
"""Clear all fields"""
|
116 |
+
return "", None, "", "", ""
|
117 |
+
|
118 |
+
# Custom CSS for better styling
|
119 |
+
css = """
|
120 |
+
.gradio-container {
|
121 |
+
max-width: 1200px !important;
|
122 |
+
margin: auto !important;
|
123 |
+
}
|
124 |
+
.main-header {
|
125 |
+
text-align: center;
|
126 |
+
margin-bottom: 2rem;
|
127 |
+
}
|
128 |
+
.status-success {
|
129 |
+
color: #28a745 !important;
|
130 |
+
}
|
131 |
+
.status-error {
|
132 |
+
color: #dc3545 !important;
|
133 |
+
}
|
134 |
+
.info-box {
|
135 |
+
background-color: #f8f9fa;
|
136 |
+
padding: 1rem;
|
137 |
+
border-radius: 0.5rem;
|
138 |
+
border-left: 4px solid #007bff;
|
139 |
+
margin: 1rem 0;
|
140 |
+
}
|
141 |
+
"""
|
142 |
+
|
143 |
+
# Create Gradio interface
|
144 |
+
with gr.Blocks(css=css, title="PDF Summarizer with Groq AI") as demo:
|
145 |
+
# Header
|
146 |
+
gr.HTML("""
|
147 |
+
<div class="main-header">
|
148 |
+
<h1>π PDF Summarizer with Groq AI</h1>
|
149 |
+
<p>Upload any PDF document and get an AI-powered summary using Groq's Llama model</p>
|
150 |
+
</div>
|
151 |
+
""")
|
152 |
+
|
153 |
+
# Info box
|
154 |
+
gr.HTML("""
|
155 |
+
<div class="info-box">
|
156 |
+
<strong>π How to get your Groq API Key:</strong><br>
|
157 |
+
1. Visit <a href="https://console.groq.com/" target="_blank">console.groq.com</a><br>
|
158 |
+
2. Sign up or log in to your account<br>
|
159 |
+
3. Navigate to API Keys section<br>
|
160 |
+
4. Create a new API key and copy it<br>
|
161 |
+
5. Paste it in the field below
|
162 |
+
</div>
|
163 |
+
""")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column(scale=1):
|
167 |
+
# Input section
|
168 |
+
gr.Markdown("## π§ Configuration")
|
169 |
+
api_key_input = gr.Textbox(
|
170 |
+
label="Groq API Key",
|
171 |
+
placeholder="Enter your Groq API key here...",
|
172 |
+
type="password"
|
173 |
+
)
|
174 |
+
gr.Markdown("*Your API key is not stored and only used for this session*")
|
175 |
+
|
176 |
+
pdf_file_input = gr.File(
|
177 |
+
label="Upload PDF Document",
|
178 |
+
file_types=[".pdf"]
|
179 |
+
)
|
180 |
+
gr.Markdown("*Upload any PDF file to summarize*")
|
181 |
+
|
182 |
+
with gr.Row():
|
183 |
+
summarize_btn = gr.Button("π Generate Summary", variant="primary", size="lg")
|
184 |
+
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
185 |
+
|
186 |
+
with gr.Column(scale=2):
|
187 |
+
# Output section
|
188 |
+
gr.Markdown("## π Results")
|
189 |
+
status_output = gr.Textbox(
|
190 |
+
label="Status",
|
191 |
+
interactive=False,
|
192 |
+
show_label=True
|
193 |
+
)
|
194 |
+
|
195 |
+
with gr.Tabs():
|
196 |
+
with gr.TabItem("π Summary"):
|
197 |
+
summary_output = gr.Textbox(
|
198 |
+
label="AI Generated Summary",
|
199 |
+
lines=15,
|
200 |
+
interactive=False,
|
201 |
+
placeholder="Your PDF summary will appear here..."
|
202 |
+
)
|
203 |
+
|
204 |
+
with gr.TabItem("π Extracted Text Preview"):
|
205 |
+
text_preview_output = gr.Textbox(
|
206 |
+
label="Extracted Text (First 500 characters)",
|
207 |
+
lines=10,
|
208 |
+
interactive=False,
|
209 |
+
placeholder="Preview of extracted text will appear here..."
|
210 |
+
)
|
211 |
+
|
212 |
+
# Usage instructions
|
213 |
+
gr.HTML("""
|
214 |
+
<div class="info-box">
|
215 |
+
<strong>π Usage Instructions:</strong><br>
|
216 |
+
1. Enter your Groq API key in the field above<br>
|
217 |
+
2. Upload a PDF document (any size, any content)<br>
|
218 |
+
3. Click "Generate Summary" to process your document<br>
|
219 |
+
4. View the AI-generated summary and extracted text preview<br>
|
220 |
+
5. Use "Clear All" to reset all fields
|
221 |
+
</div>
|
222 |
+
""")
|
223 |
+
|
224 |
+
# Model information
|
225 |
+
gr.HTML("""
|
226 |
+
<div style="margin-top: 2rem; padding: 1rem; background-color: #e9ecef; border-radius: 0.5rem;">
|
227 |
+
<strong>π€ Model Information:</strong><br>
|
228 |
+
β’ Model: Llama-3.3-70B-Versatile (via Groq)<br>
|
229 |
+
β’ Temperature: 0.3 (focused, factual summaries)<br>
|
230 |
+
β’ Max Tokens: 2048 (comprehensive summaries)<br>
|
231 |
+
β’ Top-p: 0.9 (balanced creativity and accuracy)
|
232 |
+
</div>
|
233 |
+
""")
|
234 |
+
|
235 |
+
# Event handlers
|
236 |
+
summarize_btn.click(
|
237 |
+
fn=process_pdf_and_summarize,
|
238 |
+
inputs=[api_key_input, pdf_file_input],
|
239 |
+
outputs=[status_output, text_preview_output, summary_output],
|
240 |
+
show_progress=True
|
241 |
+
)
|
242 |
+
|
243 |
+
clear_btn.click(
|
244 |
+
fn=clear_all,
|
245 |
+
outputs=[api_key_input, pdf_file_input, status_output, text_preview_output, summary_output]
|
246 |
+
)
|
247 |
+
|
248 |
+
# Launch the app
|
249 |
+
if __name__ == "__main__":
|
250 |
+
print("π Starting PDF Summarizer App...")
|
251 |
+
print("π Make sure you have the required packages installed:")
|
252 |
+
print(" pip install gradio groq PyPDF2")
|
253 |
+
print("\nπ Don't forget to get your Groq API key from: https://console.groq.com/")
|
254 |
+
|
255 |
+
demo.launch(
|
256 |
+
server_name="0.0.0.0", # Allow external connections
|
257 |
+
server_port=7860, # Default port
|
258 |
+
share=True, # Create shareable link
|
259 |
+
debug=True, # Enable debug mode
|
260 |
+
show_error=True # Show detailed errors
|
261 |
+
)
|