Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import tempfile
|
3 |
-
import os
|
4 |
import base64
|
|
|
5 |
from openai import OpenAI
|
6 |
|
7 |
def generate_systematic_review(api_key, pdf_files):
|
@@ -16,10 +15,20 @@ def generate_systematic_review(api_key, pdf_files):
|
|
16 |
str: Generated systematic review text
|
17 |
"""
|
18 |
if not api_key.strip():
|
19 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
if not pdf_files:
|
22 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
try:
|
25 |
# Initialize OpenAI client with the provided API key
|
@@ -28,9 +37,13 @@ def generate_systematic_review(api_key, pdf_files):
|
|
28 |
# Create a list to hold file inputs for the API
|
29 |
file_inputs = []
|
30 |
|
|
|
|
|
|
|
31 |
# Process each uploaded PDF file
|
32 |
for pdf_file in pdf_files:
|
33 |
file_name = os.path.basename(pdf_file.name)
|
|
|
34 |
|
35 |
# Read the file as binary data
|
36 |
with open(pdf_file.name, "rb") as f:
|
@@ -120,38 +133,206 @@ By following these steps, you can ensure that your systematic review paper is we
|
|
120 |
result_text += item.text
|
121 |
|
122 |
if result_text:
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
except Exception as e:
|
128 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
# Create the Gradio interface
|
131 |
-
with gr.Blocks(title="Systematic Review Generator") as app:
|
132 |
gr.Markdown("# Systematic Review Generator")
|
133 |
gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4.1 model.")
|
134 |
|
135 |
with gr.Row():
|
136 |
-
with gr.Column():
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
# Set up the event handler
|
157 |
submit_btn.click(
|
@@ -159,22 +340,6 @@ with gr.Blocks(title="Systematic Review Generator") as app:
|
|
159 |
inputs=[api_key, pdf_files],
|
160 |
outputs=output
|
161 |
)
|
162 |
-
|
163 |
-
gr.Markdown("""
|
164 |
-
## How to Use
|
165 |
-
|
166 |
-
1. Enter your OpenAI API key
|
167 |
-
2. Upload two or more PDF research papers
|
168 |
-
3. Click "Generate Systematic Review"
|
169 |
-
4. The systematic review will be displayed in the output area
|
170 |
-
|
171 |
-
## Note
|
172 |
-
|
173 |
-
This application requires a valid OpenAI API key with access to the GPT-4.1 model.
|
174 |
-
Your API key is not stored and is only used to make the API call to OpenAI.
|
175 |
-
|
176 |
-
Please note that large PDF files may cause issues with the API due to size limits.
|
177 |
-
""")
|
178 |
|
179 |
if __name__ == "__main__":
|
180 |
-
app.launch(
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import base64
|
3 |
+
import os
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
|
|
15 |
str: Generated systematic review text
|
16 |
"""
|
17 |
if not api_key.strip():
|
18 |
+
return """
|
19 |
+
<div class="error-message">
|
20 |
+
<h3>Error</h3>
|
21 |
+
<p>Please provide a valid OpenAI API key.</p>
|
22 |
+
</div>
|
23 |
+
"""
|
24 |
|
25 |
if not pdf_files:
|
26 |
+
return """
|
27 |
+
<div class="error-message">
|
28 |
+
<h3>Error</h3>
|
29 |
+
<p>Please upload at least one PDF file.</p>
|
30 |
+
</div>
|
31 |
+
"""
|
32 |
|
33 |
try:
|
34 |
# Initialize OpenAI client with the provided API key
|
|
|
37 |
# Create a list to hold file inputs for the API
|
38 |
file_inputs = []
|
39 |
|
40 |
+
# List of uploaded file names for display
|
41 |
+
file_names = []
|
42 |
+
|
43 |
# Process each uploaded PDF file
|
44 |
for pdf_file in pdf_files:
|
45 |
file_name = os.path.basename(pdf_file.name)
|
46 |
+
file_names.append(file_name)
|
47 |
|
48 |
# Read the file as binary data
|
49 |
with open(pdf_file.name, "rb") as f:
|
|
|
133 |
result_text += item.text
|
134 |
|
135 |
if result_text:
|
136 |
+
# Format the file names for display
|
137 |
+
files_html = ""
|
138 |
+
for name in file_names:
|
139 |
+
files_html += f'<div class="file-pill"><span class="file-icon">📄</span> {name} <span class="file-x">×</span></div>'
|
140 |
+
|
141 |
+
# Create a nicely formatted response interface
|
142 |
+
return f"""
|
143 |
+
<div class="response-container">
|
144 |
+
<div class="files-container">
|
145 |
+
{files_html}
|
146 |
+
</div>
|
147 |
+
<div class="assistant-label">Assistant</div>
|
148 |
+
<div class="review-content">
|
149 |
+
<p>Here is a <strong>systematic review</strong> of the provided papers:</p>
|
150 |
+
<hr>
|
151 |
+
{result_text}
|
152 |
+
</div>
|
153 |
+
</div>
|
154 |
+
"""
|
155 |
|
156 |
+
return """
|
157 |
+
<div class="error-message">
|
158 |
+
<h3>Error</h3>
|
159 |
+
<p>Failed to generate a systematic review. Please try again.</p>
|
160 |
+
</div>
|
161 |
+
"""
|
162 |
|
163 |
except Exception as e:
|
164 |
+
return f"""
|
165 |
+
<div class="error-message">
|
166 |
+
<h3>Error</h3>
|
167 |
+
<p>An error occurred: {str(e)}</p>
|
168 |
+
</div>
|
169 |
+
"""
|
170 |
+
|
171 |
+
# Custom CSS for the interface
|
172 |
+
custom_css = """
|
173 |
+
.gradio-container {
|
174 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
175 |
+
}
|
176 |
+
|
177 |
+
.response-container {
|
178 |
+
background-color: #f9f9f9;
|
179 |
+
border-radius: 8px;
|
180 |
+
padding: 15px;
|
181 |
+
margin-top: 10px;
|
182 |
+
font-size: 16px;
|
183 |
+
}
|
184 |
+
|
185 |
+
.files-container {
|
186 |
+
display: flex;
|
187 |
+
flex-wrap: wrap;
|
188 |
+
gap: 8px;
|
189 |
+
margin-bottom: 15px;
|
190 |
+
}
|
191 |
+
|
192 |
+
.file-pill {
|
193 |
+
background-color: #f0f0f0;
|
194 |
+
border-radius: 16px;
|
195 |
+
padding: 4px 12px;
|
196 |
+
display: flex;
|
197 |
+
align-items: center;
|
198 |
+
gap: 5px;
|
199 |
+
font-size: 14px;
|
200 |
+
}
|
201 |
+
|
202 |
+
.file-icon {
|
203 |
+
margin-right: 4px;
|
204 |
+
}
|
205 |
+
|
206 |
+
.file-x {
|
207 |
+
margin-left: 4px;
|
208 |
+
color: #888;
|
209 |
+
}
|
210 |
+
|
211 |
+
.assistant-label {
|
212 |
+
font-weight: 600;
|
213 |
+
margin-bottom: 5px;
|
214 |
+
color: #444;
|
215 |
+
}
|
216 |
+
|
217 |
+
.review-content {
|
218 |
+
background-color: white;
|
219 |
+
border-radius: 8px;
|
220 |
+
padding: 15px;
|
221 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
222 |
+
}
|
223 |
+
|
224 |
+
.review-content h1, .review-content h2, .review-content h3 {
|
225 |
+
margin-top: 20px;
|
226 |
+
margin-bottom: 10px;
|
227 |
+
font-weight: 600;
|
228 |
+
}
|
229 |
+
|
230 |
+
.review-content h1 {
|
231 |
+
font-size: 24px;
|
232 |
+
border-bottom: 1px solid #eee;
|
233 |
+
padding-bottom: 10px;
|
234 |
+
}
|
235 |
+
|
236 |
+
.review-content h2 {
|
237 |
+
font-size: 20px;
|
238 |
+
}
|
239 |
+
|
240 |
+
.review-content h3 {
|
241 |
+
font-size: 18px;
|
242 |
+
}
|
243 |
+
|
244 |
+
.review-content p {
|
245 |
+
margin-bottom: 15px;
|
246 |
+
line-height: 1.5;
|
247 |
+
}
|
248 |
+
|
249 |
+
.review-content hr {
|
250 |
+
margin: 20px 0;
|
251 |
+
border: 0;
|
252 |
+
border-top: 1px solid #eee;
|
253 |
+
}
|
254 |
+
|
255 |
+
.review-content table {
|
256 |
+
border-collapse: collapse;
|
257 |
+
width: 100%;
|
258 |
+
margin: 20px 0;
|
259 |
+
}
|
260 |
+
|
261 |
+
.review-content th, .review-content td {
|
262 |
+
border: 1px solid #ddd;
|
263 |
+
padding: 8px 12px;
|
264 |
+
text-align: left;
|
265 |
+
}
|
266 |
+
|
267 |
+
.review-content th {
|
268 |
+
background-color: #f2f2f2;
|
269 |
+
font-weight: 600;
|
270 |
+
}
|
271 |
+
|
272 |
+
.review-content tr:nth-child(even) {
|
273 |
+
background-color: #f9f9f9;
|
274 |
+
}
|
275 |
+
|
276 |
+
.error-message {
|
277 |
+
background-color: #fff0f0;
|
278 |
+
border-left: 4px solid #ff5252;
|
279 |
+
padding: 15px;
|
280 |
+
border-radius: 4px;
|
281 |
+
margin-top: 10px;
|
282 |
+
}
|
283 |
+
|
284 |
+
.error-message h3 {
|
285 |
+
color: #d32f2f;
|
286 |
+
margin-top: 0;
|
287 |
+
margin-bottom: 10px;
|
288 |
+
}
|
289 |
+
"""
|
290 |
|
291 |
# Create the Gradio interface
|
292 |
+
with gr.Blocks(css=custom_css, title="Systematic Review Generator") as app:
|
293 |
gr.Markdown("# Systematic Review Generator")
|
294 |
gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4.1 model.")
|
295 |
|
296 |
with gr.Row():
|
297 |
+
with gr.Column(scale=1):
|
298 |
+
with gr.Box():
|
299 |
+
gr.Markdown("### Settings")
|
300 |
+
api_key = gr.Textbox(
|
301 |
+
label="OpenAI API Key",
|
302 |
+
placeholder="Enter your OpenAI API key...",
|
303 |
+
type="password"
|
304 |
+
)
|
305 |
+
|
306 |
+
pdf_files = gr.File(
|
307 |
+
label="Upload PDF Files",
|
308 |
+
file_count="multiple",
|
309 |
+
file_types=[".pdf"]
|
310 |
+
)
|
311 |
+
|
312 |
+
model_info = gr.Markdown("""
|
313 |
+
**Model**: gpt-4.1
|
314 |
+
**Tokens**: 4000 (max output)
|
315 |
+
**Temperature**: 0.7
|
316 |
+
""")
|
317 |
+
|
318 |
+
submit_btn = gr.Button("Generate Systematic Review", variant="primary", size="lg")
|
319 |
|
320 |
+
with gr.Accordion("How to Use", open=False):
|
321 |
+
gr.Markdown("""
|
322 |
+
1. Enter your OpenAI API key in the field above
|
323 |
+
2. Upload two or more PDF research papers
|
324 |
+
3. Click "Generate Systematic Review"
|
325 |
+
4. The systematic review will be displayed in the output area
|
326 |
+
|
327 |
+
**Note**: This application requires a valid OpenAI API key with access to the GPT-4.1 model.
|
328 |
+
Your API key is not stored and is only used to make the API call to OpenAI.
|
329 |
+
|
330 |
+
Please be aware that large PDF files may cause issues with the API due to size limits.
|
331 |
+
""")
|
332 |
+
|
333 |
+
with gr.Column(scale=2):
|
334 |
+
# HTML output for better formatting
|
335 |
+
output = gr.HTML(label="Generated Review")
|
336 |
|
337 |
# Set up the event handler
|
338 |
submit_btn.click(
|
|
|
340 |
inputs=[api_key, pdf_files],
|
341 |
outputs=output
|
342 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
344 |
if __name__ == "__main__":
|
345 |
+
app.launch()
|