File size: 11,454 Bytes
24a9f83
bc42021
35bfd69
24a9f83
 
 
 
 
 
 
 
 
 
 
 
 
 
35bfd69
 
 
 
 
 
24a9f83
 
35bfd69
 
 
 
 
 
24a9f83
 
 
 
 
 
 
 
35bfd69
 
 
24a9f83
 
 
35bfd69
24a9f83
 
 
bc42021
 
 
 
 
 
 
24a9f83
 
 
 
 
bc42021
24a9f83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc42021
24a9f83
 
bc42021
 
 
35bfd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24a9f83
35bfd69
 
 
 
 
 
24a9f83
 
35bfd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24a9f83
 
35bfd69
24a9f83
 
 
 
35bfd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24a9f83
35bfd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24a9f83
 
 
 
 
 
 
 
 
35bfd69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import gradio as gr
import base64
import os
from openai import OpenAI

def generate_systematic_review(api_key, pdf_files):
    """
    Generate a systematic review of the uploaded PDF files using OpenAI's API.
    
    Args:
        api_key (str): OpenAI API key provided by the user
        pdf_files (list): List of uploaded PDF files
        
    Returns:
        str: Generated systematic review text
    """
    if not api_key.strip():
        return """
        <div class="error-message">
            <h3>Error</h3>
            <p>Please provide a valid OpenAI API key.</p>
        </div>
        """
    
    if not pdf_files:
        return """
        <div class="error-message">
            <h3>Error</h3>
            <p>Please upload at least one PDF file.</p>
        </div>
        """
    
    try:
        # Initialize OpenAI client with the provided API key
        client = OpenAI(api_key=api_key)
        
        # Create a list to hold file inputs for the API
        file_inputs = []
        
        # List of uploaded file names for display
        file_names = []
        
        # Process each uploaded PDF file
        for pdf_file in pdf_files:
            file_name = os.path.basename(pdf_file.name)
            file_names.append(file_name)
            
            # Read the file as binary data
            with open(pdf_file.name, "rb") as f:
                binary_data = f.read()
            
            # Encode the binary data to base64
            base64_encoded = base64.b64encode(binary_data).decode('utf-8')
            
            # Create proper data URL with MIME type
            data_url = f"data:application/pdf;base64,{base64_encoded}"
            
            # Add to file inputs
            file_inputs.append({
                "type": "input_file",
                "filename": file_name,
                "file_data": data_url
            })
        
        # System prompt defining systematic review steps
        system_prompt = """Step 1: Identify a Research Field

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.
Step 2: Generate a Research Question

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).
Step 3: Create a Protocol

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.
Step 4: Evaluate Relevant Literature

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.
Step 5: Investigate Sources for Answers

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.
Step 6: Collect Data as per Protocol

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.
Step 7: Data Extraction

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.
Step 8: Critical Analysis of Results

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.
Step 9: Interpreting Derivations

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.
Step 10: Concluding Statements

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.
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.
"""
        
        # Make the API call to OpenAI
        response = client.responses.create(
            model="gpt-4.1",
            input=[
                {
                    "role": "system",
                    "content": [
                        {
                            "type": "input_text",
                            "text": system_prompt
                        }
                    ]
                },
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "input_text",
                            "text": "Please generate the systematic review of these papers (include also important new generated tables)"
                        },
                        *file_inputs
                    ]
                }
            ],
            temperature=0.7,
            max_output_tokens=4000,
            top_p=1
        )
        
        # Extract and return the review text from the response
        if hasattr(response, 'content') and len(response.content) > 0:
            result_text = ""
            for item in response.content:
                if hasattr(item, 'text'):
                    result_text += item.text
            
            if result_text:
                # Format the file names for display
                files_html = ""
                for name in file_names:
                    files_html += f'<div class="file-pill"><span class="file-icon">πŸ“„</span> {name} <span class="file-x">Γ—</span></div>'
                
                # Create a nicely formatted response interface
                return f"""
                <div class="response-container">
                    <div class="files-container">
                        {files_html}
                    </div>
                    <div class="assistant-label">Assistant</div>
                    <div class="review-content">
                        <p>Here is a <strong>systematic review</strong> of the provided papers:</p>
                        <hr>
                        {result_text}
                    </div>
                </div>
                """
        
        return """
        <div class="error-message">
            <h3>Error</h3>
            <p>Failed to generate a systematic review. Please try again.</p>
        </div>
        """
        
    except Exception as e:
        return f"""
        <div class="error-message">
            <h3>Error</h3>
            <p>An error occurred: {str(e)}</p>
        </div>
        """

# Custom CSS for the interface
custom_css = """
.gradio-container {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.response-container {
    background-color: #f9f9f9;
    border-radius: 8px;
    padding: 15px;
    margin-top: 10px;
    font-size: 16px;
}

.files-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 15px;
}

.file-pill {
    background-color: #f0f0f0;
    border-radius: 16px;
    padding: 4px 12px;
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 14px;
}

.file-icon {
    margin-right: 4px;
}

.file-x {
    margin-left: 4px;
    color: #888;
}

.assistant-label {
    font-weight: 600;
    margin-bottom: 5px;
    color: #444;
}

.review-content {
    background-color: white;
    border-radius: 8px;
    padding: 15px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.review-content h1, .review-content h2, .review-content h3 {
    margin-top: 20px;
    margin-bottom: 10px;
    font-weight: 600;
}

.review-content h1 {
    font-size: 24px;
    border-bottom: 1px solid #eee;
    padding-bottom: 10px;
}

.review-content h2 {
    font-size: 20px;
}

.review-content h3 {
    font-size: 18px;
}

.review-content p {
    margin-bottom: 15px;
    line-height: 1.5;
}

.review-content hr {
    margin: 20px 0;
    border: 0;
    border-top: 1px solid #eee;
}

.review-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 20px 0;
}

.review-content th, .review-content td {
    border: 1px solid #ddd;
    padding: 8px 12px;
    text-align: left;
}

.review-content th {
    background-color: #f2f2f2;
    font-weight: 600;
}

.review-content tr:nth-child(even) {
    background-color: #f9f9f9;
}

.error-message {
    background-color: #fff0f0;
    border-left: 4px solid #ff5252;
    padding: 15px;
    border-radius: 4px;
    margin-top: 10px;
}

.error-message h3 {
    color: #d32f2f;
    margin-top: 0;
    margin-bottom: 10px;
}
"""

# Create the Gradio interface
with gr.Blocks(css=custom_css, title="Systematic Review Generator") as app:
    gr.Markdown("# Systematic Review Generator")
    gr.Markdown("Upload PDF files and generate a systematic review using OpenAI's GPT-4.1 model.")
    
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Box():
                gr.Markdown("### Settings")
                api_key = gr.Textbox(
                    label="OpenAI API Key", 
                    placeholder="Enter your OpenAI API key...",
                    type="password"
                )
                
                pdf_files = gr.File(
                    label="Upload PDF Files", 
                    file_count="multiple",
                    file_types=[".pdf"]
                )
                
                model_info = gr.Markdown("""
                **Model**: gpt-4.1  
                **Tokens**: 4000 (max output)  
                **Temperature**: 0.7
                """)
                
                submit_btn = gr.Button("Generate Systematic Review", variant="primary", size="lg")
            
            with gr.Accordion("How to Use", open=False):
                gr.Markdown("""
                1. Enter your OpenAI API key in the field above
                2. Upload two or more PDF research papers
                3. Click "Generate Systematic Review"
                4. The systematic review will be displayed in the output area
                
                **Note**: This application requires a valid OpenAI API key with access to the GPT-4.1 model.
                Your API key is not stored and is only used to make the API call to OpenAI.
                
                Please be aware that large PDF files may cause issues with the API due to size limits.
                """)
        
        with gr.Column(scale=2):
            # HTML output for better formatting
            output = gr.HTML(label="Generated Review")
    
    # Set up the event handler
    submit_btn.click(
        fn=generate_systematic_review,
        inputs=[api_key, pdf_files],
        outputs=output
    )

if __name__ == "__main__":
    app.launch()