File size: 3,246 Bytes
01bc500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42df2c5
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
92
93
94
95
96
97
98
99
100
import gradio as gr
import os
import subprocess
import shutil
from pathlib import Path
import time

# Function to handle file upload and ingestion
def upload_and_ingest(uploaded_file):
    if uploaded_file is None:
        return "No file uploaded."
    
    try:
        # Create the pdf_docs directory if it doesn't exist
        pdf_docs_dir = "/home/tony/pdf_docs"
        os.makedirs(pdf_docs_dir, exist_ok=True)
        
        # Copy uploaded file to pdf_docs directory
        filename = os.path.basename(uploaded_file.name)
        file_path = os.path.join(pdf_docs_dir, filename)
        shutil.copy2(uploaded_file.name, file_path)
        
        # Run the ingestion script and capture output
        result = subprocess.run(
            ["python", "/home/tony/ingest.py"],
            cwd="/home/tony",
            capture_output=True, 
            text=True
        )
        
        if result.returncode == 0:
            return f"βœ… File '{filename}' uploaded and ingested successfully!\n\nIngestion Output:\n{result.stdout}"
        else:
            return f"❌ Error during ingestion:\n{result.stderr}\n\nStdout:\n{result.stdout}"
            
    except Exception as e:
        return f"❌ Error: {str(e)}"

# Function to handle Google Drive folder link (placeholder for now)
def link_gdrive_folder(folder_link):
    if not folder_link or not folder_link.strip():
        return "Please provide a Google Drive folder link."
    
    # TODO: Implement Google Drive integration
    return f"🚧 Google Drive integration coming soon!\nFolder link: {folder_link}"

# Create Gradio Interface
with gr.Blocks(title="PDF Ingestion Tool", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸ“š PDF Ingestion Tool")
    gr.Markdown("Upload PDF files or link Google Drive folders to ingest into the medical knowledge base.")
    
    with gr.Tab("File Upload"):
        with gr.Row():
            file_input = gr.File(
                label="Upload PDF File",
                file_types=[".pdf"],
                type="filepath"
            )
        
        upload_btn = gr.Button("Upload & Ingest", variant="primary")
        upload_output = gr.Textbox(
            label="Ingestion Status",
            lines=10,
            max_lines=20,
            show_copy_button=True
        )
        
        upload_btn.click(
            fn=upload_and_ingest,
            inputs=[file_input],
            outputs=[upload_output],
            show_progress=True
        )
    
    with gr.Tab("Google Drive"):
        with gr.Row():
            gdrive_input = gr.Textbox(
                label="Google Drive Folder Link",
                placeholder="https://drive.google.com/drive/folders/...",
                lines=1
            )
        
        gdrive_btn = gr.Button("Link & Ingest", variant="primary")
        gdrive_output = gr.Textbox(
            label="Status",
            lines=10,
            max_lines=20,
            show_copy_button=True
        )
        
        gdrive_btn.click(
            fn=link_gdrive_folder,
            inputs=[gdrive_input],
            outputs=[gdrive_output],
            show_progress=True
        )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)