shukdevdatta123 commited on
Commit
fb22ee6
·
verified ·
1 Parent(s): 67b8403

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -8,8 +8,7 @@ def set_api_key(api_key):
8
  if api_key:
9
  openai.api_key = api_key
10
  return "API Key Set"
11
- else:
12
- return "API Key not set"
13
 
14
  # PDF text extraction function
15
  def extract_text_from_pdf(pdf_path):
@@ -24,19 +23,23 @@ def extract_text_from_pdf(pdf_path):
24
  # Review generator function
25
  def generate_review(api_key, uploaded_files, review_question, include_tables):
26
  if not api_key:
27
- return "Please enter your OpenAI API key."
28
  if not uploaded_files:
29
- return "Please upload at least one PDF file."
30
  if not review_question:
31
- return "Please enter a review question."
32
 
33
- # File type validation (redundant but kept for robustness)
 
 
 
 
34
  for file in uploaded_files:
35
- if not file.lower().endswith('.pdf'):
36
- return f"Invalid file type. Please upload a PDF file, but '{os.path.basename(file)}' is not a PDF."
37
 
38
  system_prompt = """
39
- 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:
40
  Step 1: Identify a Research Field
41
  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.
42
  Step 2: Generate a Research Question
@@ -66,7 +69,7 @@ def generate_review(api_key, uploaded_files, review_question, include_tables):
66
  # Check for extraction errors
67
  for i, text in enumerate(texts):
68
  if text.startswith("Error extracting text"):
69
- return text
70
 
71
  table_note = " Include relevant tables to compare methodologies, results, and limitations." if include_tables else ""
72
  user_prompt = (
@@ -77,36 +80,35 @@ def generate_review(api_key, uploaded_files, review_question, include_tables):
77
 
78
  try:
79
  response = openai.ChatCompletion.create(
80
- model="gpt-4", # Updated from "gpt-4.1" to a valid model
81
  messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
82
  temperature=0.7,
83
  top_p=1,
84
  max_tokens=8192
85
  )
86
  review_html = response["choices"][0]["message"]["content"]
87
- download_button = gr.File(file_name="systematic_review.html", data=review_html)
88
 
89
- return [review_html, download_button]
90
  except Exception as e:
91
- return f"Error generating systematic review: {e}"
92
 
93
  # Gradio Interface
94
- api_key_input = gr.Textbox(label="API Key", type="password", placeholder="Enter OpenAI API Key", lines=1)
95
- file_input = gr.File(label="Upload PDF Research Papers", file_count="multiple", file_types=["pdf"])
96
- review_question_input = gr.Textbox(label="Review Question or Topic", placeholder="Please Generate a systematic review of the following papers.")
97
- include_tables_input = gr.Checkbox(label="Include Comparison Tables", value=True)
98
-
99
- output_review = gr.HTML(label="Generated Systematic Review")
100
- output_download = gr.File(label="Download Review as .html")
101
-
102
- # Create Interface
103
- interface = gr.Interface(
104
- fn=generate_review,
105
- inputs=[api_key_input, file_input, review_question_input, include_tables_input],
106
- outputs=[output_review, output_download],
107
- live=True,
108
- title="Systematic Review Generator for Research Papers",
109
- description="Generate a polished and structured systematic review from multiple uploaded research papers."
110
- )
111
 
112
  interface.launch()
 
8
  if api_key:
9
  openai.api_key = api_key
10
  return "API Key Set"
11
+ return "API Key not set"
 
12
 
13
  # PDF text extraction function
14
  def extract_text_from_pdf(pdf_path):
 
23
  # Review generator function
24
  def generate_review(api_key, uploaded_files, review_question, include_tables):
25
  if not api_key:
26
+ return "Please enter your OpenAI API key.", None
27
  if not uploaded_files:
28
+ return "Please upload at least one PDF file.", None
29
  if not review_question:
30
+ return "Please enter a review question.", None
31
 
32
+ # Ensure uploaded_files is a list of file paths
33
+ if not isinstance(uploaded_files, list):
34
+ uploaded_files = [uploaded_files]
35
+
36
+ # Validate file types
37
  for file in uploaded_files:
38
+ if not isinstance(file, str) or not file.lower().endswith('.pdf'):
39
+ return f"Invalid file type detected. Please upload only PDF files. Problematic file: '{file}'", None
40
 
41
  system_prompt = """
42
+ 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 detail. Structure it using these steps:
43
  Step 1: Identify a Research Field
44
  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.
45
  Step 2: Generate a Research Question
 
69
  # Check for extraction errors
70
  for i, text in enumerate(texts):
71
  if text.startswith("Error extracting text"):
72
+ return text, None
73
 
74
  table_note = " Include relevant tables to compare methodologies, results, and limitations." if include_tables else ""
75
  user_prompt = (
 
80
 
81
  try:
82
  response = openai.ChatCompletion.create(
83
+ model="gpt-4",
84
  messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
85
  temperature=0.7,
86
  top_p=1,
87
  max_tokens=8192
88
  )
89
  review_html = response["choices"][0]["message"]["content"]
90
+ download_button = review_html.encode('utf-8') # For file download
91
 
92
+ return review_html, download_button
93
  except Exception as e:
94
+ return f"Error generating systematic review: {e}", None
95
 
96
  # Gradio Interface
97
+ with gr.Blocks(title="Systematic Review Generator for Research Papers") as interface:
98
+ gr.Markdown("# Systematic Review Generator\nUpload PDF research papers to generate a structured systematic review.")
99
+
100
+ api_key_input = gr.Textbox(label="API Key", type="password", placeholder="Enter OpenAI API Key")
101
+ file_input = gr.File(label="Upload PDF Research Papers", file_count="multiple", file_types=["pdf"])
102
+ review_question_input = gr.Textbox(label="Review Question or Topic", placeholder="E.g., What are the impacts of climate change on agriculture?")
103
+ include_tables_input = gr.Checkbox(label="Include Comparison Tables", default=True)
104
+
105
+ output_review = gr.HTML(label="Generated Systematic Review")
106
+ output_download = gr.File(label="Download Review as .html")
107
+
108
+ gr.Button("Generate Review").click(
109
+ fn=generate_review,
110
+ inputs=[api_key_input, file_input, review_question_input, include_tables_input],
111
+ outputs=[output_review, output_download]
112
+ )
 
113
 
114
  interface.launch()