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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import fitz # PyMuPDF for PDF processing
4
+ import os
5
+ import tempfile
6
+ import base64
7
+ from datetime import datetime
8
+
9
+ # Variable to store API key
10
+ api_key = ""
11
+
12
+ # Function to update API key
13
+ def set_api_key(key):
14
+ global api_key
15
+ api_key = key
16
+ return "API Key Set Successfully!"
17
+
18
+ # Function to extract text from PDF
19
+ def extract_text_from_pdf(pdf_path):
20
+ try:
21
+ doc = fitz.open(pdf_path)
22
+ text = "\n".join([page.get_text("text") for page in doc])
23
+ return text
24
+ except Exception as e:
25
+ return f"Error extracting text from PDF: {str(e)}"
26
+
27
+ # Function to interact with OpenAI API for systematic review
28
+ def generate_systematic_review(pdf_files, review_question, include_tables=True):
29
+ if not api_key:
30
+ return "Please enter your OpenAI API key first."
31
+
32
+ if not pdf_files:
33
+ return "Please upload at least one PDF file."
34
+
35
+ if not review_question:
36
+ return "Please enter a review question."
37
+
38
+ try:
39
+ openai.api_key = api_key
40
+
41
+ # Create the system message with systematic review guidelines
42
+ 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:
43
+
44
+ Step 1: Identify a Research Field
45
+ 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.
46
+
47
+ Step 2: Generate a Research Question
48
+ Once you have identified your research field, the next step is to generate a research question. This question should be specific, measurable, achievable, relevant, and time-bound (SMART).
49
+
50
+ Step 3: Create a Protocol
51
+ After generating your research question, the next step is to create a protocol. A detailed plan of how you will conduct your research, including the methods you will use, the data you will collect, and the analysis you will perform.
52
+
53
+ Step 4: Evaluate Relevant Literature
54
+ The fourth step is to evaluate relevant literature. This involves searching for and reviewing existing studies related to your research question. You should critically evaluate the quality of these studies and identify any gaps or limitations in the current literature.
55
+
56
+ Step 5: Investigate Sources for Answers
57
+ The fifth step is to investigate sources for answers. This involves searching for and accessing relevant data and information that will help you answer your research question.
58
+
59
+ Step 6: Collect Data as per Protocol
60
+ The sixth step is to collect data as per protocol. This involves implementing the methods outlined in your protocol and collecting the data specified. You should ensure that your data collection methods are rigorous and reliable.
61
+
62
+ Step 7: Data Extraction
63
+ The seventh step is to extract the data. This involves organizing and analyzing the data you have collected, and extracting the relevant information that will help you answer your research question.
64
+
65
+ Step 8: Critical Analysis of Results
66
+ The eighth step is to conduct a critical analysis of your results. This involves interpreting your findings, identifying patterns and trends, and drawing conclusions based on your data.
67
+
68
+ Step 9: Interpreting Derivations
69
+ The ninth step is to interpret the derivations. This involves taking the conclusions you have drawn from your data and interpreting them in the context of your research question.
70
+
71
+ Step 10: Concluding Statements
72
+ The final step is to make concluding statements. This involves summarizing your findings and drawing conclusions based on your research. You should also provide recommendations for future research and implications for practice.
73
+
74
+ Step-11:
75
+ Please include references in the form of citation and also link to the reference papers.
76
+
77
+ Your response should be formatted in HTML (but avoid showing these tags ```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.
78
+ """
79
+
80
+ # Extract text from each PDF
81
+ pdf_texts = []
82
+ pdf_names = []
83
+
84
+ for pdf_file in pdf_files:
85
+ if isinstance(pdf_file, str): # If it's already a path
86
+ pdf_path = pdf_file
87
+ else: # If it's a file object
88
+ pdf_path = pdf_file.name
89
+
90
+ pdf_name = os.path.basename(pdf_path)
91
+ pdf_text = extract_text_from_pdf(pdf_path)
92
+
93
+ pdf_texts.append(pdf_text)
94
+ pdf_names.append(pdf_name)
95
+
96
+ # Prepare the user prompt with the review question and instructions
97
+ table_instruction = ""
98
+ if include_tables:
99
+ table_instruction = " Please include important new generated tables in your review."
100
+
101
+ user_prompt = f"Please generate a systematic review of the following {len(pdf_files)} papers: {', '.join(pdf_names)}.{table_instruction}\n\nReview Question: {review_question}"
102
+
103
+ # Create the messages for the API call
104
+ messages = [
105
+ {"role": "system", "content": system_prompt},
106
+ {"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))])}
107
+ ]
108
+
109
+ # Call the API with temperature=0.7 and top_p=1
110
+ response = openai.ChatCompletion.create(
111
+ model="gpt-4.1",
112
+ messages=messages,
113
+ temperature=0.7,
114
+ top_p=1,
115
+ max_tokens=16384
116
+ )
117
+
118
+ # Get the AI response
119
+ review_content = response["choices"][0]["message"]["content"]
120
+
121
+ # Apply professional academic paper styling
122
+ styled_html = f"""
123
+ <!DOCTYPE html>
124
+ <html lang="en">
125
+ <head>
126
+ <meta charset="UTF-8">
127
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
128
+ <title>Systematic Review</title>
129
+ <style>
130
+ /* Academic Paper Styling */
131
+ body {{
132
+ font-family: 'Times New Roman', Times, serif;
133
+ line-height: 1.6;
134
+ color: #333;
135
+ margin: 0;
136
+ padding: 0;
137
+ background-color: #f9f9f9;
138
+ }}
139
+ .paper-container {{
140
+ max-width: 800px;
141
+ margin: 0 auto;
142
+ padding: 40px;
143
+ background-color: white;
144
+ box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
145
+ }}
146
+ header {{
147
+ text-align: center;
148
+ margin-bottom: 30px;
149
+ border-bottom: 1px solid #ddd;
150
+ padding-bottom: 20px;
151
+ }}
152
+ h1 {{
153
+ font-size: 24px;
154
+ margin: 0 0 15px;
155
+ font-weight: bold;
156
+ }}
157
+ .author-info {{
158
+ font-size: 14px;
159
+ margin-bottom: 15px;
160
+ }}
161
+ .abstract {{
162
+ font-style: italic;
163
+ margin: 20px 0;
164
+ padding: 15px;
165
+ background-color: #f8f8f8;
166
+ border-left: 3px solid #ccc;
167
+ }}
168
+ h2 {{
169
+ font-size: 18px;
170
+ margin: 30px 0 15px;
171
+ border-bottom: 1px solid #eee;
172
+ padding-bottom: 5px;
173
+ }}
174
+ h3 {{
175
+ font-size: 16px;
176
+ margin: 25px 0 10px;
177
+ }}
178
+ p {{
179
+ margin: 0 0 15px;
180
+ text-align: justify;
181
+ }}
182
+ .section {{
183
+ margin-bottom: 30px;
184
+ }}
185
+ table {{
186
+ width: 100%;
187
+ border-collapse: collapse;
188
+ margin: 20px 0;
189
+ font-size: 14px;
190
+ }}
191
+ table, th, td {{
192
+ border: 1px solid #ddd;
193
+ }}
194
+ th, td {{
195
+ padding: 10px;
196
+ text-align: left;
197
+ }}
198
+ th {{
199
+ background-color: #f2f2f2;
200
+ }}
201
+ tr:nth-child(even) {{
202
+ background-color: #f9f9f9;
203
+ }}
204
+ .citation {{
205
+ font-size: 14px;
206
+ color: #555;
207
+ }}
208
+ .reference-list {{
209
+ margin-top: 40px;
210
+ border-top: 1px solid #ddd;
211
+ padding-top: 20px;
212
+ }}
213
+ .reference-list h2 {{
214
+ margin-top: 0;
215
+ }}
216
+ .reference-item {{
217
+ margin-bottom: 10px;
218
+ padding-left: 25px;
219
+ text-indent: -25px;
220
+ }}
221
+ ul, ol {{
222
+ margin: 15px 0;
223
+ padding-left: 25px;
224
+ }}
225
+ li {{
226
+ margin-bottom: 5px;
227
+ }}
228
+ .figure {{
229
+ margin: 25px 0;
230
+ text-align: center;
231
+ }}
232
+ .figure img {{
233
+ max-width: 100%;
234
+ }}
235
+ .figure-caption {{
236
+ font-size: 14px;
237
+ color: #666;
238
+ margin-top: 10px;
239
+ }}
240
+ .footnote {{
241
+ font-size: 12px;
242
+ color: #777;
243
+ }}
244
+ @media print {{
245
+ body {{
246
+ background-color: white;
247
+ }}
248
+ .paper-container {{
249
+ box-shadow: none;
250
+ padding: 0;
251
+ }}
252
+ }}
253
+ </style>
254
+ </head>
255
+ <body>
256
+ <div class="paper-container">
257
+ {review_content}
258
+ </div>
259
+ </body>
260
+ </html>
261
+ """
262
+
263
+ return styled_html
264
+
265
+ except Exception as e:
266
+ return f"""
267
+ <div style="color: red; padding: 20px; border: 1px solid red; border-radius: 5px; background-color: #ffecec;">
268
+ <h3>Error Generating Systematic Review</h3>
269
+ <p>{str(e)}</p>
270
+ </div>
271
+ """
272
+
273
+ # Function to save uploaded files
274
+ def save_uploaded_files(files):
275
+ if not files:
276
+ return []
277
+
278
+ saved_paths = []
279
+ for file in files:
280
+ if file is not None:
281
+ # Create a temporary file
282
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
283
+ tmp_file.write(file)
284
+ saved_paths.append(tmp_file.name)
285
+
286
+ return saved_paths
287
+
288
+ # Function to create a downloadable HTML file
289
+ def create_html_download_link(html_content):
290
+ if not html_content or "<div style=\"color: red; padding: 20px;" in html_content or "Please upload at least one PDF file" in html_content:
291
+ return None
292
+
293
+ # Create timestamp for the filename
294
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
295
+ filename = f"systematic_review_{timestamp}.html"
296
+
297
+ # Encode the HTML content for download
298
+ b64_html = base64.b64encode(html_content.encode()).decode()
299
+ download_link = f'<a href="data:text/html;base64,{b64_html}" download="{filename}" class="download-button">Download HTML</a>'
300
+
301
+ return download_link
302
+
303
+ # Add CSS styling for the Gradio interface
304
+ custom_css = """
305
+ <style>
306
+ /* Main UI */
307
+ .gradio-container {
308
+ font-family: 'Arial', sans-serif;
309
+ background-color: #f9f9f9;
310
+ }
311
+
312
+ /* Header */
313
+ h1 {
314
+ font-size: 28px;
315
+ color: #333;
316
+ margin-bottom: 20px;
317
+ text-align: center;
318
+ padding-bottom: 10px;
319
+ border-bottom: 2px solid #4a00e0;
320
+ }
321
+
322
+ /* Primary Button */
323
+ #generate_button {
324
+ background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
325
+ color: white;
326
+ font-weight: bold;
327
+ padding: 10px 20px;
328
+ border-radius: 5px;
329
+ transition: all 0.3s ease;
330
+ }
331
+ #generate_button:hover {
332
+ background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%);
333
+ transform: translateY(-2px);
334
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
335
+ }
336
+
337
+ /* API Key Button */
338
+ #api_key_button {
339
+ background: linear-gradient(135deg, #68d391 0%, #48bb78 100%);
340
+ color: white;
341
+ font-weight: bold;
342
+ margin-top: 27px;
343
+ padding: 10px 20px;
344
+ border-radius: 5px;
345
+ transition: all 0.3s ease;
346
+ }
347
+ #api_key_button:hover {
348
+ background: linear-gradient(135deg, #38a169 0%, #68d391 100%);
349
+ transform: translateY(-2px);
350
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
351
+ }
352
+
353
+ /* Form Elements */
354
+ .input-container {
355
+ background-color: white;
356
+ padding: 20px;
357
+ border-radius: 8px;
358
+ box-shadow: 0 2px 10px rgba(0,0,0,0.05);
359
+ margin-bottom: 20px;
360
+ }
361
+
362
+ /* Labels */
363
+ label {
364
+ font-weight: 600;
365
+ color: #555;
366
+ margin-bottom: 8px;
367
+ }
368
+
369
+ /* Instructions Accordion */
370
+ .accordion {
371
+ background-color: white;
372
+ border: 1px solid #e0e0e0;
373
+ border-radius: 8px;
374
+ margin-bottom: 20px;
375
+ }
376
+
377
+ /* Output Container */
378
+ .output-container {
379
+ background-color: white;
380
+ padding: 15px;
381
+ border-radius: 8px;
382
+ box-shadow: 0 2px 10px rgba(0,0,0,0.05);
383
+ }
384
+
385
+ /* File Upload Area */
386
+ .file-upload {
387
+ border: 2px dashed #ccc;
388
+ border-radius: 5px;
389
+ padding: 20px;
390
+ text-align: center;
391
+ margin-bottom: 20px;
392
+ }
393
+
394
+ /* Download Button */
395
+ .download-button {
396
+ display: inline-block;
397
+ background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%);
398
+ color: white;
399
+ font-weight: bold;
400
+ padding: 8px 16px;
401
+ border-radius: 4px;
402
+ text-decoration: none;
403
+ margin-bottom: 10px;
404
+ transition: all 0.3s ease;
405
+ }
406
+ .download-button:hover {
407
+ background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%);
408
+ transform: translateY(-2px);
409
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
410
+ }
411
+
412
+ /* Download Container */
413
+ #download-container {
414
+ display: flex;
415
+ justify-content: center;
416
+ margin: 20px 0;
417
+ padding: 15px;
418
+ background-color: #f5f5f5;
419
+ border-radius: 8px;
420
+ }
421
+
422
+ /* Responsive adjustments */
423
+ @media screen and (max-width: 768px) {
424
+ .gradio-container {
425
+ padding: 10px;
426
+ }
427
+ }
428
+ </style>
429
+ """
430
+
431
+ # Gradio UI Layout with improved styling
432
+ with gr.Blocks(css=custom_css) as demo:
433
+ gr.Markdown("# Systematic Review Generator for Research Papers")
434
+
435
+ with gr.Accordion("How to Use This App", open=False):
436
+ gr.Markdown("""
437
+ ### Getting Started:
438
+ 1. Enter your OpenAI API key in the field below and click "Set API Key"
439
+ 2. Upload multiple PDF research papers (2 or more recommended)
440
+ 3. Enter your review question or topic
441
+ 4. Check the "Include Tables" option if you want the review to include comparison tables
442
+ 5. Click "Generate Systematic Review" to start the process
443
+ 6. After generation, you can download the review as HTML
444
+
445
+ ### Tips for Best Results:
446
+ - Upload papers that are related to the same research topic or field
447
+ - Be specific in your review question to get more focused results
448
+ - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
449
+ - The more papers you upload, the more comprehensive the review will be
450
+ - The review will be formatted as a professional academic paper with proper sections and citations
451
+ """)
452
+
453
+ # API Key Input in a styled container
454
+ with gr.Row(elem_classes="input-container"):
455
+ with gr.Column(scale=3):
456
+ api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password", placeholder="sk-...")
457
+ with gr.Column(scale=1):
458
+ api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
459
+ api_key_output = gr.Textbox(label="API Key Status", interactive=False)
460
+
461
+ # PDF Upload and Review Settings
462
+ with gr.Row(elem_classes="input-container"):
463
+ with gr.Column():
464
+ gr.Markdown("### Upload Research Papers")
465
+ pdf_files = gr.File(label="Upload PDF Research Papers", file_count="multiple", type="binary", elem_classes="file-upload")
466
+ review_question = gr.Textbox(
467
+ label="Review Question or Topic",
468
+ value="Please generate a systematic review of the following papers.",
469
+ placeholder="e.g., What are the effects of mindfulness meditation on stress reduction?"
470
+ )
471
+ include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
472
+ generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
473
+
474
+ # Download link container
475
+ download_html_output = gr.HTML(label="Download Options")
476
+
477
+ # Output with improved styling
478
+ with gr.Row(elem_classes="output-container"):
479
+ review_output = gr.HTML(label="Systematic Review")
480
+
481
+ # Button actions
482
+ api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
483
+
484
+ # Generate systematic review
485
+ def process_files_and_generate_review(files, question, include_tables):
486
+ if not files:
487
+ return ("""
488
+ <div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
489
+ <h3 style="color: #666;">Please upload at least one PDF file.</h3>
490
+ <p>To generate a systematic review, upload one or more research papers in PDF format.</p>
491
+ </div>
492
+ """, "")
493
+
494
+ # Save uploaded files
495
+ saved_paths = save_uploaded_files(files)
496
+
497
+ # Show loading message
498
+ loading_message = """
499
+ <div style="padding: 20px; text-align: center;">
500
+ <h3>Generating Systematic Review...</h3>
501
+ <p>This may take a few minutes depending on the number and size of papers.</p>
502
+ <div style="width: 100%; height: 4px; background-color: #f0f0f0; margin: 20px 0; border-radius: 2px; overflow: hidden;">
503
+ <div style="width: 30%; height: 100%; background: linear-gradient(90deg, #4a00e0, #8e2de2); animation: progress 2s infinite linear;"></div>
504
+ </div>
505
+ <style>
506
+ @keyframes progress {
507
+ 0% { margin-left: -30%; }
508
+ 100% { margin-left: 100%; }
509
+ }
510
+ </style>
511
+ </div>
512
+ """
513
+
514
+ yield loading_message, ""
515
+
516
+ # Generate review
517
+ review = generate_systematic_review(saved_paths, question, include_tables)
518
+
519
+ # Create HTML download link
520
+ html_link = create_html_download_link(review)
521
+
522
+ # Create download link HTML
523
+ download_link = f"""
524
+ <div id="download-container">
525
+ <div>
526
+ <h3>Download Option:</h3>
527
+ {html_link or ""}
528
+ </div>
529
+ </div>
530
+ """
531
+
532
+ # Clean up temporary files
533
+ for path in saved_paths:
534
+ try:
535
+ os.remove(path)
536
+ except:
537
+ pass
538
+
539
+ yield review, download_link
540
+
541
+ generate_button.click(
542
+ process_files_and_generate_review,
543
+ inputs=[pdf_files, review_question, include_tables],
544
+ outputs=[review_output, download_html_output]
545
+ )
546
+
547
+ # Launch the app
548
+ if __name__ == "__main__":
549
+ demo.launch(share=True)