Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
@@ -48,60 +48,82 @@ The final step is to make concluding statements. This involves summarizing your
|
|
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 |
-
# Process each uploaded PDF file
|
55 |
for pdf_file in pdf_files:
|
56 |
-
file_name = os.path.basename(pdf_file.name)
|
57 |
-
|
58 |
-
# Read the file as binary data
|
59 |
with open(pdf_file.name, "rb") as f:
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
# Create
|
76 |
-
|
77 |
-
{
|
78 |
-
"role": "system",
|
79 |
-
"content": system_prompt
|
80 |
-
},
|
81 |
-
{
|
82 |
-
"role": "user",
|
83 |
-
"content": [
|
84 |
-
{"type": "text", "text": "Please generate the systematic review of these papers (include also important new generated tables)"},
|
85 |
-
*file_contents
|
86 |
-
]
|
87 |
-
}
|
88 |
-
]
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
max_tokens=4000,
|
96 |
-
top_p=1
|
97 |
)
|
98 |
|
99 |
-
#
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
return "Failed to generate a systematic review. Please try again."
|
106 |
|
107 |
except Exception as e:
|
@@ -153,7 +175,7 @@ with gr.Blocks(title="Systematic Review Generator") as app:
|
|
153 |
This application requires a valid OpenAI API key with access to the GPT-4 model.
|
154 |
Your API key is not stored and is only used to make the API call to OpenAI.
|
155 |
|
156 |
-
|
157 |
""")
|
158 |
|
159 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import tempfile
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
|
|
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 |
+
# First, upload all files to get file IDs
|
52 |
+
file_ids = []
|
|
|
|
|
53 |
for pdf_file in pdf_files:
|
|
|
|
|
|
|
54 |
with open(pdf_file.name, "rb") as f:
|
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 |
+
# Create a thread
|
71 |
+
thread = client.beta.threads.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Add a message to the thread
|
74 |
+
message = client.beta.threads.messages.create(
|
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 |
+
# Run the assistant on the thread
|
81 |
+
run = client.beta.threads.runs.create(
|
82 |
+
thread_id=thread.id,
|
83 |
+
assistant_id=assistant.id
|
84 |
+
)
|
85 |
|
86 |
+
# Poll for the run completion
|
87 |
+
import time
|
88 |
+
while True:
|
89 |
+
run_status = client.beta.threads.runs.retrieve(
|
90 |
+
thread_id=thread.id,
|
91 |
+
run_id=run.id
|
92 |
+
)
|
93 |
+
if run_status.status == "completed":
|
94 |
+
break
|
95 |
+
elif run_status.status in ["failed", "cancelled", "expired"]:
|
96 |
+
return f"Run failed with status: {run_status.status}"
|
97 |
+
time.sleep(2)
|
98 |
+
|
99 |
+
# Get the messages from the thread
|
100 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
101 |
+
|
102 |
+
# Get the latest assistant message
|
103 |
+
assistant_messages = [msg for msg in messages.data if msg.role == "assistant"]
|
104 |
+
if assistant_messages:
|
105 |
+
latest_message = assistant_messages[0] # Most recent message first
|
106 |
+
|
107 |
+
# Extract text content from the message
|
108 |
+
result_text = ""
|
109 |
+
for content_item in latest_message.content:
|
110 |
+
if content_item.type == "text":
|
111 |
+
result_text += content_item.text.value
|
112 |
+
|
113 |
+
# Clean up: delete assistant, thread, and files
|
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 |
+
return result_text
|
120 |
+
|
121 |
+
# Clean up resources even if we didn't get a response
|
122 |
+
client.beta.assistants.delete(assistant_id=assistant.id)
|
123 |
+
client.beta.threads.delete(thread_id=thread.id)
|
124 |
+
for file_id in file_ids:
|
125 |
+
client.files.delete(file_id=file_id)
|
126 |
+
|
127 |
return "Failed to generate a systematic review. Please try again."
|
128 |
|
129 |
except Exception as e:
|
|
|
175 |
This application requires a valid OpenAI API key with access to the GPT-4 model.
|
176 |
Your API key is not stored and is only used to make the API call to OpenAI.
|
177 |
|
178 |
+
The review generation may take a few minutes depending on the number and size of PDF files.
|
179 |
""")
|
180 |
|
181 |
if __name__ == "__main__":
|