File size: 2,453 Bytes
c33d1d0
018fb30
6184bc1
 
 
e8faa1f
6184bc1
 
b08204e
6184bc1
70bd277
6184bc1
 
ea07eae
6184bc1
 
 
 
 
 
 
 
 
e8faa1f
6184bc1
 
e8faa1f
6184bc1
 
 
 
 
e8faa1f
6184bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8faa1f
6184bc1
 
 
 
 
e8faa1f
6184bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8faa1f
 
 
 
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
import os
import gradio as gr
import warnings
from pathlib import Path
import shutil

# Suppress LangChain deprecation warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

from rag_tool import RAGTool

# Initialize the RAG Tool
rag_tool = RAGTool()

# Function to handle document uploads
def upload_file(file):
    try:
        # Create documents directory if it doesn't exist
        os.makedirs("./documents", exist_ok=True)
        
        # Get the file path and name
        file_path = Path(file.name)
        destination = Path("./documents") / file_path.name
        
        # Copy the file to documents directory
        shutil.copy(file_path, destination)
        
        # Configure RAG tool
        rag_tool.configure(
            documents_path=str(destination),
            embedding_model="sentence-transformers/all-MiniLM-L6-v2",
            persist_directory="./vector_store"
        )
        
        return f"File uploaded and processed: {file_path.name}"
    except Exception as e:
        return f"Error processing file: {str(e)}"

# Function to query the documents
def query_document(question):
    try:
        if not hasattr(rag_tool, 'vector_store') or rag_tool.vector_store is None:
            return "Please upload a document first."
        
        response = rag_tool(question)
        return response
    except Exception as e:
        return f"Error querying document: {str(e)}"

# Create a simple Gradio interface
with gr.Blocks(title="RAG Tool") as demo:
    gr.Markdown("# Document Question Answering System")
    gr.Markdown("Upload a document (PDF, TXT) and ask questions about it")
    
    with gr.Row():
        with gr.Column():
            file_input = gr.File(label="Upload Document")
            upload_button = gr.Button("Process Document")
            upload_result = gr.Textbox(label="Upload Status")
        
        with gr.Column():
            query_input = gr.Textbox(label="Ask a Question", placeholder="What would you like to know?")
            query_button = gr.Button("Get Answer")
            response_output = gr.Textbox(label="Answer")
    
    # Set up the button click events
    upload_button.click(
        upload_file,
        inputs=file_input,
        outputs=upload_result
    )
    
    query_button.click(
        query_document,
        inputs=query_input,
        outputs=response_output
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()