Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,12 @@ from io import BytesIO
|
|
| 4 |
import pypdf
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# **IMPORTANT:**
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#space_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") # or set it here
|
| 12 |
-
#rag_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") + "/rag"
|
| 13 |
-
#insert_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") + "/insert"
|
| 14 |
-
space_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space")
|
| 15 |
|
| 16 |
-
# Function to extract text from a PDF file
|
| 17 |
def extract_text_from_pdf(pdf_file):
|
| 18 |
pdf_stream = BytesIO(pdf_file)
|
| 19 |
reader = pypdf.PdfReader(pdf_stream)
|
|
@@ -24,12 +20,10 @@ def extract_text_from_pdf(pdf_file):
|
|
| 24 |
|
| 25 |
# Function to handle PDF upload and insertion into Milvus
|
| 26 |
def upload_and_index_pdf(pdf_file, server_url):
|
| 27 |
-
#insert_url = server_url.rstrip("/") + "/insert"
|
| 28 |
-
insert_url = server_url + "/insert"
|
| 29 |
try:
|
| 30 |
files = {'file': (pdf_file.name, open(pdf_file.name, 'rb'), 'application/pdf')}
|
| 31 |
-
response = requests.post(insert_url, files=files, timeout=600)
|
| 32 |
-
response.raise_for_status()
|
| 33 |
return "PDF uploaded and indexed successfully!"
|
| 34 |
except requests.exceptions.RequestException as e:
|
| 35 |
return f"Error during PDF upload: {e}"
|
|
@@ -38,8 +32,6 @@ def upload_and_index_pdf(pdf_file, server_url):
|
|
| 38 |
|
| 39 |
# Function to perform RAG query
|
| 40 |
def perform_rag_query(question, server_url):
|
| 41 |
-
#rag_url = server_url.rstrip("/") + "/rag"
|
| 42 |
-
rag_url = server_url + "/rag"
|
| 43 |
try:
|
| 44 |
response = requests.post(rag_url, json={"question": question}, timeout=300)
|
| 45 |
response.raise_for_status()
|
|
@@ -50,6 +42,14 @@ def perform_rag_query(question, server_url):
|
|
| 50 |
except Exception as e:
|
| 51 |
return f"An unexpected error occurred: {e}"
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Gradio interface setup
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown(
|
|
@@ -60,20 +60,35 @@ with gr.Blocks() as demo:
|
|
| 60 |
)
|
| 61 |
with gr.Row():
|
| 62 |
with gr.Column():
|
| 63 |
-
pdf_input = gr.File(label="Upload PDF", type="
|
| 64 |
server_url_input = gr.Textbox(
|
| 65 |
label="Milvus Server URL",
|
| 66 |
-
value=space_url,
|
| 67 |
placeholder="Enter your Milvus Server URL"
|
| 68 |
)
|
| 69 |
upload_button = gr.Button("Upload and Index PDF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
with gr.Column():
|
| 71 |
upload_output = gr.Textbox(label="Upload Status")
|
| 72 |
-
|
| 73 |
with gr.Row():
|
| 74 |
with gr.Column():
|
| 75 |
question_input = gr.Textbox(label="Ask a question about the PDF")
|
| 76 |
query_button = gr.Button("Ask")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Column():
|
| 78 |
answer_output = gr.Textbox(label="Answer")
|
| 79 |
|
|
|
|
| 4 |
import pypdf
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# **IMPORTANT:** Set your Hugging Face Space URL here or as an environment variable
|
| 8 |
+
space_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") # Your Milvus Server Space URL
|
| 9 |
+
rag_url = space_url + "/rag"
|
| 10 |
+
insert_url = space_url + "/insert"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to extract text from a PDF file (no changes needed here)
|
| 13 |
def extract_text_from_pdf(pdf_file):
|
| 14 |
pdf_stream = BytesIO(pdf_file)
|
| 15 |
reader = pypdf.PdfReader(pdf_stream)
|
|
|
|
| 20 |
|
| 21 |
# Function to handle PDF upload and insertion into Milvus
|
| 22 |
def upload_and_index_pdf(pdf_file, server_url):
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
files = {'file': (pdf_file.name, open(pdf_file.name, 'rb'), 'application/pdf')}
|
| 25 |
+
response = requests.post(insert_url, files=files, timeout=600)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
return "PDF uploaded and indexed successfully!"
|
| 28 |
except requests.exceptions.RequestException as e:
|
| 29 |
return f"Error during PDF upload: {e}"
|
|
|
|
| 32 |
|
| 33 |
# Function to perform RAG query
|
| 34 |
def perform_rag_query(question, server_url):
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
response = requests.post(rag_url, json={"question": question}, timeout=300)
|
| 37 |
response.raise_for_status()
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"An unexpected error occurred: {e}"
|
| 44 |
|
| 45 |
+
# Example questions
|
| 46 |
+
example_questions = [
|
| 47 |
+
"What are the enabling technologies for GPT?",
|
| 48 |
+
"Explain the potential applications of GPT.",
|
| 49 |
+
"What are some emerging challenges with GPT technology?",
|
| 50 |
+
"Describe the future directions for GPT research."
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
# Gradio interface setup
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown(
|
|
|
|
| 60 |
)
|
| 61 |
with gr.Row():
|
| 62 |
with gr.Column():
|
| 63 |
+
pdf_input = gr.File(label="Upload PDF", type="filepath") # Changed type to "filepath"
|
| 64 |
server_url_input = gr.Textbox(
|
| 65 |
label="Milvus Server URL",
|
| 66 |
+
value=space_url,
|
| 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 |
+
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
| 84 |
question_input = gr.Textbox(label="Ask a question about the PDF")
|
| 85 |
query_button = gr.Button("Ask")
|
| 86 |
+
# Example questions
|
| 87 |
+
gr.Examples(
|
| 88 |
+
examples=example_questions,
|
| 89 |
+
inputs=question_input,
|
| 90 |
+
label="Example Questions",
|
| 91 |
+
)
|
| 92 |
with gr.Column():
|
| 93 |
answer_output = gr.Textbox(label="Answer")
|
| 94 |
|