Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import pypdf
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# **IMPORTANT:** Replace this with your actual Hugging Face Space URL
|
| 8 |
+
# Example: "https://ruslanmv-milvus-server.hf.space"
|
| 9 |
+
# "https://huggingface.co/spaces/ruslanmv/Milvus-Server/rag"
|
| 10 |
+
# "https://ruslanmv-milvus-server.hf.space/rag"
|
| 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)
|
| 20 |
+
text = ""
|
| 21 |
+
for page in reader.pages:
|
| 22 |
+
text += page.extract_text()
|
| 23 |
+
return text
|
| 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) # Increased timeout
|
| 32 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 33 |
+
return "PDF uploaded and indexed successfully!"
|
| 34 |
+
except requests.exceptions.RequestException as e:
|
| 35 |
+
return f"Error during PDF upload: {e}"
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"An unexpected error occurred: {e}"
|
| 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()
|
| 46 |
+
results = response.json().get("result", [])
|
| 47 |
+
return "\n".join(results)
|
| 48 |
+
except requests.exceptions.RequestException as e:
|
| 49 |
+
return f"Error during RAG query: {e}"
|
| 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(
|
| 56 |
+
"""
|
| 57 |
+
# Milvus PDF Search Client
|
| 58 |
+
Upload a PDF to index it in Milvus, then ask questions about its content.
|
| 59 |
+
"""
|
| 60 |
+
)
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column():
|
| 63 |
+
pdf_input = gr.File(label="Upload PDF", type="file")
|
| 64 |
+
server_url_input = gr.Textbox(
|
| 65 |
+
label="Milvus Server URL",
|
| 66 |
+
value=space_url, # Use the environment variable or default
|
| 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 |
+
|
| 80 |
+
upload_button.click(
|
| 81 |
+
fn=upload_and_index_pdf,
|
| 82 |
+
inputs=[pdf_input, server_url_input],
|
| 83 |
+
outputs=upload_output,
|
| 84 |
+
)
|
| 85 |
+
query_button.click(
|
| 86 |
+
fn=perform_rag_query,
|
| 87 |
+
inputs=[question_input, server_url_input],
|
| 88 |
+
outputs=answer_output,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
demo.launch()
|