Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,12 @@ def extract_text_from_pdf(pdf_file):
|
|
21 |
# Function to handle PDF upload and insertion into Milvus
|
22 |
def upload_and_index_pdf(pdf_file, server_url):
|
23 |
try:
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
response = requests.post(insert_url, files=files, timeout=600)
|
26 |
response.raise_for_status()
|
27 |
return "PDF uploaded and indexed successfully!"
|
@@ -67,15 +72,6 @@ with gr.Blocks() as demo:
|
|
67 |
placeholder="Enter your Milvus Server URL"
|
68 |
)
|
69 |
upload_button = gr.Button("Upload and Index PDF")
|
70 |
-
|
71 |
-
# Load and index the default PDF on startup (if it exists)
|
72 |
-
if os.path.exists("transformers.pdf"): # Check if transformers.pdf exists
|
73 |
-
with open("transformers.pdf", "rb") as f:
|
74 |
-
default_pdf_content = f.read()
|
75 |
-
# Using gr.State to store the default PDF content between sessions
|
76 |
-
default_pdf_file = gr.State(default_pdf_content)
|
77 |
-
upload_and_index_pdf(default_pdf_file, server_url_input.value)
|
78 |
-
|
79 |
with gr.Column():
|
80 |
upload_output = gr.Textbox(label="Upload Status")
|
81 |
|
@@ -92,6 +88,15 @@ with gr.Blocks() as demo:
|
|
92 |
with gr.Column():
|
93 |
answer_output = gr.Textbox(label="Answer")
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
upload_button.click(
|
96 |
fn=upload_and_index_pdf,
|
97 |
inputs=[pdf_input, server_url_input],
|
|
|
21 |
# Function to handle PDF upload and insertion into Milvus
|
22 |
def upload_and_index_pdf(pdf_file, server_url):
|
23 |
try:
|
24 |
+
# Check if pdf_file is a file path (string) or a file object
|
25 |
+
if isinstance(pdf_file, str):
|
26 |
+
files = {'file': (pdf_file, open(pdf_file, 'rb'), 'application/pdf')}
|
27 |
+
else:
|
28 |
+
files = {'file': (pdf_file.name, pdf_file, 'application/pdf')}
|
29 |
+
|
30 |
response = requests.post(insert_url, files=files, timeout=600)
|
31 |
response.raise_for_status()
|
32 |
return "PDF uploaded and indexed successfully!"
|
|
|
72 |
placeholder="Enter your Milvus Server URL"
|
73 |
)
|
74 |
upload_button = gr.Button("Upload and Index PDF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
with gr.Column():
|
76 |
upload_output = gr.Textbox(label="Upload Status")
|
77 |
|
|
|
88 |
with gr.Column():
|
89 |
answer_output = gr.Textbox(label="Answer")
|
90 |
|
91 |
+
# Load and index the default PDF on startup (if it exists)
|
92 |
+
|
93 |
+
if os.path.exists("transformers.pdf"):
|
94 |
+
print("transformers.pdf exists")
|
95 |
+
upload_and_index_pdf("transformers.pdf", space_url)
|
96 |
+
upload_output.value = "Default PDF (transformers.pdf) indexed on startup!" # Update status
|
97 |
+
else:
|
98 |
+
print("transformers.pdf does not exist")
|
99 |
+
|
100 |
upload_button.click(
|
101 |
fn=upload_and_index_pdf,
|
102 |
inputs=[pdf_input, server_url_input],
|