Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
import os
|
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
def generate_systematic_review(api_key, pdf_files):
|
@@ -33,13 +34,19 @@ def generate_systematic_review(api_key, pdf_files):
|
|
33 |
|
34 |
# Read the file as binary data
|
35 |
with open(pdf_file.name, "rb") as f:
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Add to file inputs
|
39 |
file_inputs.append({
|
40 |
"type": "input_file",
|
41 |
"filename": file_name,
|
42 |
-
"file_data":
|
43 |
})
|
44 |
|
45 |
# System prompt defining systematic review steps
|
@@ -100,11 +107,6 @@ By following these steps, you can ensure that your systematic review paper is we
|
|
100 |
]
|
101 |
}
|
102 |
],
|
103 |
-
text={
|
104 |
-
"format": {
|
105 |
-
"type": "text"
|
106 |
-
}
|
107 |
-
},
|
108 |
temperature=0.7,
|
109 |
max_output_tokens=4000,
|
110 |
top_p=1
|
@@ -112,9 +114,13 @@ By following these steps, you can ensure that your systematic review paper is we
|
|
112 |
|
113 |
# Extract and return the review text from the response
|
114 |
if hasattr(response, 'content') and len(response.content) > 0:
|
|
|
115 |
for item in response.content:
|
116 |
if hasattr(item, 'text'):
|
117 |
-
|
|
|
|
|
|
|
118 |
|
119 |
return "Failed to generate a systematic review. Please try again."
|
120 |
|
@@ -166,6 +172,8 @@ with gr.Blocks(title="Systematic Review Generator") as app:
|
|
166 |
|
167 |
This application requires a valid OpenAI API key with access to the GPT-4.1 model.
|
168 |
Your API key is not stored and is only used to make the API call to OpenAI.
|
|
|
|
|
169 |
""")
|
170 |
|
171 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
import os
|
4 |
+
import base64
|
5 |
from openai import OpenAI
|
6 |
|
7 |
def generate_systematic_review(api_key, pdf_files):
|
|
|
34 |
|
35 |
# Read the file as binary data
|
36 |
with open(pdf_file.name, "rb") as f:
|
37 |
+
binary_data = f.read()
|
38 |
+
|
39 |
+
# Encode the binary data to base64
|
40 |
+
base64_encoded = base64.b64encode(binary_data).decode('utf-8')
|
41 |
+
|
42 |
+
# Create proper data URL with MIME type
|
43 |
+
data_url = f"data:application/pdf;base64,{base64_encoded}"
|
44 |
|
45 |
# Add to file inputs
|
46 |
file_inputs.append({
|
47 |
"type": "input_file",
|
48 |
"filename": file_name,
|
49 |
+
"file_data": data_url
|
50 |
})
|
51 |
|
52 |
# System prompt defining systematic review steps
|
|
|
107 |
]
|
108 |
}
|
109 |
],
|
|
|
|
|
|
|
|
|
|
|
110 |
temperature=0.7,
|
111 |
max_output_tokens=4000,
|
112 |
top_p=1
|
|
|
114 |
|
115 |
# Extract and return the review text from the response
|
116 |
if hasattr(response, 'content') and len(response.content) > 0:
|
117 |
+
result_text = ""
|
118 |
for item in response.content:
|
119 |
if hasattr(item, 'text'):
|
120 |
+
result_text += item.text
|
121 |
+
|
122 |
+
if result_text:
|
123 |
+
return result_text
|
124 |
|
125 |
return "Failed to generate a systematic review. Please try again."
|
126 |
|
|
|
172 |
|
173 |
This application requires a valid OpenAI API key with access to the GPT-4.1 model.
|
174 |
Your API key is not stored and is only used to make the API call to OpenAI.
|
175 |
+
|
176 |
+
Please note that large PDF files may cause issues with the API due to size limits.
|
177 |
""")
|
178 |
|
179 |
if __name__ == "__main__":
|