André Oliveira commited on
Commit
5519564
·
1 Parent(s): 97f2a62

fix: uploads on client

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -5,12 +5,14 @@ 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
14
  BASE_INTERNAL = "http://127.0.0.1:8000"
15
 
16
 
@@ -25,31 +27,46 @@ def call_api(endpoint: str, payload: dict) -> str:
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 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": {}
@@ -59,21 +76,18 @@ def upload_docs_tool(files, docs_path="data/docs"):
59
 
60
 
61
  def optimize_rag_tool(payload: str) -> str:
62
- """🔧 Explicit optimization request: user provides all pipeline configs manually."""
63
  return call_api("/optimize_rag", json.loads(payload))
64
 
65
 
66
  def autotune_tool(payload: str) -> str:
67
- """🔧 Autotune RAG: recommends chunk sizes and embedding models automatically."""
68
  return call_api("/autotune_rag", json.loads(payload))
69
 
70
 
71
  def generate_qa_tool(payload: str) -> str:
72
- """🧩 Generates a validation QA dataset for RAG evaluation."""
73
  return call_api("/generate_validation_qa", json.loads(payload))
74
 
75
 
76
- # Dynamically assign Pydantic model docstrings to MCP tool functions
77
  optimize_rag_tool.__doc__ = OptimizeRequest.__doc__
78
  autotune_tool.__doc__ = AutotuneRequest.__doc__
79
  generate_qa_tool.__doc__ = QARequest.__doc__
@@ -95,8 +109,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
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")
 
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 API calls
16
  BASE_INTERNAL = "http://127.0.0.1:8000"
17
 
18
 
 
27
  def upload_docs_tool(files, docs_path="data/docs"):
28
  """
29
  Upload documents to the server's docs folder.
30
+ Accepts:
31
+ - MCP FileData objects
32
+ - Local file paths
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
+ # ----- MCP FileData -----
41
+ if isinstance(f, dict) and "data" in f:
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] or "downloaded_file"
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
+ # ----- Local file path -----
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
+ else:
63
+ continue
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": {}
 
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
  # Upload Documents
110
  with gr.Column():
111
  gr.Markdown("## Upload Documents")
112
+ gr.Markdown("📂 Accepts MCP FileData, local paths, or URLs")
113
+ upload_files = gr.File(file_count="multiple", type="file") # MCP FileData compatible
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")