File size: 7,175 Bytes
24a9f83 |
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 |
import gradio as gr
import tempfile
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 "Please provide a valid OpenAI API key."
if not pdf_files:
return "Please upload at least one PDF file."
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 = []
# Process each uploaded PDF file
for pdf_file in pdf_files:
file_name = os.path.basename(pdf_file.name)
# Read the file as binary data
with open(pdf_file.name, "rb") as f:
file_data = f.read()
# Add to file inputs
file_inputs.append({
"type": "input_file",
"filename": file_name,
"file_data": f"data:application/pdf;base64,{file_data[:10]}" # Truncated for demo
})
# 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
]
}
],
text={
"format": {
"type": "text"
}
},
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:
for item in response.content:
if hasattr(item, 'text'):
return item.text
return "Failed to generate a systematic review. Please try again."
except Exception as e:
return f"An error occurred: {str(e)}"
# Create the Gradio interface
with gr.Blocks(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():
# Input components
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"]
)
submit_btn = gr.Button("Generate Systematic Review", variant="primary")
with gr.Column():
# Output component
output = gr.Markdown(label="Generated Systematic Review")
# Set up the event handler
submit_btn.click(
fn=generate_systematic_review,
inputs=[api_key, pdf_files],
outputs=output
)
gr.Markdown("""
## How to Use
1. Enter your OpenAI API key
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.
""")
if __name__ == "__main__":
app.launch() |