André Oliveira
commited on
Commit
·
170863a
1
Parent(s):
4f1961d
fix: uploads on client
Browse files
app.py
CHANGED
|
@@ -2,12 +2,11 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 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 |
|
|
|
|
| 11 |
threading.Thread(target=start_api, daemon=True).start()
|
| 12 |
|
| 13 |
# Base URL for internal calls
|
|
@@ -24,32 +23,45 @@ def call_api(endpoint: str, payload: dict) -> str:
|
|
| 24 |
|
| 25 |
def upload_docs_tool(files, docs_path="data/docs"):
|
| 26 |
"""
|
| 27 |
-
Upload documents to the server's docs folder.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
"""
|
|
|
|
|
|
|
| 32 |
os.makedirs(docs_path, exist_ok=True)
|
| 33 |
-
|
| 34 |
|
| 35 |
for f in files:
|
| 36 |
-
if f.startswith("http://") or f.startswith("https://"):
|
| 37 |
-
# Download
|
| 38 |
-
r = requests.get(f, stream=True)
|
| 39 |
fname = f.split("/")[-1]
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
shutil.copyfileobj(r.raw, out_file)
|
| 43 |
-
|
|
|
|
| 44 |
# Local file path
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Only store the file path string for MCP compatibility
|
| 50 |
-
saved.append(dest)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
def optimize_rag_tool(payload: str) -> str:
|
|
@@ -67,7 +79,7 @@ def generate_qa_tool(payload: str) -> str:
|
|
| 67 |
return call_api("/generate_validation_qa", json.loads(payload))
|
| 68 |
|
| 69 |
|
| 70 |
-
#
|
| 71 |
optimize_rag_tool.__doc__ = OptimizeRequest.__doc__
|
| 72 |
autotune_tool.__doc__ = AutotuneRequest.__doc__
|
| 73 |
generate_qa_tool.__doc__ = QARequest.__doc__
|
|
@@ -83,6 +95,7 @@ DEFAULT_OPTIMIZE_JSON = model_to_json(OptimizeRequest)
|
|
| 83 |
DEFAULT_AUTOTUNE_JSON = model_to_json(AutotuneRequest)
|
| 84 |
DEFAULT_QA_JSON = model_to_json(QARequest)
|
| 85 |
|
|
|
|
| 86 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 87 |
gr.Markdown("# Ragmint MCP Client")
|
| 88 |
|
|
@@ -113,7 +126,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 113 |
gr.Markdown(AutotuneRequest.__doc__ or "No description available.")
|
| 114 |
autotune_input = gr.Textbox(lines=12, value=DEFAULT_AUTOTUNE_JSON, label="AutotuneRequest JSON")
|
| 115 |
autotune_btn = gr.Button("Submit", variant="primary")
|
| 116 |
-
autotune_out = gr.Textbox(lines=15)
|
| 117 |
autotune_btn.click(autotune_tool, inputs=autotune_input, outputs=autotune_out)
|
| 118 |
gr.Markdown("---")
|
| 119 |
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
import threading
|
|
|
|
| 6 |
from models import OptimizeRequest, AutotuneRequest, QARequest
|
| 7 |
from api import start_api
|
| 8 |
|
| 9 |
+
# Start FastAPI server in background
|
| 10 |
threading.Thread(target=start_api, daemon=True).start()
|
| 11 |
|
| 12 |
# Base URL for internal calls
|
|
|
|
| 23 |
|
| 24 |
def upload_docs_tool(files, docs_path="data/docs"):
|
| 25 |
"""
|
| 26 |
+
Upload documents to the server's docs folder via FastAPI /upload_docs.
|
| 27 |
+
Accepts:
|
| 28 |
+
- local file paths (str)
|
| 29 |
+
- URLs (str)
|
| 30 |
+
- file-like objects (from Cursor attachment)
|
| 31 |
"""
|
| 32 |
+
import shutil
|
| 33 |
+
|
| 34 |
os.makedirs(docs_path, exist_ok=True)
|
| 35 |
+
files_payload = []
|
| 36 |
|
| 37 |
for f in files:
|
| 38 |
+
if isinstance(f, str) and (f.startswith("http://") or f.startswith("https://")):
|
| 39 |
+
# Download URL temporarily
|
|
|
|
| 40 |
fname = f.split("/")[-1]
|
| 41 |
+
tmp_path = os.path.join("/tmp", fname)
|
| 42 |
+
r = requests.get(f, stream=True)
|
| 43 |
+
with open(tmp_path, "wb") as out_file:
|
| 44 |
shutil.copyfileobj(r.raw, out_file)
|
| 45 |
+
files_payload.append(("files", open(tmp_path, "rb")))
|
| 46 |
+
elif isinstance(f, str):
|
| 47 |
# Local file path
|
| 48 |
+
files_payload.append(("files", open(f, "rb")))
|
| 49 |
+
else:
|
| 50 |
+
# Assume file-like object (Cursor attachment)
|
| 51 |
+
files_payload.append(("files", f))
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
try:
|
| 54 |
+
resp = requests.post(
|
| 55 |
+
f"{BASE_INTERNAL}/upload_docs",
|
| 56 |
+
files=files_payload,
|
| 57 |
+
data={"docs_path": docs_path}
|
| 58 |
+
)
|
| 59 |
+
return resp.json()
|
| 60 |
+
finally:
|
| 61 |
+
# Close all file handles
|
| 62 |
+
for _, file_obj in files_payload:
|
| 63 |
+
if not file_obj.closed:
|
| 64 |
+
file_obj.close()
|
| 65 |
|
| 66 |
|
| 67 |
def optimize_rag_tool(payload: str) -> str:
|
|
|
|
| 79 |
return call_api("/generate_validation_qa", json.loads(payload))
|
| 80 |
|
| 81 |
|
| 82 |
+
# Assign Pydantic docstrings
|
| 83 |
optimize_rag_tool.__doc__ = OptimizeRequest.__doc__
|
| 84 |
autotune_tool.__doc__ = AutotuneRequest.__doc__
|
| 85 |
generate_qa_tool.__doc__ = QARequest.__doc__
|
|
|
|
| 95 |
DEFAULT_AUTOTUNE_JSON = model_to_json(AutotuneRequest)
|
| 96 |
DEFAULT_QA_JSON = model_to_json(QARequest)
|
| 97 |
|
| 98 |
+
|
| 99 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 100 |
gr.Markdown("# Ragmint MCP Client")
|
| 101 |
|
|
|
|
| 126 |
gr.Markdown(AutotuneRequest.__doc__ or "No description available.")
|
| 127 |
autotune_input = gr.Textbox(lines=12, value=DEFAULT_AUTOTUNE_JSON, label="AutotuneRequest JSON")
|
| 128 |
autotune_btn = gr.Button("Submit", variant="primary")
|
| 129 |
+
autotune_out = gr.Textbox(lines=15, label="Response")
|
| 130 |
autotune_btn.click(autotune_tool, inputs=autotune_input, outputs=autotune_out)
|
| 131 |
gr.Markdown("---")
|
| 132 |
|