File size: 3,481 Bytes
d37e507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
import requests
from io import BytesIO
import pypdf
import os

# **IMPORTANT:** Replace this with your actual Hugging Face Space URL
# Example: "https://ruslanmv-milvus-server.hf.space" 
#           "https://huggingface.co/spaces/ruslanmv/Milvus-Server/rag"
#           "https://ruslanmv-milvus-server.hf.space/rag"
#space_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space")  # or set it here
#rag_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") + "/rag"
#insert_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space") + "/insert"
space_url = os.environ.get("SPACE_URL", "https://ruslanmv-milvus-server.hf.space")

# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_file):
    pdf_stream = BytesIO(pdf_file)
    reader = pypdf.PdfReader(pdf_stream)
    text = ""
    for page in reader.pages:
        text += page.extract_text()
    return text

# Function to handle PDF upload and insertion into Milvus
def upload_and_index_pdf(pdf_file, server_url):
    #insert_url = server_url.rstrip("/") + "/insert"
    insert_url = server_url + "/insert"
    try:
        files = {'file': (pdf_file.name, open(pdf_file.name, 'rb'), 'application/pdf')}
        response = requests.post(insert_url, files=files, timeout=600)  # Increased timeout
        response.raise_for_status()  # Raise an exception for bad status codes
        return "PDF uploaded and indexed successfully!"
    except requests.exceptions.RequestException as e:
        return f"Error during PDF upload: {e}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"

# Function to perform RAG query
def perform_rag_query(question, server_url):
    #rag_url = server_url.rstrip("/") + "/rag"
    rag_url = server_url + "/rag"
    try:
        response = requests.post(rag_url, json={"question": question}, timeout=300)
        response.raise_for_status()
        results = response.json().get("result", [])
        return "\n".join(results)
    except requests.exceptions.RequestException as e:
        return f"Error during RAG query: {e}"
    except Exception as e:
        return f"An unexpected error occurred: {e}"

# Gradio interface setup
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # Milvus PDF Search Client
        Upload a PDF to index it in Milvus, then ask questions about its content.
        """
    )
    with gr.Row():
        with gr.Column():
            pdf_input = gr.File(label="Upload PDF", type="file")
            server_url_input = gr.Textbox(
                label="Milvus Server URL",
                value=space_url,  # Use the environment variable or default
                placeholder="Enter your Milvus Server URL"
            )
            upload_button = gr.Button("Upload and Index PDF")
        with gr.Column():
            upload_output = gr.Textbox(label="Upload Status")
    
    with gr.Row():
        with gr.Column():
            question_input = gr.Textbox(label="Ask a question about the PDF")
            query_button = gr.Button("Ask")
        with gr.Column():
            answer_output = gr.Textbox(label="Answer")

    upload_button.click(
        fn=upload_and_index_pdf,
        inputs=[pdf_input, server_url_input],
        outputs=upload_output,
    )
    query_button.click(
        fn=perform_rag_query,
        inputs=[question_input, server_url_input],
        outputs=answer_output,
    )

demo.launch()