Update app.py
Browse files
app.py
CHANGED
@@ -1,182 +1,224 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
-
from openai import OpenAI
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
if not pdf_files:
|
21 |
return "Please upload at least one PDF file."
|
22 |
|
|
|
|
|
|
|
23 |
try:
|
24 |
-
|
25 |
-
client = OpenAI(api_key=api_key)
|
26 |
-
|
27 |
-
# System prompt defining systematic review steps
|
28 |
-
system_prompt = """Step 1: Identify a Research Field
|
29 |
-
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.
|
30 |
-
Step 2: Generate a Research Question
|
31 |
-
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).
|
32 |
-
Step 3: Create a Protocol
|
33 |
-
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.
|
34 |
-
Step 4: Evaluate Relevant Literature
|
35 |
-
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.
|
36 |
-
Step 5: Investigate Sources for Answers
|
37 |
-
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.
|
38 |
-
Step 6: Collect Data as per Protocol
|
39 |
-
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.
|
40 |
-
Step 7: Data Extraction
|
41 |
-
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.
|
42 |
-
Step 8: Critical Analysis of Results
|
43 |
-
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.
|
44 |
-
Step 9: Interpreting Derivations
|
45 |
-
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.
|
46 |
-
Step 10: Concluding Statements
|
47 |
-
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.
|
48 |
-
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.
|
49 |
-
"""
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
file_response = client.files.create(
|
56 |
-
file=f,
|
57 |
-
purpose="assistants"
|
58 |
-
)
|
59 |
-
file_ids.append(file_response.id)
|
60 |
-
|
61 |
-
# Create an assistant
|
62 |
-
assistant = client.beta.assistants.create(
|
63 |
-
name="Systematic Review Generator",
|
64 |
-
instructions=system_prompt,
|
65 |
-
model="gpt-4.1",
|
66 |
-
tools=[{"type": "file_search"}],
|
67 |
-
file_ids=file_ids
|
68 |
-
)
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
thread_id=thread.id,
|
76 |
-
role="user",
|
77 |
-
content="Please generate the systematic review of these papers (include also important new generated tables)"
|
78 |
-
)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
thread_id=thread.id,
|
83 |
-
assistant_id=assistant.id
|
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 |
-
client.beta.assistants.delete(assistant_id=assistant.id)
|
115 |
-
client.beta.threads.delete(thread_id=thread.id)
|
116 |
-
for file_id in file_ids:
|
117 |
-
client.files.delete(file_id=file_id)
|
118 |
|
119 |
-
|
|
|
|
|
|
|
|
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
client.files.delete(file_id=file_id)
|
126 |
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
except Exception as e:
|
130 |
-
return f"
|
131 |
|
132 |
-
#
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
)
|
145 |
-
|
146 |
-
pdf_files = gr.File(
|
147 |
-
label="Upload PDF Files",
|
148 |
-
file_count="multiple",
|
149 |
-
file_types=[".pdf"]
|
150 |
-
)
|
151 |
-
|
152 |
-
submit_btn = gr.Button("Generate Systematic Review", variant="primary")
|
153 |
-
|
154 |
-
with gr.Column():
|
155 |
-
# Output component
|
156 |
-
output = gr.Markdown(label="Generated Systematic Review")
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
)
|
164 |
|
165 |
-
gr.
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
|
|
|
174 |
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
|
|
181 |
if __name__ == "__main__":
|
182 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import fitz # PyMuPDF for PDF processing
|
4 |
import os
|
5 |
import tempfile
|
|
|
6 |
|
7 |
+
# Variable to store API key
|
8 |
+
api_key = ""
|
9 |
+
|
10 |
+
# Function to update API key
|
11 |
+
def set_api_key(key):
|
12 |
+
global api_key
|
13 |
+
api_key = key
|
14 |
+
return "API Key Set Successfully!"
|
15 |
+
|
16 |
+
# Function to extract text from PDF
|
17 |
+
def extract_text_from_pdf(pdf_path):
|
18 |
+
try:
|
19 |
+
doc = fitz.open(pdf_path)
|
20 |
+
text = "\n".join([page.get_text("text") for page in doc])
|
21 |
+
return text
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error extracting text from PDF: {str(e)}"
|
24 |
+
|
25 |
+
# Function to interact with OpenAI API for systematic review
|
26 |
+
def generate_systematic_review(pdf_files, review_question, include_tables=True):
|
27 |
+
if not api_key:
|
28 |
+
return "Please enter your OpenAI API key first."
|
29 |
|
30 |
if not pdf_files:
|
31 |
return "Please upload at least one PDF file."
|
32 |
|
33 |
+
if not review_question:
|
34 |
+
return "Please enter a review question."
|
35 |
+
|
36 |
try:
|
37 |
+
openai.api_key = api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Create the system message with systematic review guidelines
|
40 |
+
system_prompt = """
|
41 |
+
Step 1: Identify a Research Field
|
42 |
+
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
Step 2: Generate a Research Question
|
45 |
+
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).
|
46 |
|
47 |
+
Step 3: Create a Protocol
|
48 |
+
After generating your research question, the next step is to create a protocol. 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.
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
Step 4: Evaluate Relevant Literature
|
51 |
+
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.
|
|
|
|
|
|
|
52 |
|
53 |
+
Step 5: Investigate Sources for Answers
|
54 |
+
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.
|
55 |
+
|
56 |
+
Step 6: Collect Data as per Protocol
|
57 |
+
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.
|
58 |
+
|
59 |
+
Step 7: Data Extraction
|
60 |
+
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.
|
61 |
+
|
62 |
+
Step 8: Critical Analysis of Results
|
63 |
+
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.
|
64 |
+
|
65 |
+
Step 9: Interpreting Derivations
|
66 |
+
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.
|
67 |
+
|
68 |
+
Step 10: Concluding Statements
|
69 |
+
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.
|
70 |
+
"""
|
71 |
+
|
72 |
+
# Extract text from each PDF
|
73 |
+
pdf_texts = []
|
74 |
+
pdf_names = []
|
75 |
+
|
76 |
+
for pdf_file in pdf_files:
|
77 |
+
if isinstance(pdf_file, str): # If it's already a path
|
78 |
+
pdf_path = pdf_file
|
79 |
+
else: # If it's a file object
|
80 |
+
pdf_path = pdf_file.name
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
pdf_name = os.path.basename(pdf_path)
|
83 |
+
pdf_text = extract_text_from_pdf(pdf_path)
|
84 |
+
|
85 |
+
pdf_texts.append(pdf_text)
|
86 |
+
pdf_names.append(pdf_name)
|
87 |
|
88 |
+
# Prepare the user prompt with the review question and instructions
|
89 |
+
table_instruction = ""
|
90 |
+
if include_tables:
|
91 |
+
table_instruction = " Please include important new generated tables in your review."
|
|
|
92 |
|
93 |
+
user_prompt = f"Please generate a systematic review of the following {len(pdf_files)} papers: {', '.join(pdf_names)}.{table_instruction}\n\nReview Question: {review_question}"
|
94 |
+
|
95 |
+
# Create the messages for the API call
|
96 |
+
messages = [
|
97 |
+
{"role": "system", "content": system_prompt},
|
98 |
+
{"role": "user", "content": user_prompt + "\n\n" + "\n\n".join([f"Paper {i+1} - {pdf_names[i]}:\n{pdf_texts[i]}" for i in range(len(pdf_texts))])}
|
99 |
+
]
|
100 |
|
101 |
+
# Call the API with temperature=1 and top_p=1 as specified
|
102 |
+
response = openai.ChatCompletion.create(
|
103 |
+
model="gpt-4.1",
|
104 |
+
messages=messages,
|
105 |
+
temperature=1,
|
106 |
+
top_p=1,
|
107 |
+
max_tokens=2048
|
108 |
+
)
|
109 |
+
|
110 |
+
return response["choices"][0]["message"]["content"]
|
111 |
+
|
112 |
except Exception as e:
|
113 |
+
return f"Error generating systematic review: {str(e)}"
|
114 |
|
115 |
+
# Function to save uploaded files
|
116 |
+
def save_uploaded_files(files):
|
117 |
+
if not files:
|
118 |
+
return []
|
119 |
|
120 |
+
saved_paths = []
|
121 |
+
for file in files:
|
122 |
+
if file is not None:
|
123 |
+
# Create a temporary file
|
124 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
125 |
+
tmp_file.write(file)
|
126 |
+
saved_paths.append(tmp_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
return saved_paths
|
129 |
+
|
130 |
+
# Gradio UI Layout
|
131 |
+
with gr.Blocks() as demo:
|
132 |
+
gr.Markdown("# Systematic Review Generator for Research Papers")
|
|
|
133 |
|
134 |
+
with gr.Accordion("How to Use This App", open=True):
|
135 |
+
gr.Markdown("""
|
136 |
+
### Getting Started:
|
137 |
+
1. Enter your OpenAI API key in the field below and click "Set API Key"
|
138 |
+
2. Upload multiple PDF research papers (2 or more recommended)
|
139 |
+
3. Enter your review question or topic
|
140 |
+
4. Check the "Include Tables" option if you want the review to include comparison tables
|
141 |
+
5. Click "Generate Systematic Review" to start the process
|
142 |
+
|
143 |
+
### Tips:
|
144 |
+
- For best results, upload papers that are related to the same research topic or field
|
145 |
+
- Be specific in your review question to get more focused results
|
146 |
+
- The generated review will follow a systematic structure including research field identification, data extraction, analysis, and conclusions
|
147 |
+
- The more papers you upload, the more comprehensive the review will be
|
148 |
+
""")
|
149 |
|
150 |
+
# API Key Input
|
151 |
+
with gr.Row():
|
152 |
+
api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
|
153 |
+
api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
|
154 |
+
api_key_output = gr.Textbox(label="API Key Status", interactive=False)
|
155 |
+
|
156 |
+
# PDF Upload and Review Settings
|
157 |
+
with gr.Row():
|
158 |
+
with gr.Column():
|
159 |
+
pdf_files = gr.File(label="Upload PDF Research Papers", file_count="multiple", type="binary")
|
160 |
+
review_question = gr.Textbox(label="Review Question or Topic", placeholder="What are the current advances in GAN applications for speech processing?")
|
161 |
+
include_tables = gr.Checkbox(label="Include Comparison Tables", value=True)
|
162 |
+
generate_button = gr.Button("Generate Systematic Review", elem_id="generate_button")
|
163 |
+
|
164 |
+
# Output
|
165 |
+
review_output = gr.Textbox(label="Systematic Review", interactive=False, lines=20)
|
166 |
|
167 |
+
# Button actions
|
168 |
+
api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
|
169 |
|
170 |
+
# Generate systematic review
|
171 |
+
def process_files_and_generate_review(files, question, include_tables):
|
172 |
+
if not files:
|
173 |
+
return "Please upload at least one PDF file."
|
174 |
+
|
175 |
+
# Save uploaded files
|
176 |
+
saved_paths = save_uploaded_files(files)
|
177 |
+
|
178 |
+
# Generate review
|
179 |
+
review = generate_systematic_review(saved_paths, question, include_tables)
|
180 |
+
|
181 |
+
# Clean up temporary files
|
182 |
+
for path in saved_paths:
|
183 |
+
try:
|
184 |
+
os.remove(path)
|
185 |
+
except:
|
186 |
+
pass
|
187 |
+
|
188 |
+
return review
|
189 |
|
190 |
+
generate_button.click(
|
191 |
+
process_files_and_generate_review,
|
192 |
+
inputs=[pdf_files, review_question, include_tables],
|
193 |
+
outputs=[review_output]
|
194 |
+
)
|
195 |
+
|
196 |
+
# Add CSS styling
|
197 |
+
css = """
|
198 |
+
<style>
|
199 |
+
#generate_button {
|
200 |
+
background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%); /* Purple gradient */
|
201 |
+
color: white;
|
202 |
+
font-weight: bold;
|
203 |
+
}
|
204 |
+
#generate_button:hover {
|
205 |
+
background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%); /* Slightly lighter */
|
206 |
+
}
|
207 |
+
#api_key_button {
|
208 |
+
background: linear-gradient(135deg, #68d391 0%, #48bb78 100%); /* Green gradient */
|
209 |
+
color: white;
|
210 |
+
font-weight: bold;
|
211 |
+
margin-top: 27px;
|
212 |
+
}
|
213 |
+
#api_key_button:hover {
|
214 |
+
background: linear-gradient(135deg, #38a169 0%, #68d391 100%); /* Slightly darker green */
|
215 |
+
}
|
216 |
+
.gradio-container {
|
217 |
+
max-width: 1200px !important;
|
218 |
+
}
|
219 |
+
</style>
|
220 |
+
"""
|
221 |
|
222 |
+
# Launch the app
|
223 |
if __name__ == "__main__":
|
224 |
+
demo.launch(css=css)
|