|
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: |
|
|
|
client = OpenAI(api_key=api_key) |
|
|
|
|
|
file_inputs = [] |
|
|
|
|
|
file_names = [] |
|
|
|
|
|
for pdf_file in pdf_files: |
|
file_name = os.path.basename(pdf_file.name) |
|
file_names.append(file_name) |
|
|
|
|
|
with open(pdf_file.name, "rb") as f: |
|
binary_data = f.read() |
|
|
|
|
|
base64_encoded = base64.b64encode(binary_data).decode('utf-8') |
|
|
|
|
|
data_url = f"data:application/pdf;base64,{base64_encoded}" |
|
|
|
|
|
file_inputs.append({ |
|
"type": "input_file", |
|
"filename": file_name, |
|
"file_data": data_url |
|
}) |
|
|
|
|
|
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. |
|
""" |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
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: |
|
|
|
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>' |
|
|
|
|
|
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 = """ |
|
.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; |
|
} |
|
""" |
|
|
|
|
|
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): |
|
|
|
output = gr.HTML(label="Generated Review") |
|
|
|
|
|
submit_btn.click( |
|
fn=generate_systematic_review, |
|
inputs=[api_key, pdf_files], |
|
outputs=output |
|
) |
|
|
|
if __name__ == "__main__": |
|
app.launch() |