André Oliveira
commited on
Commit
·
94a9110
1
Parent(s):
4f9b2d4
refactor: document upload allows FileData
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import json
|
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
import threading
|
|
|
|
| 7 |
from models import OptimizeRequest, AutotuneRequest, QARequest
|
| 8 |
from api import start_api
|
| 9 |
|
|
@@ -25,20 +26,35 @@ def upload_docs_tool(files, docs_path="data/docs"):
|
|
| 25 |
"""
|
| 26 |
Upload documents to the server's docs folder.
|
| 27 |
|
| 28 |
-
|
| 29 |
-
- files: List of file paths to upload
|
| 30 |
-
- docs_path: Destination folder (default: data/docs)
|
| 31 |
-
|
| 32 |
-
Returns:
|
| 33 |
-
- dict with uploaded filenames and docs_path
|
| 34 |
"""
|
| 35 |
os.makedirs(docs_path, exist_ok=True)
|
| 36 |
saved = []
|
|
|
|
| 37 |
for f in files:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return {"status": "ok", "uploaded_files": saved, "docs_path": docs_path}
|
| 43 |
|
| 44 |
|
|
@@ -79,11 +95,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 79 |
# Upload Documents
|
| 80 |
with gr.Column():
|
| 81 |
gr.Markdown("## Upload Documents")
|
| 82 |
-
gr.Markdown("📂 Upload files to your `data/docs` folder")
|
| 83 |
upload_files = gr.File(file_count="multiple", type="filepath")
|
| 84 |
upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
|
| 85 |
upload_btn = gr.Button("Upload", variant="primary")
|
| 86 |
-
upload_out = gr.
|
| 87 |
upload_btn.click(upload_docs_tool, inputs=[upload_files, upload_path], outputs=upload_out)
|
| 88 |
gr.Markdown("---")
|
| 89 |
|
|
|
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
import threading
|
| 7 |
+
import mimetypes
|
| 8 |
from models import OptimizeRequest, AutotuneRequest, QARequest
|
| 9 |
from api import start_api
|
| 10 |
|
|
|
|
| 26 |
"""
|
| 27 |
Upload documents to the server's docs folder.
|
| 28 |
|
| 29 |
+
Accepts local file paths or URLs. Returns a list of MCP FileData-like dicts.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
os.makedirs(docs_path, exist_ok=True)
|
| 32 |
saved = []
|
| 33 |
+
|
| 34 |
for f in files:
|
| 35 |
+
if f.startswith("http://") or f.startswith("https://"):
|
| 36 |
+
# Download file from URL
|
| 37 |
+
r = requests.get(f, stream=True)
|
| 38 |
+
fname = f.split("/")[-1]
|
| 39 |
+
dest = os.path.join(docs_path, fname)
|
| 40 |
+
with open(dest, "wb") as out_file:
|
| 41 |
+
shutil.copyfileobj(r.raw, out_file)
|
| 42 |
+
else:
|
| 43 |
+
# Local file path
|
| 44 |
+
fname = os.path.basename(f)
|
| 45 |
+
dest = os.path.join(docs_path, fname)
|
| 46 |
+
shutil.copy(f, dest)
|
| 47 |
+
|
| 48 |
+
mime_type = mimetypes.guess_type(dest)[0] or "application/octet-stream"
|
| 49 |
+
saved.append({
|
| 50 |
+
"path": dest,
|
| 51 |
+
"url": f"file://{os.path.abspath(dest)}",
|
| 52 |
+
"orig_name": fname,
|
| 53 |
+
"mime_type": mime_type,
|
| 54 |
+
"is_stream": False,
|
| 55 |
+
"meta": {}
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
return {"status": "ok", "uploaded_files": saved, "docs_path": docs_path}
|
| 59 |
|
| 60 |
|
|
|
|
| 95 |
# Upload Documents
|
| 96 |
with gr.Column():
|
| 97 |
gr.Markdown("## Upload Documents")
|
| 98 |
+
gr.Markdown("📂 Upload files (local paths or URLs) to your `data/docs` folder")
|
| 99 |
upload_files = gr.File(file_count="multiple", type="filepath")
|
| 100 |
upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
|
| 101 |
upload_btn = gr.Button("Upload", variant="primary")
|
| 102 |
+
upload_out = gr.JSON(label="Response")
|
| 103 |
upload_btn.click(upload_docs_tool, inputs=[upload_files, upload_path], outputs=upload_out)
|
| 104 |
gr.Markdown("---")
|
| 105 |
|