Spaces:
Sleeping
Sleeping
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) | |