Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import base64
|
3 |
import os
|
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
@@ -15,324 +15,123 @@ def generate_systematic_review(api_key, pdf_files):
|
|
15 |
str: Generated systematic review text
|
16 |
"""
|
17 |
if not api_key.strip():
|
18 |
-
return ""
|
19 |
-
<div class="error-message">
|
20 |
-
<h3>Error</h3>
|
21 |
-
<p>Please provide a valid OpenAI API key.</p>
|
22 |
-
</div>
|
23 |
-
"""
|
24 |
|
25 |
if not pdf_files:
|
26 |
-
return ""
|
27 |
-
<div class="error-message">
|
28 |
-
<h3>Error</h3>
|
29 |
-
<p>Please upload at least one PDF file.</p>
|
30 |
-
</div>
|
31 |
-
"""
|
32 |
|
33 |
try:
|
34 |
# Initialize OpenAI client with the provided API key
|
35 |
client = OpenAI(api_key=api_key)
|
36 |
|
37 |
-
# Create a list to hold file inputs for the API
|
38 |
-
file_inputs = []
|
39 |
-
|
40 |
-
# List of uploaded file names for display
|
41 |
-
file_names = []
|
42 |
-
|
43 |
-
# Process each uploaded PDF file
|
44 |
-
for pdf_file in pdf_files:
|
45 |
-
file_name = os.path.basename(pdf_file.name)
|
46 |
-
file_names.append(file_name)
|
47 |
-
|
48 |
-
# Read the file as binary data
|
49 |
-
with open(pdf_file.name, "rb") as f:
|
50 |
-
binary_data = f.read()
|
51 |
-
|
52 |
-
# Encode the binary data to base64
|
53 |
-
base64_encoded = base64.b64encode(binary_data).decode('utf-8')
|
54 |
-
|
55 |
-
# Create proper data URL with MIME type
|
56 |
-
data_url = f"data:application/pdf;base64,{base64_encoded}"
|
57 |
-
|
58 |
-
# Add to file inputs
|
59 |
-
file_inputs.append({
|
60 |
-
"type": "input_file",
|
61 |
-
"filename": file_name,
|
62 |
-
"file_data": data_url
|
63 |
-
})
|
64 |
-
|
65 |
# System prompt defining systematic review steps
|
66 |
system_prompt = """Step 1: Identify a Research Field
|
67 |
-
|
68 |
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.
|
69 |
Step 2: Generate a Research Question
|
70 |
-
|
71 |
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).
|
72 |
Step 3: Create a Protocol
|
73 |
-
|
74 |
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.
|
75 |
Step 4: Evaluate Relevant Literature
|
76 |
-
|
77 |
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.
|
78 |
Step 5: Investigate Sources for Answers
|
79 |
-
|
80 |
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.
|
81 |
Step 6: Collect Data as per Protocol
|
82 |
-
|
83 |
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.
|
84 |
Step 7: Data Extraction
|
85 |
-
|
86 |
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.
|
87 |
Step 8: Critical Analysis of Results
|
88 |
-
|
89 |
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.
|
90 |
Step 9: Interpreting Derivations
|
91 |
-
|
92 |
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.
|
93 |
Step 10: Concluding Statements
|
94 |
-
|
95 |
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.
|
96 |
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.
|
97 |
"""
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
# Make the API call to OpenAI
|
100 |
-
response = client.
|
101 |
-
model="gpt-4
|
102 |
-
|
103 |
-
{
|
104 |
-
"role": "system",
|
105 |
-
"content": [
|
106 |
-
{
|
107 |
-
"type": "input_text",
|
108 |
-
"text": system_prompt
|
109 |
-
}
|
110 |
-
]
|
111 |
-
},
|
112 |
-
{
|
113 |
-
"role": "user",
|
114 |
-
"content": [
|
115 |
-
{
|
116 |
-
"type": "input_text",
|
117 |
-
"text": "Please generate the systematic review of these papers (include also important new generated tables)"
|
118 |
-
},
|
119 |
-
*file_inputs
|
120 |
-
]
|
121 |
-
}
|
122 |
-
],
|
123 |
temperature=0.7,
|
124 |
-
|
125 |
top_p=1
|
126 |
)
|
127 |
|
128 |
# Extract and return the review text from the response
|
129 |
-
if
|
130 |
-
result_text =
|
131 |
-
for item in response.content:
|
132 |
-
if hasattr(item, 'text'):
|
133 |
-
result_text += item.text
|
134 |
-
|
135 |
if result_text:
|
136 |
-
|
137 |
-
files_html = ""
|
138 |
-
for name in file_names:
|
139 |
-
files_html += f'<div class="file-pill"><span class="file-icon">📄</span> {name} <span class="file-x">×</span></div>'
|
140 |
-
|
141 |
-
# Create a nicely formatted response interface
|
142 |
-
return f"""
|
143 |
-
<div class="response-container">
|
144 |
-
<div class="files-container">
|
145 |
-
{files_html}
|
146 |
-
</div>
|
147 |
-
<div class="assistant-label">Assistant</div>
|
148 |
-
<div class="review-content">
|
149 |
-
<p>Here is a <strong>systematic review</strong> of the provided papers:</p>
|
150 |
-
<hr>
|
151 |
-
{result_text}
|
152 |
-
</div>
|
153 |
-
</div>
|
154 |
-
"""
|
155 |
|
156 |
-
return ""
|
157 |
-
<div class="error-message">
|
158 |
-
<h3>Error</h3>
|
159 |
-
<p>Failed to generate a systematic review. Please try again.</p>
|
160 |
-
</div>
|
161 |
-
"""
|
162 |
|
163 |
except Exception as e:
|
164 |
-
return f""
|
165 |
-
<div class="error-message">
|
166 |
-
<h3>Error</h3>
|
167 |
-
<p>An error occurred: {str(e)}</p>
|
168 |
-
</div>
|
169 |
-
"""
|
170 |
-
|
171 |
-
# Custom CSS for the interface
|
172 |
-
custom_css = """
|
173 |
-
.gradio-container {
|
174 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
175 |
-
}
|
176 |
-
|
177 |
-
.response-container {
|
178 |
-
background-color: #f9f9f9;
|
179 |
-
border-radius: 8px;
|
180 |
-
padding: 15px;
|
181 |
-
margin-top: 10px;
|
182 |
-
font-size: 16px;
|
183 |
-
}
|
184 |
-
|
185 |
-
.files-container {
|
186 |
-
display: flex;
|
187 |
-
flex-wrap: wrap;
|
188 |
-
gap: 8px;
|
189 |
-
margin-bottom: 15px;
|
190 |
-
}
|
191 |
-
|
192 |
-
.file-pill {
|
193 |
-
background-color: #f0f0f0;
|
194 |
-
border-radius: 16px;
|
195 |
-
padding: 4px 12px;
|
196 |
-
display: flex;
|
197 |
-
align-items: center;
|
198 |
-
gap: 5px;
|
199 |
-
font-size: 14px;
|
200 |
-
}
|
201 |
-
|
202 |
-
.file-icon {
|
203 |
-
margin-right: 4px;
|
204 |
-
}
|
205 |
-
|
206 |
-
.file-x {
|
207 |
-
margin-left: 4px;
|
208 |
-
color: #888;
|
209 |
-
}
|
210 |
-
|
211 |
-
.assistant-label {
|
212 |
-
font-weight: 600;
|
213 |
-
margin-bottom: 5px;
|
214 |
-
color: #444;
|
215 |
-
}
|
216 |
-
|
217 |
-
.review-content {
|
218 |
-
background-color: white;
|
219 |
-
border-radius: 8px;
|
220 |
-
padding: 15px;
|
221 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
222 |
-
}
|
223 |
-
|
224 |
-
.review-content h1, .review-content h2, .review-content h3 {
|
225 |
-
margin-top: 20px;
|
226 |
-
margin-bottom: 10px;
|
227 |
-
font-weight: 600;
|
228 |
-
}
|
229 |
-
|
230 |
-
.review-content h1 {
|
231 |
-
font-size: 24px;
|
232 |
-
border-bottom: 1px solid #eee;
|
233 |
-
padding-bottom: 10px;
|
234 |
-
}
|
235 |
-
|
236 |
-
.review-content h2 {
|
237 |
-
font-size: 20px;
|
238 |
-
}
|
239 |
-
|
240 |
-
.review-content h3 {
|
241 |
-
font-size: 18px;
|
242 |
-
}
|
243 |
-
|
244 |
-
.review-content p {
|
245 |
-
margin-bottom: 15px;
|
246 |
-
line-height: 1.5;
|
247 |
-
}
|
248 |
-
|
249 |
-
.review-content hr {
|
250 |
-
margin: 20px 0;
|
251 |
-
border: 0;
|
252 |
-
border-top: 1px solid #eee;
|
253 |
-
}
|
254 |
-
|
255 |
-
.review-content table {
|
256 |
-
border-collapse: collapse;
|
257 |
-
width: 100%;
|
258 |
-
margin: 20px 0;
|
259 |
-
}
|
260 |
-
|
261 |
-
.review-content th, .review-content td {
|
262 |
-
border: 1px solid #ddd;
|
263 |
-
padding: 8px 12px;
|
264 |
-
text-align: left;
|
265 |
-
}
|
266 |
-
|
267 |
-
.review-content th {
|
268 |
-
background-color: #f2f2f2;
|
269 |
-
font-weight: 600;
|
270 |
-
}
|
271 |
-
|
272 |
-
.review-content tr:nth-child(even) {
|
273 |
-
background-color: #f9f9f9;
|
274 |
-
}
|
275 |
-
|
276 |
-
.error-message {
|
277 |
-
background-color: #fff0f0;
|
278 |
-
border-left: 4px solid #ff5252;
|
279 |
-
padding: 15px;
|
280 |
-
border-radius: 4px;
|
281 |
-
margin-top: 10px;
|
282 |
-
}
|
283 |
-
|
284 |
-
.error-message h3 {
|
285 |
-
color: #d32f2f;
|
286 |
-
margin-top: 0;
|
287 |
-
margin-bottom: 10px;
|
288 |
-
}
|
289 |
-
"""
|
290 |
|
291 |
# Create the Gradio interface
|
292 |
-
with gr.Blocks(
|
293 |
gr.Markdown("# Systematic Review Generator")
|
294 |
-
gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4
|
295 |
|
296 |
with gr.Row():
|
297 |
-
with gr.Column(
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
)
|
305 |
-
|
306 |
-
pdf_files = gr.File(
|
307 |
-
label="Upload PDF Files",
|
308 |
-
file_count="multiple",
|
309 |
-
file_types=[".pdf"]
|
310 |
-
)
|
311 |
-
|
312 |
-
model_info = gr.Markdown("""
|
313 |
-
**Model**: gpt-4.1
|
314 |
-
**Tokens**: 4000 (max output)
|
315 |
-
**Temperature**: 0.7
|
316 |
-
""")
|
317 |
-
|
318 |
-
submit_btn = gr.Button("Generate Systematic Review", variant="primary", size="lg")
|
319 |
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
""")
|
332 |
-
|
333 |
-
with gr.Column(scale=2):
|
334 |
-
# HTML output for better formatting
|
335 |
-
output = gr.HTML(label="Generated Review")
|
336 |
|
337 |
# Set up the event handler
|
338 |
submit_btn.click(
|
@@ -340,6 +139,22 @@ with gr.Blocks(css=custom_css, title="Systematic Review Generator") as app:
|
|
340 |
inputs=[api_key, pdf_files],
|
341 |
outputs=output
|
342 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
|
344 |
if __name__ == "__main__":
|
345 |
-
app.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
import base64
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
|
|
15 |
str: Generated systematic review text
|
16 |
"""
|
17 |
if not api_key.strip():
|
18 |
+
return "Please provide a valid OpenAI API key."
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
if not pdf_files:
|
21 |
+
return "Please upload at least one PDF file."
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
try:
|
24 |
# Initialize OpenAI client with the provided API key
|
25 |
client = OpenAI(api_key=api_key)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# System prompt defining systematic review steps
|
28 |
system_prompt = """Step 1: Identify a Research Field
|
|
|
29 |
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.
|
30 |
Step 2: Generate a Research Question
|
|
|
31 |
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).
|
32 |
Step 3: Create a Protocol
|
|
|
33 |
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.
|
34 |
Step 4: Evaluate Relevant Literature
|
|
|
35 |
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.
|
36 |
Step 5: Investigate Sources for Answers
|
|
|
37 |
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.
|
38 |
Step 6: Collect Data as per Protocol
|
|
|
39 |
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.
|
40 |
Step 7: Data Extraction
|
|
|
41 |
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.
|
42 |
Step 8: Critical Analysis of Results
|
|
|
43 |
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.
|
44 |
Step 9: Interpreting Derivations
|
|
|
45 |
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.
|
46 |
Step 10: Concluding Statements
|
|
|
47 |
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.
|
48 |
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.
|
49 |
"""
|
50 |
|
51 |
+
# Create a list to store file data for the API call
|
52 |
+
file_contents = []
|
53 |
+
|
54 |
+
# Process each uploaded PDF file
|
55 |
+
for pdf_file in pdf_files:
|
56 |
+
file_name = os.path.basename(pdf_file.name)
|
57 |
+
|
58 |
+
# Read the file as binary data
|
59 |
+
with open(pdf_file.name, "rb") as f:
|
60 |
+
binary_data = f.read()
|
61 |
+
|
62 |
+
# Encode the binary data to base64
|
63 |
+
base64_encoded = base64.b64encode(binary_data).decode('utf-8')
|
64 |
+
|
65 |
+
# Create proper data URL with MIME type
|
66 |
+
file_data = f"data:application/pdf;base64,{base64_encoded}"
|
67 |
+
|
68 |
+
# Add to file contents
|
69 |
+
file_contents.append({
|
70 |
+
"type": "file_attachment",
|
71 |
+
"file_name": file_name,
|
72 |
+
"data": file_data
|
73 |
+
})
|
74 |
+
|
75 |
+
# Create messages for the API call
|
76 |
+
messages = [
|
77 |
+
{
|
78 |
+
"role": "system",
|
79 |
+
"content": system_prompt
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"role": "user",
|
83 |
+
"content": [
|
84 |
+
{"type": "text", "text": "Please generate the systematic review of these papers (include also important new generated tables)"},
|
85 |
+
*file_contents
|
86 |
+
]
|
87 |
+
}
|
88 |
+
]
|
89 |
+
|
90 |
# Make the API call to OpenAI
|
91 |
+
response = client.chat.completions.create(
|
92 |
+
model="gpt-4-1106-preview", # Updated model name (or use gpt-4-turbo)
|
93 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
temperature=0.7,
|
95 |
+
max_tokens=4000,
|
96 |
top_p=1
|
97 |
)
|
98 |
|
99 |
# Extract and return the review text from the response
|
100 |
+
if response.choices and len(response.choices) > 0:
|
101 |
+
result_text = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
102 |
if result_text:
|
103 |
+
return result_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
return "Failed to generate a systematic review. Please try again."
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
except Exception as e:
|
108 |
+
return f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Create the Gradio interface
|
111 |
+
with gr.Blocks(title="Systematic Review Generator") as app:
|
112 |
gr.Markdown("# Systematic Review Generator")
|
113 |
+
gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4 model.")
|
114 |
|
115 |
with gr.Row():
|
116 |
+
with gr.Column():
|
117 |
+
# Input components
|
118 |
+
api_key = gr.Textbox(
|
119 |
+
label="OpenAI API Key",
|
120 |
+
placeholder="Enter your OpenAI API key...",
|
121 |
+
type="password"
|
122 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
pdf_files = gr.File(
|
125 |
+
label="Upload PDF Files",
|
126 |
+
file_count="multiple",
|
127 |
+
file_types=[".pdf"]
|
128 |
+
)
|
129 |
+
|
130 |
+
submit_btn = gr.Button("Generate Systematic Review", variant="primary")
|
131 |
+
|
132 |
+
with gr.Column():
|
133 |
+
# Output component
|
134 |
+
output = gr.Markdown(label="Generated Systematic Review")
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
# Set up the event handler
|
137 |
submit_btn.click(
|
|
|
139 |
inputs=[api_key, pdf_files],
|
140 |
outputs=output
|
141 |
)
|
142 |
+
|
143 |
+
gr.Markdown("""
|
144 |
+
## How to Use
|
145 |
+
|
146 |
+
1. Enter your OpenAI API key
|
147 |
+
2. Upload one or more PDF research papers
|
148 |
+
3. Click "Generate Systematic Review"
|
149 |
+
4. The systematic review will be displayed in the output area
|
150 |
+
|
151 |
+
## Note
|
152 |
+
|
153 |
+
This application requires a valid OpenAI API key with access to the GPT-4 model.
|
154 |
+
Your API key is not stored and is only used to make the API call to OpenAI.
|
155 |
+
|
156 |
+
Please note that large PDF files may cause issues with the API due to size limits.
|
157 |
+
""")
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
+
app.launch(share=True)
|