shukdevdatta123 commited on
Commit
a2df9a3
·
verified ·
1 Parent(s): e56be65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +282 -34
app.py CHANGED
@@ -37,7 +37,8 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True):
37
  openai.api_key = api_key
38
 
39
  # Create the system message with systematic review guidelines
40
- system_prompt = """You are an expert academic assistant. Create a systematic review in HTML format using <h2>, <h3>, <p>, <ul>, and <table> tags. The Systematic Review must be in great details. Structure it using these steps:
 
41
  Step 1: Identify a Research Field
42
  The first step in writing a systematic review paper is to identify a research field. This involves selecting a specific area of study that you are interested in and want to explore further.
43
 
@@ -70,6 +71,8 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True):
70
 
71
  Step-11:
72
  Please include references in the form of citation and also link to the reference papers.
 
 
73
  """
74
 
75
  # Extract text from each PDF
@@ -101,7 +104,7 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True):
101
  {"role": "user", "content": user_prompt + "\n\n" + "\n\n".join([f"Paper {i+1} - {pdf_names[i]}:\n{pdf_texts[i]}" for i in range(len(pdf_texts))])}
102
  ]
103
 
104
- # Call the API with temperature=1 and top_p=1 as specified
105
  response = openai.ChatCompletion.create(
106
  model="gpt-4.1",
107
  messages=messages,
@@ -110,19 +113,160 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True):
110
  max_tokens=16384
111
  )
112
 
113
- # Format the response in HTML
114
  review_content = response["choices"][0]["message"]["content"]
115
 
116
- # Create a basic HTML structure
117
- html_output = f"""
118
- <h2>Systematic Review</h2>
119
- <p>{review_content}</p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  """
121
 
122
- return html_output
123
 
124
  except Exception as e:
125
- return f"Error generating systematic review: {str(e)}"
 
 
 
 
 
126
 
127
  # Function to save uploaded files
128
  def save_uploaded_files(files):
@@ -139,34 +283,107 @@ def save_uploaded_files(files):
139
 
140
  return saved_paths
141
 
142
- # Add CSS styling
143
  custom_css = """
144
  <style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  #generate_button {
146
- background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%); /* Purple gradient */
147
  color: white;
148
  font-weight: bold;
 
 
 
149
  }
150
  #generate_button:hover {
151
- background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%); /* Slightly lighter */
 
 
152
  }
 
 
153
  #api_key_button {
154
- background: linear-gradient(135deg, #68d391 0%, #48bb78 100%); /* Green gradient */
155
  color: white;
156
  font-weight: bold;
157
  margin-top: 27px;
 
 
 
158
  }
159
  #api_key_button:hover {
160
- background: linear-gradient(135deg, #38a169 0%, #68d391 100%); /* Slightly darker green */
 
 
161
  }
162
- .gradio-container {
163
- font-family: 'Arial', sans-serif;
164
- background-color: #f0f4f8;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  }
166
  </style>
167
  """
168
 
169
- # Gradio UI Layout
170
  with gr.Blocks(css=custom_css) as demo:
171
  gr.Markdown("# Systematic Review Generator for Research Papers")
172
 
@@ -179,29 +396,38 @@ with gr.Blocks(css=custom_css) as demo:
179
  4. Check the "Include Tables" option if you want the review to include comparison tables
180
  5. Click "Generate Systematic Review" to start the process
181
 
182
- ### Tips:
183
- - For best results, upload papers that are related to the same research topic or field
184
  - Be specific in your review question to get more focused results
185
  - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
186
  - The more papers you upload, the more comprehensive the review will be
 
187
  """)
188
 
189
- # API Key Input
190
- with gr.Row():
191
- api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
192
- api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
 
 
193
  api_key_output = gr.Textbox(label="API Key Status", interactive=False)
194
 
195
  # PDF Upload and Review Settings
196
- with gr.Row():
197
  with gr.Column():
198
- pdf_files = gr.File(label="Upload PDF Research Papers", file_count="multiple", type="binary")
199
- review_question = gr.Textbox(label="Review Question or Topic", value="Please Generate a systematic review of the following papers.")
 
 
 
 
 
200
  include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
201
- generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button")
202
-
203
- # Output
204
- review_output = gr.HTML(label="Systematic Review")
 
205
 
206
  # Button actions
207
  api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
@@ -209,11 +435,33 @@ with gr.Blocks(css=custom_css) as demo:
209
  # Generate systematic review
210
  def process_files_and_generate_review(files, question, include_tables):
211
  if not files:
212
- return "Please upload at least one PDF file."
 
 
 
 
 
213
 
214
  # Save uploaded files
215
  saved_paths = save_uploaded_files(files)
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  # Generate review
218
  review = generate_systematic_review(saved_paths, question, include_tables)
219
 
@@ -224,7 +472,7 @@ with gr.Blocks(css=custom_css) as demo:
224
  except:
225
  pass
226
 
227
- return review
228
 
229
  generate_button.click(
230
  process_files_and_generate_review,
@@ -234,4 +482,4 @@ with gr.Blocks(css=custom_css) as demo:
234
 
235
  # Launch the app
236
  if __name__ == "__main__":
237
- demo.launch(share=True)
 
37
  openai.api_key = api_key
38
 
39
  # Create the system message with systematic review guidelines
40
+ system_prompt = """You are an expert academic assistant. Create a systematic review using academic research paper formatting. The Systematic Review must be in great details. Structure it using these steps:
41
+
42
  Step 1: Identify a Research Field
43
  The first step in writing a systematic review paper is to identify a research field. This involves selecting a specific area of study that you are interested in and want to explore further.
44
 
 
71
 
72
  Step-11:
73
  Please include references in the form of citation and also link to the reference papers.
74
+
75
+ Your response should be formatted in HTML but generate the content to look like a professional academic paper. Include proper section headers, abstracts, methodology sections, etc. Number all sections like an academic paper.
76
  """
77
 
78
  # Extract text from each PDF
 
104
  {"role": "user", "content": user_prompt + "\n\n" + "\n\n".join([f"Paper {i+1} - {pdf_names[i]}:\n{pdf_texts[i]}" for i in range(len(pdf_texts))])}
105
  ]
106
 
107
+ # Call the API with temperature=0.7 and top_p=1
108
  response = openai.ChatCompletion.create(
109
  model="gpt-4.1",
110
  messages=messages,
 
113
  max_tokens=16384
114
  )
115
 
116
+ # Get the AI response
117
  review_content = response["choices"][0]["message"]["content"]
118
 
119
+ # Apply professional academic paper styling
120
+ styled_html = f"""
121
+ <!DOCTYPE html>
122
+ <html lang="en">
123
+ <head>
124
+ <meta charset="UTF-8">
125
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
126
+ <title>Systematic Review</title>
127
+ <style>
128
+ /* Academic Paper Styling */
129
+ body {{
130
+ font-family: 'Times New Roman', Times, serif;
131
+ line-height: 1.6;
132
+ color: #333;
133
+ margin: 0;
134
+ padding: 0;
135
+ background-color: #f9f9f9;
136
+ }}
137
+ .paper-container {{
138
+ max-width: 800px;
139
+ margin: 0 auto;
140
+ padding: 40px;
141
+ background-color: white;
142
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
143
+ }}
144
+ header {{
145
+ text-align: center;
146
+ margin-bottom: 30px;
147
+ border-bottom: 1px solid #ddd;
148
+ padding-bottom: 20px;
149
+ }}
150
+ h1 {{
151
+ font-size: 24px;
152
+ margin: 0 0 15px;
153
+ font-weight: bold;
154
+ }}
155
+ .author-info {{
156
+ font-size: 14px;
157
+ margin-bottom: 15px;
158
+ }}
159
+ .abstract {{
160
+ font-style: italic;
161
+ margin: 20px 0;
162
+ padding: 15px;
163
+ background-color: #f8f8f8;
164
+ border-left: 3px solid #ccc;
165
+ }}
166
+ h2 {{
167
+ font-size: 18px;
168
+ margin: 30px 0 15px;
169
+ border-bottom: 1px solid #eee;
170
+ padding-bottom: 5px;
171
+ }}
172
+ h3 {{
173
+ font-size: 16px;
174
+ margin: 25px 0 10px;
175
+ }}
176
+ p {{
177
+ margin: 0 0 15px;
178
+ text-align: justify;
179
+ }}
180
+ .section {{
181
+ margin-bottom: 30px;
182
+ }}
183
+ table {{
184
+ width: 100%;
185
+ border-collapse: collapse;
186
+ margin: 20px 0;
187
+ font-size: 14px;
188
+ }}
189
+ table, th, td {{
190
+ border: 1px solid #ddd;
191
+ }}
192
+ th, td {{
193
+ padding: 10px;
194
+ text-align: left;
195
+ }}
196
+ th {{
197
+ background-color: #f2f2f2;
198
+ }}
199
+ tr:nth-child(even) {{
200
+ background-color: #f9f9f9;
201
+ }}
202
+ .citation {{
203
+ font-size: 14px;
204
+ color: #555;
205
+ }}
206
+ .reference-list {{
207
+ margin-top: 40px;
208
+ border-top: 1px solid #ddd;
209
+ padding-top: 20px;
210
+ }}
211
+ .reference-list h2 {{
212
+ margin-top: 0;
213
+ }}
214
+ .reference-item {{
215
+ margin-bottom: 10px;
216
+ padding-left: 25px;
217
+ text-indent: -25px;
218
+ }}
219
+ ul, ol {{
220
+ margin: 15px 0;
221
+ padding-left: 25px;
222
+ }}
223
+ li {{
224
+ margin-bottom: 5px;
225
+ }}
226
+ .figure {{
227
+ margin: 25px 0;
228
+ text-align: center;
229
+ }}
230
+ .figure img {{
231
+ max-width: 100%;
232
+ }}
233
+ .figure-caption {{
234
+ font-size: 14px;
235
+ color: #666;
236
+ margin-top: 10px;
237
+ }}
238
+ .footnote {{
239
+ font-size: 12px;
240
+ color: #777;
241
+ }}
242
+ @media print {{
243
+ body {{
244
+ background-color: white;
245
+ }}
246
+ .paper-container {{
247
+ box-shadow: none;
248
+ padding: 0;
249
+ }}
250
+ }}
251
+ </style>
252
+ </head>
253
+ <body>
254
+ <div class="paper-container">
255
+ {review_content}
256
+ </div>
257
+ </body>
258
+ </html>
259
  """
260
 
261
+ return styled_html
262
 
263
  except Exception as e:
264
+ return f"""
265
+ <div style="color: red; padding: 20px; border: 1px solid red; border-radius: 5px; background-color: #ffecec;">
266
+ <h3>Error Generating Systematic Review</h3>
267
+ <p>{str(e)}</p>
268
+ </div>
269
+ """
270
 
271
  # Function to save uploaded files
272
  def save_uploaded_files(files):
 
283
 
284
  return saved_paths
285
 
286
+ # Add CSS styling for the Gradio interface
287
  custom_css = """
288
  <style>
289
+ /* Main UI */
290
+ .gradio-container {
291
+ font-family: 'Arial', sans-serif;
292
+ background-color: #f9f9f9;
293
+ }
294
+
295
+ /* Header */
296
+ h1 {
297
+ font-size: 28px;
298
+ color: #333;
299
+ margin-bottom: 20px;
300
+ text-align: center;
301
+ padding-bottom: 10px;
302
+ border-bottom: 2px solid #4a00e0;
303
+ }
304
+
305
+ /* Primary Button */
306
  #generate_button {
307
+ background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
308
  color: white;
309
  font-weight: bold;
310
+ padding: 10px 20px;
311
+ border-radius: 5px;
312
+ transition: all 0.3s ease;
313
  }
314
  #generate_button:hover {
315
+ background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%);
316
+ transform: translateY(-2px);
317
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
318
  }
319
+
320
+ /* API Key Button */
321
  #api_key_button {
322
+ background: linear-gradient(135deg, #68d391 0%, #48bb78 100%);
323
  color: white;
324
  font-weight: bold;
325
  margin-top: 27px;
326
+ padding: 10px 20px;
327
+ border-radius: 5px;
328
+ transition: all 0.3s ease;
329
  }
330
  #api_key_button:hover {
331
+ background: linear-gradient(135deg, #38a169 0%, #68d391 100%);
332
+ transform: translateY(-2px);
333
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
334
  }
335
+
336
+ /* Form Elements */
337
+ .input-container {
338
+ background-color: white;
339
+ padding: 20px;
340
+ border-radius: 8px;
341
+ box-shadow: 0 2px 10px rgba(0,0,0,0.05);
342
+ margin-bottom: 20px;
343
+ }
344
+
345
+ /* Labels */
346
+ label {
347
+ font-weight: 600;
348
+ color: #555;
349
+ margin-bottom: 8px;
350
+ }
351
+
352
+ /* Instructions Accordion */
353
+ .accordion {
354
+ background-color: white;
355
+ border: 1px solid #e0e0e0;
356
+ border-radius: 8px;
357
+ margin-bottom: 20px;
358
+ }
359
+
360
+ /* Output Container */
361
+ .output-container {
362
+ background-color: white;
363
+ padding: 15px;
364
+ border-radius: 8px;
365
+ box-shadow: 0 2px 10px rgba(0,0,0,0.05);
366
+ }
367
+
368
+ /* File Upload Area */
369
+ .file-upload {
370
+ border: 2px dashed #ccc;
371
+ border-radius: 5px;
372
+ padding: 20px;
373
+ text-align: center;
374
+ margin-bottom: 20px;
375
+ }
376
+
377
+ /* Responsive adjustments */
378
+ @media screen and (max-width: 768px) {
379
+ .gradio-container {
380
+ padding: 10px;
381
+ }
382
  }
383
  </style>
384
  """
385
 
386
+ # Gradio UI Layout with improved styling
387
  with gr.Blocks(css=custom_css) as demo:
388
  gr.Markdown("# Systematic Review Generator for Research Papers")
389
 
 
396
  4. Check the "Include Tables" option if you want the review to include comparison tables
397
  5. Click "Generate Systematic Review" to start the process
398
 
399
+ ### Tips for Best Results:
400
+ - Upload papers that are related to the same research topic or field
401
  - Be specific in your review question to get more focused results
402
  - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
403
  - The more papers you upload, the more comprehensive the review will be
404
+ - The review will be formatted as a professional academic paper with proper sections and citations
405
  """)
406
 
407
+ # API Key Input in a styled container
408
+ with gr.Row(elem_classes="input-container"):
409
+ with gr.Column(scale=3):
410
+ api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="sk-...")
411
+ with gr.Column(scale=1):
412
+ api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
413
  api_key_output = gr.Textbox(label="API Key Status", interactive=False)
414
 
415
  # PDF Upload and Review Settings
416
+ with gr.Row(elem_classes="input-container"):
417
  with gr.Column():
418
+ gr.Markdown("### Upload Research Papers")
419
+ pdf_files = gr.File(label="Upload PDF Research Papers", file_count="multiple", type="binary", elem_classes="file-upload")
420
+ review_question = gr.Textbox(
421
+ label="Review Question or Topic",
422
+ value="Please generate a systematic review of the following papers.",
423
+ placeholder="e.g., What are the effects of mindfulness meditation on stress reduction?"
424
+ )
425
  include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
426
+ generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
427
+
428
+ # Output with improved styling
429
+ with gr.Row(elem_classes="output-container"):
430
+ review_output = gr.HTML(label="Systematic Review")
431
 
432
  # Button actions
433
  api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
 
435
  # Generate systematic review
436
  def process_files_and_generate_review(files, question, include_tables):
437
  if not files:
438
+ return """
439
+ <div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
440
+ <h3 style="color: #666;">Please upload at least one PDF file.</h3>
441
+ <p>To generate a systematic review, upload one or more research papers in PDF format.</p>
442
+ </div>
443
+ """
444
 
445
  # Save uploaded files
446
  saved_paths = save_uploaded_files(files)
447
 
448
+ # Show loading message
449
+ yield """
450
+ <div style="padding: 20px; text-align: center;">
451
+ <h3>Generating Systematic Review...</h3>
452
+ <p>This may take a few minutes depending on the number and size of papers.</p>
453
+ <div style="width: 100%; height: 4px; background-color: #f0f0f0; margin: 20px 0; border-radius: 2px; overflow: hidden;">
454
+ <div style="width: 30%; height: 100%; background: linear-gradient(90deg, #4a00e0, #8e2de2); animation: progress 2s infinite linear;"></div>
455
+ </div>
456
+ <style>
457
+ @keyframes progress {
458
+ 0% { margin-left: -30%; }
459
+ 100% { margin-left: 100%; }
460
+ }
461
+ </style>
462
+ </div>
463
+ """
464
+
465
  # Generate review
466
  review = generate_systematic_review(saved_paths, question, include_tables)
467
 
 
472
  except:
473
  pass
474
 
475
+ yield review
476
 
477
  generate_button.click(
478
  process_files_and_generate_review,
 
482
 
483
  # Launch the app
484
  if __name__ == "__main__":
485
+ demo.launch(share=True)