shukdevdatta123 commited on
Commit
06265a9
·
verified ·
1 Parent(s): 4f49a0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -67
app.py CHANGED
@@ -5,7 +5,6 @@ import os
5
  import tempfile
6
  import base64
7
  from datetime import datetime
8
- import re
9
 
10
  # Variable to store API key
11
  api_key = ""
@@ -26,7 +25,7 @@ def extract_text_from_pdf(pdf_path):
26
  return f"Error extracting text from PDF: {str(e)}"
27
 
28
  # Function to interact with OpenAI API for systematic review
29
- def generate_systematic_review(pdf_files, review_question, include_tables=True, include_figures=True):
30
  if not api_key:
31
  return "Please enter your OpenAI API key first."
32
 
@@ -98,15 +97,11 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True,
98
  pdf_names.append(pdf_name)
99
 
100
  # Prepare the user prompt with the review question and instructions
101
- visual_instruction = ""
102
- if include_tables and include_figures:
103
- visual_instruction = " Please include important new generated tables and create essential figures (using SVG format) to illustrate key findings, trends, or conceptual frameworks in your review. Create at least 2-3 figures that help visualize the most important insights."
104
- elif include_tables:
105
- visual_instruction = " Please include important new generated tables in your review."
106
- elif include_figures:
107
- visual_instruction = " Please create essential figures (using SVG format) to illustrate key findings, trends, or conceptual frameworks in your review. Create at least 2-3 figures that help visualize the most important insights."
108
 
109
- user_prompt = f"Please generate a systematic review of the following {len(pdf_files)} papers: {', '.join(pdf_names)}.{visual_instruction}\n\nReview Question: {review_question}"
110
 
111
  # Create the messages for the API call
112
  messages = [
@@ -126,9 +121,6 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True,
126
  # Get the AI response
127
  review_content = response["choices"][0]["message"]["content"]
128
 
129
- # Process and replace any SVG content with properly formatted SVG
130
- review_content = process_svg_content(review_content)
131
-
132
  # Apply professional academic paper styling
133
  styled_html = f"""
134
  <!DOCTYPE html>
@@ -240,26 +232,18 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True,
240
  margin: 25px 0;
241
  text-align: center;
242
  }}
243
- .figure svg {{
244
  max-width: 100%;
245
- height: auto;
246
  }}
247
  .figure-caption {{
248
  font-size: 14px;
249
  color: #666;
250
  margin-top: 10px;
251
- text-align: center;
252
  }}
253
  .footnote {{
254
  font-size: 12px;
255
  color: #777;
256
  }}
257
- svg {{
258
- display: block;
259
- margin: 0 auto;
260
- max-width: 100%;
261
- height: auto;
262
- }}
263
  @media print {{
264
  body {{
265
  background-color: white;
@@ -289,33 +273,6 @@ def generate_systematic_review(pdf_files, review_question, include_tables=True,
289
  </div>
290
  """
291
 
292
- # Function to clean up and format SVG content
293
- def process_svg_content(content):
294
- # Find SVG blocks
295
- svg_pattern = r'```svg\s*([\s\S]*?)\s*```'
296
-
297
- def replace_svg(match):
298
- svg_content = match.group(1).strip()
299
- # Ensure proper SVG structure
300
- if not svg_content.startswith('<svg'):
301
- svg_content = f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 500">{svg_content}</svg>'
302
-
303
- # Wrap in figure div with caption if not already
304
- if '<div class="figure">' not in svg_content:
305
- figure_number = len(re.findall(r'<div class="figure">', content[:match.start()])) + 1
306
- return f"""
307
- <div class="figure">
308
- {svg_content}
309
- <div class="figure-caption">Figure {figure_number}: Generated visualization of key findings.</div>
310
- </div>
311
- """
312
- return svg_content
313
-
314
- # Replace SVG code blocks with properly formatted SVG elements
315
- processed_content = re.sub(svg_pattern, replace_svg, content)
316
-
317
- return processed_content
318
-
319
  # Function to save uploaded files
320
  def save_uploaded_files(files):
321
  if not files:
@@ -465,13 +422,6 @@ custom_css = """
465
  border-radius: 8px;
466
  }
467
 
468
- /* Checkbox Group */
469
- .checkbox-group {
470
- display: flex;
471
- gap: 20px;
472
- margin: 15px 0;
473
- }
474
-
475
  /* Responsive adjustments */
476
  @media screen and (max-width: 768px) {
477
  .gradio-container {
@@ -491,7 +441,7 @@ with gr.Blocks(css=custom_css) as demo:
491
  1. Enter your OpenAI API key in the field below and click "Set API Key"
492
  2. Upload multiple PDF research papers (2 or more recommended)
493
  3. Enter your review question or topic
494
- 4. Choose visual elements to include (tables and/or figures)
495
  5. Click "Generate Systematic Review" to start the process
496
  6. After generation, you can download the review as HTML
497
 
@@ -500,8 +450,6 @@ with gr.Blocks(css=custom_css) as demo:
500
  - Be specific in your review question to get more focused results
501
  - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
502
  - The more papers you upload, the more comprehensive the review will be
503
- - Tables help organize and compare findings across studies
504
- - Figures help visualize key trends, frameworks, or relationships identified in the review
505
  - The review will be formatted as a professional academic paper with proper sections and citations
506
  """)
507
 
@@ -523,11 +471,7 @@ with gr.Blocks(css=custom_css) as demo:
523
  value="Please generate a systematic review of the following papers.",
524
  placeholder="e.g., What are the effects of mindfulness meditation on stress reduction?"
525
  )
526
-
527
- with gr.Row(elem_classes="checkbox-group"):
528
- include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
529
- include_figures = gr.Checkbox(label="Include Visual Figures", value=True)
530
-
531
  generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
532
 
533
  # Download link container
@@ -541,7 +485,7 @@ with gr.Blocks(css=custom_css) as demo:
541
  api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
542
 
543
  # Generate systematic review
544
- def process_files_and_generate_review(files, question, include_tables, include_figures):
545
  if not files:
546
  return ("""
547
  <div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
@@ -573,7 +517,7 @@ with gr.Blocks(css=custom_css) as demo:
573
  yield loading_message, ""
574
 
575
  # Generate review
576
- review = generate_systematic_review(saved_paths, question, include_tables, include_figures)
577
 
578
  # Create HTML download link
579
  html_link = create_html_download_link(review)
@@ -599,7 +543,7 @@ with gr.Blocks(css=custom_css) as demo:
599
 
600
  generate_button.click(
601
  process_files_and_generate_review,
602
- inputs=[pdf_files, review_question, include_tables, include_figures],
603
  outputs=[review_output, download_html_output]
604
  )
605
 
 
5
  import tempfile
6
  import base64
7
  from datetime import datetime
 
8
 
9
  # Variable to store API key
10
  api_key = ""
 
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
 
 
97
  pdf_names.append(pdf_name)
98
 
99
  # Prepare the user prompt with the review question and instructions
100
+ table_instruction = ""
101
+ if include_tables:
102
+ table_instruction = " Please include important new generated tables in your review."
 
 
 
 
103
 
104
+ 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}"
105
 
106
  # Create the messages for the API call
107
  messages = [
 
121
  # Get the AI response
122
  review_content = response["choices"][0]["message"]["content"]
123
 
 
 
 
124
  # Apply professional academic paper styling
125
  styled_html = f"""
126
  <!DOCTYPE html>
 
232
  margin: 25px 0;
233
  text-align: center;
234
  }}
235
+ .figure img {{
236
  max-width: 100%;
 
237
  }}
238
  .figure-caption {{
239
  font-size: 14px;
240
  color: #666;
241
  margin-top: 10px;
 
242
  }}
243
  .footnote {{
244
  font-size: 12px;
245
  color: #777;
246
  }}
 
 
 
 
 
 
247
  @media print {{
248
  body {{
249
  background-color: white;
 
273
  </div>
274
  """
275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  # Function to save uploaded files
277
  def save_uploaded_files(files):
278
  if not files:
 
422
  border-radius: 8px;
423
  }
424
 
 
 
 
 
 
 
 
425
  /* Responsive adjustments */
426
  @media screen and (max-width: 768px) {
427
  .gradio-container {
 
441
  1. Enter your OpenAI API key in the field below and click "Set API Key"
442
  2. Upload multiple PDF research papers (2 or more recommended)
443
  3. Enter your review question or topic
444
+ 4. Check the "Include Tables" option if you want the review to include comparison tables
445
  5. Click "Generate Systematic Review" to start the process
446
  6. After generation, you can download the review as HTML
447
 
 
450
  - Be specific in your review question to get more focused results
451
  - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
452
  - The more papers you upload, the more comprehensive the review will be
 
 
453
  - The review will be formatted as a professional academic paper with proper sections and citations
454
  """)
455
 
 
471
  value="Please generate a systematic review of the following papers.",
472
  placeholder="e.g., What are the effects of mindfulness meditation on stress reduction?"
473
  )
474
+ include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
 
 
 
 
475
  generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button", size="large")
476
 
477
  # Download link container
 
485
  api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
486
 
487
  # Generate systematic review
488
+ def process_files_and_generate_review(files, question, include_tables):
489
  if not files:
490
  return ("""
491
  <div style="padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px; background-color: #f9f9f9;">
 
517
  yield loading_message, ""
518
 
519
  # Generate review
520
+ review = generate_systematic_review(saved_paths, question, include_tables)
521
 
522
  # Create HTML download link
523
  html_link = create_html_download_link(review)
 
543
 
544
  generate_button.click(
545
  process_files_and_generate_review,
546
+ inputs=[pdf_files, review_question, include_tables],
547
  outputs=[review_output, download_html_output]
548
  )
549