|
import gradio as gr |
|
import os |
|
import tempfile |
|
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: |
|
|
|
client = OpenAI(api_key=api_key) |
|
|
|
|
|
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. |
|
""" |
|
|
|
|
|
file_ids = [] |
|
for pdf_file in pdf_files: |
|
with open(pdf_file.name, "rb") as f: |
|
file_response = client.files.create( |
|
file=f, |
|
purpose="assistants" |
|
) |
|
file_ids.append(file_response.id) |
|
|
|
|
|
assistant = client.beta.assistants.create( |
|
name="Systematic Review Generator", |
|
instructions=system_prompt, |
|
model="gpt-4.1", |
|
tools=[{"type": "file_search"}], |
|
file_ids=file_ids |
|
) |
|
|
|
|
|
thread = client.beta.threads.create() |
|
|
|
|
|
message = client.beta.threads.messages.create( |
|
thread_id=thread.id, |
|
role="user", |
|
content="Please generate the systematic review of these papers (include also important new generated tables)" |
|
) |
|
|
|
|
|
run = client.beta.threads.runs.create( |
|
thread_id=thread.id, |
|
assistant_id=assistant.id |
|
) |
|
|
|
|
|
import time |
|
while True: |
|
run_status = client.beta.threads.runs.retrieve( |
|
thread_id=thread.id, |
|
run_id=run.id |
|
) |
|
if run_status.status == "completed": |
|
break |
|
elif run_status.status in ["failed", "cancelled", "expired"]: |
|
return f"Run failed with status: {run_status.status}" |
|
time.sleep(2) |
|
|
|
|
|
messages = client.beta.threads.messages.list(thread_id=thread.id) |
|
|
|
|
|
assistant_messages = [msg for msg in messages.data if msg.role == "assistant"] |
|
if assistant_messages: |
|
latest_message = assistant_messages[0] |
|
|
|
|
|
result_text = "" |
|
for content_item in latest_message.content: |
|
if content_item.type == "text": |
|
result_text += content_item.text.value |
|
|
|
|
|
client.beta.assistants.delete(assistant_id=assistant.id) |
|
client.beta.threads.delete(thread_id=thread.id) |
|
for file_id in file_ids: |
|
client.files.delete(file_id=file_id) |
|
|
|
return result_text |
|
|
|
|
|
client.beta.assistants.delete(assistant_id=assistant.id) |
|
client.beta.threads.delete(thread_id=thread.id) |
|
for file_id in file_ids: |
|
client.files.delete(file_id=file_id) |
|
|
|
return "Failed to generate a systematic review. Please try again." |
|
|
|
except Exception as e: |
|
return f"An error occurred: {str(e)}" |
|
|
|
|
|
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 model.") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
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 = gr.Markdown(label="Generated Systematic Review") |
|
|
|
|
|
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 one 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 model. |
|
Your API key is not stored and is only used to make the API call to OpenAI. |
|
|
|
The review generation may take a few minutes depending on the number and size of PDF files. |
|
""") |
|
|
|
if __name__ == "__main__": |
|
app.launch(share=True) |