Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tempfile
|
3 |
+
import os
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
def generate_systematic_review(api_key, pdf_files):
|
7 |
+
"""
|
8 |
+
Generate a systematic review of the uploaded PDF files using OpenAI's API.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
api_key (str): OpenAI API key provided by the user
|
12 |
+
pdf_files (list): List of uploaded PDF files
|
13 |
+
|
14 |
+
Returns:
|
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 |
+
# Create a list to hold file inputs for the API
|
28 |
+
file_inputs = []
|
29 |
+
|
30 |
+
# Process each uploaded PDF file
|
31 |
+
for pdf_file in pdf_files:
|
32 |
+
file_name = os.path.basename(pdf_file.name)
|
33 |
+
|
34 |
+
# Read the file as binary data
|
35 |
+
with open(pdf_file.name, "rb") as f:
|
36 |
+
file_data = f.read()
|
37 |
+
|
38 |
+
# Add to file inputs
|
39 |
+
file_inputs.append({
|
40 |
+
"type": "input_file",
|
41 |
+
"filename": file_name,
|
42 |
+
"file_data": f"data:application/pdf;base64,{file_data[:10]}" # Truncated for demo
|
43 |
+
})
|
44 |
+
|
45 |
+
# System prompt defining systematic review steps
|
46 |
+
system_prompt = """Step 1: Identify a Research Field
|
47 |
+
|
48 |
+
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.
|
49 |
+
Step 2: Generate a Research Question
|
50 |
+
|
51 |
+
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).
|
52 |
+
Step 3: Create a Protocol
|
53 |
+
|
54 |
+
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.
|
55 |
+
Step 4: Evaluate Relevant Literature
|
56 |
+
|
57 |
+
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.
|
58 |
+
Step 5: Investigate Sources for Answers
|
59 |
+
|
60 |
+
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.
|
61 |
+
Step 6: Collect Data as per Protocol
|
62 |
+
|
63 |
+
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.
|
64 |
+
Step 7: Data Extraction
|
65 |
+
|
66 |
+
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.
|
67 |
+
Step 8: Critical Analysis of Results
|
68 |
+
|
69 |
+
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.
|
70 |
+
Step 9: Interpreting Derivations
|
71 |
+
|
72 |
+
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.
|
73 |
+
Step 10: Concluding Statements
|
74 |
+
|
75 |
+
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.
|
76 |
+
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.
|
77 |
+
"""
|
78 |
+
|
79 |
+
# Make the API call to OpenAI
|
80 |
+
response = client.responses.create(
|
81 |
+
model="gpt-4.1",
|
82 |
+
input=[
|
83 |
+
{
|
84 |
+
"role": "system",
|
85 |
+
"content": [
|
86 |
+
{
|
87 |
+
"type": "input_text",
|
88 |
+
"text": system_prompt
|
89 |
+
}
|
90 |
+
]
|
91 |
+
},
|
92 |
+
{
|
93 |
+
"role": "user",
|
94 |
+
"content": [
|
95 |
+
{
|
96 |
+
"type": "input_text",
|
97 |
+
"text": "Please generate the systematic review of these papers (include also important new generated tables)"
|
98 |
+
},
|
99 |
+
*file_inputs
|
100 |
+
]
|
101 |
+
}
|
102 |
+
],
|
103 |
+
text={
|
104 |
+
"format": {
|
105 |
+
"type": "text"
|
106 |
+
}
|
107 |
+
},
|
108 |
+
temperature=0.7,
|
109 |
+
max_output_tokens=4000,
|
110 |
+
top_p=1
|
111 |
+
)
|
112 |
+
|
113 |
+
# Extract and return the review text from the response
|
114 |
+
if hasattr(response, 'content') and len(response.content) > 0:
|
115 |
+
for item in response.content:
|
116 |
+
if hasattr(item, 'text'):
|
117 |
+
return item.text
|
118 |
+
|
119 |
+
return "Failed to generate a systematic review. Please try again."
|
120 |
+
|
121 |
+
except Exception as e:
|
122 |
+
return f"An error occurred: {str(e)}"
|
123 |
+
|
124 |
+
# Create the Gradio interface
|
125 |
+
with gr.Blocks(title="Systematic Review Generator") as app:
|
126 |
+
gr.Markdown("# Systematic Review Generator")
|
127 |
+
gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4.1 model.")
|
128 |
+
|
129 |
+
with gr.Row():
|
130 |
+
with gr.Column():
|
131 |
+
# Input components
|
132 |
+
api_key = gr.Textbox(
|
133 |
+
label="OpenAI API Key",
|
134 |
+
placeholder="Enter your OpenAI API key...",
|
135 |
+
type="password"
|
136 |
+
)
|
137 |
+
|
138 |
+
pdf_files = gr.File(
|
139 |
+
label="Upload PDF Files",
|
140 |
+
file_count="multiple",
|
141 |
+
file_types=[".pdf"]
|
142 |
+
)
|
143 |
+
|
144 |
+
submit_btn = gr.Button("Generate Systematic Review", variant="primary")
|
145 |
+
|
146 |
+
with gr.Column():
|
147 |
+
# Output component
|
148 |
+
output = gr.Markdown(label="Generated Systematic Review")
|
149 |
+
|
150 |
+
# Set up the event handler
|
151 |
+
submit_btn.click(
|
152 |
+
fn=generate_systematic_review,
|
153 |
+
inputs=[api_key, pdf_files],
|
154 |
+
outputs=output
|
155 |
+
)
|
156 |
+
|
157 |
+
gr.Markdown("""
|
158 |
+
## How to Use
|
159 |
+
|
160 |
+
1. Enter your OpenAI API key
|
161 |
+
2. Upload two or more PDF research papers
|
162 |
+
3. Click "Generate Systematic Review"
|
163 |
+
4. The systematic review will be displayed in the output area
|
164 |
+
|
165 |
+
## Note
|
166 |
+
|
167 |
+
This application requires a valid OpenAI API key with access to the GPT-4.1 model.
|
168 |
+
Your API key is not stored and is only used to make the API call to OpenAI.
|
169 |
+
""")
|
170 |
+
|
171 |
+
if __name__ == "__main__":
|
172 |
+
app.launch()
|