André Oliveira
commited on
Commit
·
4f1961d
1
Parent(s):
5519564
fix: uploads on client
Browse files
app.py
CHANGED
|
@@ -5,14 +5,12 @@ import os
|
|
| 5 |
import shutil
|
| 6 |
import threading
|
| 7 |
import mimetypes
|
| 8 |
-
import base64
|
| 9 |
from models import OptimizeRequest, AutotuneRequest, QARequest
|
| 10 |
from api import start_api
|
| 11 |
-
from gradio_client import FileData
|
| 12 |
|
| 13 |
threading.Thread(target=start_api, daemon=True).start()
|
| 14 |
|
| 15 |
-
# Base URL for internal
|
| 16 |
BASE_INTERNAL = "http://127.0.0.1:8000"
|
| 17 |
|
| 18 |
|
|
@@ -27,67 +25,49 @@ def call_api(endpoint: str, payload: dict) -> str:
|
|
| 27 |
def upload_docs_tool(files, docs_path="data/docs"):
|
| 28 |
"""
|
| 29 |
Upload documents to the server's docs folder.
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
- URLs (http/https)
|
| 34 |
-
Returns MCP-compatible FileData dicts.
|
| 35 |
"""
|
| 36 |
os.makedirs(docs_path, exist_ok=True)
|
| 37 |
saved = []
|
| 38 |
|
| 39 |
for f in files:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
content = base64.b64decode(f["data"])
|
| 43 |
-
fname = f.get("orig_name", f.get("name", "unknown_file"))
|
| 44 |
-
dest = os.path.join(docs_path, fname)
|
| 45 |
-
with open(dest, "wb") as out_file:
|
| 46 |
-
out_file.write(content)
|
| 47 |
-
|
| 48 |
-
# ----- URL -----
|
| 49 |
-
elif isinstance(f, str) and (f.startswith("http://") or f.startswith("https://")):
|
| 50 |
r = requests.get(f, stream=True)
|
| 51 |
-
fname = f.split("/")[-1]
|
| 52 |
dest = os.path.join(docs_path, fname)
|
| 53 |
with open(dest, "wb") as out_file:
|
| 54 |
shutil.copyfileobj(r.raw, out_file)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
elif isinstance(f, str) and os.path.exists(f):
|
| 58 |
fname = os.path.basename(f)
|
| 59 |
dest = os.path.join(docs_path, fname)
|
| 60 |
shutil.copy(f, dest)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
mime_type = mimetypes.guess_type(dest)[0] or "application/octet-stream"
|
| 66 |
-
saved.append({
|
| 67 |
-
"path": dest,
|
| 68 |
-
"url": f"file://{os.path.abspath(dest)}",
|
| 69 |
-
"orig_name": os.path.basename(dest),
|
| 70 |
-
"mime_type": mime_type,
|
| 71 |
-
"is_stream": False,
|
| 72 |
-
"meta": {}
|
| 73 |
-
})
|
| 74 |
|
| 75 |
return {"status": "ok", "uploaded_files": saved, "docs_path": docs_path}
|
| 76 |
|
| 77 |
|
| 78 |
def optimize_rag_tool(payload: str) -> str:
|
|
|
|
| 79 |
return call_api("/optimize_rag", json.loads(payload))
|
| 80 |
|
| 81 |
|
| 82 |
def autotune_tool(payload: str) -> str:
|
|
|
|
| 83 |
return call_api("/autotune_rag", json.loads(payload))
|
| 84 |
|
| 85 |
|
| 86 |
def generate_qa_tool(payload: str) -> str:
|
|
|
|
| 87 |
return call_api("/generate_validation_qa", json.loads(payload))
|
| 88 |
|
| 89 |
|
| 90 |
-
# Dynamically assign Pydantic docstrings
|
| 91 |
optimize_rag_tool.__doc__ = OptimizeRequest.__doc__
|
| 92 |
autotune_tool.__doc__ = AutotuneRequest.__doc__
|
| 93 |
generate_qa_tool.__doc__ = QARequest.__doc__
|
|
@@ -109,8 +89,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 109 |
# Upload Documents
|
| 110 |
with gr.Column():
|
| 111 |
gr.Markdown("## Upload Documents")
|
| 112 |
-
gr.Markdown("📂
|
| 113 |
-
upload_files = gr.File(file_count="multiple", type="
|
| 114 |
upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
|
| 115 |
upload_btn = gr.Button("Upload", variant="primary")
|
| 116 |
upload_out = gr.JSON(label="Response")
|
|
|
|
| 5 |
import shutil
|
| 6 |
import threading
|
| 7 |
import mimetypes
|
|
|
|
| 8 |
from models import OptimizeRequest, AutotuneRequest, QARequest
|
| 9 |
from api import start_api
|
|
|
|
| 10 |
|
| 11 |
threading.Thread(target=start_api, daemon=True).start()
|
| 12 |
|
| 13 |
+
# Base URL for internal calls
|
| 14 |
BASE_INTERNAL = "http://127.0.0.1:8000"
|
| 15 |
|
| 16 |
|
|
|
|
| 25 |
def upload_docs_tool(files, docs_path="data/docs"):
|
| 26 |
"""
|
| 27 |
Upload documents to the server's docs folder.
|
| 28 |
+
|
| 29 |
+
Accepts local file paths or URLs. Returns a list of uploaded file paths (strings)
|
| 30 |
+
to ensure MCP compatibility.
|
|
|
|
|
|
|
| 31 |
"""
|
| 32 |
os.makedirs(docs_path, exist_ok=True)
|
| 33 |
saved = []
|
| 34 |
|
| 35 |
for f in files:
|
| 36 |
+
if f.startswith("http://") or f.startswith("https://"):
|
| 37 |
+
# Download file from URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
r = requests.get(f, stream=True)
|
| 39 |
+
fname = f.split("/")[-1]
|
| 40 |
dest = os.path.join(docs_path, fname)
|
| 41 |
with open(dest, "wb") as out_file:
|
| 42 |
shutil.copyfileobj(r.raw, out_file)
|
| 43 |
+
else:
|
| 44 |
+
# Local file path
|
|
|
|
| 45 |
fname = os.path.basename(f)
|
| 46 |
dest = os.path.join(docs_path, fname)
|
| 47 |
shutil.copy(f, dest)
|
| 48 |
|
| 49 |
+
# Only store the file path string for MCP compatibility
|
| 50 |
+
saved.append(dest)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return {"status": "ok", "uploaded_files": saved, "docs_path": docs_path}
|
| 53 |
|
| 54 |
|
| 55 |
def optimize_rag_tool(payload: str) -> str:
|
| 56 |
+
"""🔧 Explicit optimization request: user provides all pipeline configs manually."""
|
| 57 |
return call_api("/optimize_rag", json.loads(payload))
|
| 58 |
|
| 59 |
|
| 60 |
def autotune_tool(payload: str) -> str:
|
| 61 |
+
"""🔧 Autotune RAG: recommends chunk sizes and embedding models automatically."""
|
| 62 |
return call_api("/autotune_rag", json.loads(payload))
|
| 63 |
|
| 64 |
|
| 65 |
def generate_qa_tool(payload: str) -> str:
|
| 66 |
+
"""🧩 Generates a validation QA dataset for RAG evaluation."""
|
| 67 |
return call_api("/generate_validation_qa", json.loads(payload))
|
| 68 |
|
| 69 |
|
| 70 |
+
# Dynamically assign Pydantic model docstrings to MCP tool functions
|
| 71 |
optimize_rag_tool.__doc__ = OptimizeRequest.__doc__
|
| 72 |
autotune_tool.__doc__ = AutotuneRequest.__doc__
|
| 73 |
generate_qa_tool.__doc__ = QARequest.__doc__
|
|
|
|
| 89 |
# Upload Documents
|
| 90 |
with gr.Column():
|
| 91 |
gr.Markdown("## Upload Documents")
|
| 92 |
+
gr.Markdown("📂 Upload files (local paths or URLs) to your `data/docs` folder")
|
| 93 |
+
upload_files = gr.File(file_count="multiple", type="filepath")
|
| 94 |
upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
|
| 95 |
upload_btn = gr.Button("Upload", variant="primary")
|
| 96 |
upload_out = gr.JSON(label="Response")
|