shukdevdatta123 commited on
Commit
3bcebbc
·
verified ·
1 Parent(s): fb22ee6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +242 -86
app.py CHANGED
@@ -1,114 +1,270 @@
1
  import gradio as gr
2
  import openai
3
- import fitz # PyMuPDF
4
  import os
 
5
 
6
- # API Key input and status
7
- def set_api_key(api_key):
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):
15
  try:
16
  doc = fitz.open(pdf_path)
17
  text = "\n".join([page.get_text("text") for page in doc])
18
- doc.close()
19
  return text
20
  except Exception as e:
21
- return f"Error extracting text from {os.path.basename(pdf_path)}: {e}"
22
 
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
46
- 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).
47
- Step 3: Create a Protocol
48
- After generating your research question, the next step is to create a protocol. A protocol is 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.
49
- Step 4: Evaluate Relevant Literature
50
- 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.
51
- Step 5: Investigate Sources for Answers
52
- 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. This may include conducting interviews, surveys, or experiments, or analyzing existing data.
53
- Step 6: Collect Data as per Protocol
54
- 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.
55
- Step 7: Data Extraction
56
- 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.
57
- Step 8: Critical Analysis of Results
58
- 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.
59
- Step 9: Interpreting Derivations
60
- 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.
61
- Step 10: Concluding Statements
62
- 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.
63
- By following these steps, you can ensure that your systematic review paper is well-written, well-organized, and provides valuable insights into your research question.
64
- """
65
-
66
- filenames = [os.path.basename(file) for file in uploaded_files]
67
- texts = [extract_text_from_pdf(file) for file in uploaded_files]
68
-
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 = (
76
- f"Generate a polished and structured systematic review in HTML using the following papers: {', '.join(filenames)}.\n"
77
- f"Review Question: {review_question}.{table_note}\n\n"
78
- + "\n\n".join([f"Paper {i+1}: {filenames[i]}\n{texts[i]}" for i in range(len(texts))])
79
- )
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import openai
3
+ import fitz # PyMuPDF for PDF processing
4
  import os
5
+ import tempfile
6
 
7
+ # Variable to store API key
8
+ api_key = ""
9
+
10
+ # Function to update API key
11
+ def set_api_key(key):
12
+ global api_key
13
+ api_key = key
14
+ return "API Key Set Successfully!"
15
 
16
+ # Function to extract text from PDF
17
  def extract_text_from_pdf(pdf_path):
18
  try:
19
  doc = fitz.open(pdf_path)
20
  text = "\n".join([page.get_text("text") for page in doc])
 
21
  return text
22
  except Exception as e:
23
+ return f"Error extracting text from PDF: {str(e)}"
24
 
25
+ # Function to interact with OpenAI API for systematic review
26
+ def generate_systematic_review(pdf_files, review_question, include_tables=True):
27
  if not api_key:
28
+ return "<p>Please enter your OpenAI API key first.</p>"
 
 
 
 
29
 
30
+ if not pdf_files:
31
+ return "<p>Please upload at least one PDF file.</p>"
 
32
 
33
+ if not review_question:
34
+ return "<p>Please enter a review question.</p>"
 
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  try:
37
+ openai.api_key = api_key
38
+
39
+ # Create the system message with systematic review guidelines
40
+ system_prompt = """
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
+
44
+ Step 2: Generate a Research Question
45
+ 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).
46
+
47
+ Step 3: Create a Protocol
48
+ 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.
49
+
50
+ Step 4: Evaluate Relevant Literature
51
+ 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.
52
+
53
+ Step 5: Investigate Sources for Answers
54
+ 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.
55
+
56
+ Step 6: Collect Data as per Protocol
57
+ 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.
58
+
59
+ Step 7: Data Extraction
60
+ 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.
61
+
62
+ Step 8: Critical Analysis of Results
63
+ 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.
64
+
65
+ Step 9: Interpreting Derivations
66
+ 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.
67
+
68
+ Step 10: Concluding Statements
69
+ 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.
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
76
+ pdf_texts = []
77
+ pdf_names = []
78
+
79
+ for pdf_file in pdf_files:
80
+ if isinstance(pdf_file, str): # If it's already a path
81
+ pdf_path = pdf_file
82
+ else: # If it's a file object
83
+ pdf_path = pdf_file.name
84
+
85
+ pdf_name = os.path.basename(pdf_path)
86
+ pdf_text = extract_text_from_pdf(pdf_path)
87
+
88
+ pdf_texts.append(pdf_text)
89
+ pdf_names.append(pdf_name)
90
+
91
+ # Prepare the user prompt with the review question and instructions
92
+ table_instruction = ""
93
+ if include_tables:
94
+ table_instruction = " Please include important new generated tables in your review."
95
+
96
+ 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}"
97
+
98
+ # Create the messages for the API call
99
+ messages = [
100
+ {"role": "system", "content": system_prompt},
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,
108
+ temperature=1,
109
  top_p=1,
110
+ max_tokens=2048
111
  )
112
+
113
+ # Generate HTML formatted response
114
+ review_content = response["choices"][0]["message"]["content"]
115
+
116
+ # Convert the content into HTML format
117
+ html_response = f"""
118
+ <h2>Systematic Review on {review_question}</h2>
119
+ <p><strong>Research Papers:</strong> {', '.join(pdf_names)}</p>
120
+ <h3>Review Process</h3>
121
+ <ul>
122
+ <li>Identified Research Field</li>
123
+ <li>Research Question Formulation</li>
124
+ <li>Protocol Creation</li>
125
+ <li>Literature Evaluation</li>
126
+ <li>Source Investigation</li>
127
+ <li>Data Collection</li>
128
+ <li>Data Extraction</li>
129
+ <li>Critical Analysis of Results</li>
130
+ <li>Interpretation of Derivations</li>
131
+ <li>Concluding Statements</li>
132
+ </ul>
133
+ <h3>Generated Review:</h3>
134
+ <p>{review_content}</p>
135
+ """
136
 
137
+ # If tables are included in the review
138
+ if include_tables:
139
+ html_response += """
140
+ <h3>Comparison Tables</h3>
141
+ <table border="1">
142
+ <tr>
143
+ <th>Study</th>
144
+ <th>Key Findings</th>
145
+ <th>Comparison</th>
146
+ </tr>
147
+ <!-- Include tables here -->
148
+ <tr>
149
+ <td>Study 1</td>
150
+ <td>Summary of findings</td>
151
+ <td>Comparison with other studies</td>
152
+ </tr>
153
+ </table>
154
+ """
155
+
156
+ return html_response
157
+
158
  except Exception as e:
159
+ return f"<p>Error generating systematic review: {str(e)}</p>"
160
+
161
+ # Function to save uploaded files
162
+ def save_uploaded_files(files):
163
+ if not files:
164
+ return []
165
+
166
+ saved_paths = []
167
+ for file in files:
168
+ if file is not None:
169
+ # Create a temporary file
170
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
171
+ tmp_file.write(file)
172
+ saved_paths.append(tmp_file.name)
173
+
174
+ return saved_paths
175
 
176
+ # Gradio UI Layout
177
+ with gr.Blocks() as demo:
178
+ gr.Markdown("# Systematic Review Generator for Research Papers")
179
 
180
+ with gr.Accordion("How to Use This App", open=True):
181
+ gr.Markdown("""
182
+ ### Getting Started:
183
+ 1. Enter your OpenAI API key in the field below and click "Set API Key"
184
+ 2. Upload multiple PDF research papers (2 or more recommended)
185
+ 3. Enter your review question or topic
186
+ 4. Check the "Include Tables" option if you want the review to include comparison tables
187
+ 5. Click "Generate Systematic Review" to start the process
188
+
189
+ ### Tips:
190
+ - For best results, upload papers that are related to the same research topic or field
191
+ - Be specific in your review question to get more focused results
192
+ - The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
193
+ - The more papers you upload, the more comprehensive the review will be
194
+ """)
195
 
196
+ # API Key Input
197
+ with gr.Row():
198
+ api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
199
+ api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
200
+ api_key_output = gr.Textbox(label="API Key Status", interactive=False)
201
 
202
+ # PDF Upload and Review Settings
203
+ with gr.Row():
204
+ with gr.Column():
205
+ pdf_files = gr.File(label="Upload PDF Research Papers", file_count="multiple", type="binary")
206
+ review_question = gr.Textbox(label="Review Question or Topic", placeholder="What are the current advances in GAN applications for speech processing?")
207
+ include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
208
+ generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button")
209
+
210
+ # Output
211
+ review_output = gr.HTML(label="Systematic Review", interactive=False)
212
+
213
+ # Button actions
214
+ api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
215
+
216
+ # Generate systematic review
217
+ def process_files_and_generate_review(files, question, include_tables):
218
+ if not files:
219
+ return "<p>Please upload at least one PDF file.</p>"
220
+
221
+ # Save uploaded files
222
+ saved_paths = save_uploaded_files(files)
223
+
224
+ # Generate review
225
+ review = generate_systematic_review(saved_paths, question, include_tables)
226
+
227
+ # Clean up temporary files
228
+ for path in saved_paths:
229
+ try:
230
+ os.remove(path)
231
+ except:
232
+ pass
233
+
234
+ return review
235
+
236
+ generate_button.click(
237
+ process_files_and_generate_review,
238
+ inputs=[pdf_files, review_question, include_tables],
239
+ outputs=[review_output]
240
  )
241
 
242
+ # Add CSS styling
243
+ css = """
244
+ <style>
245
+ #generate_button {
246
+ background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%); /* Purple gradient */
247
+ color: white;
248
+ font-weight: bold;
249
+ }
250
+ #generate_button:hover {
251
+ background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%); /* Slightly lighter */
252
+ }
253
+ #api_key_button {
254
+ background: linear-gradient(135deg, #68d391 0%, #48bb78 100%); /* Green gradient */
255
+ color: white;
256
+ font-weight: bold;
257
+ margin-top: 27px;
258
+ }
259
+ #api_key_button:hover {
260
+ background: linear-gradient(135deg, #38a169 0%, #68d391 100%); /* Slightly darker green */
261
+ }
262
+ .gradio-container {
263
+ max-width: 1200px !important;
264
+ }
265
+ </style>
266
+ """
267
+
268
+ # Launch the app
269
+ if __name__ == "__main__":
270
+ demo.launch(share=True)