André Oliveira commited on
Commit
9d761b8
·
1 Parent(s): e86c88e

refactor: new gradio app

Browse files
Files changed (2) hide show
  1. api.py +18 -1
  2. app.py +99 -35
api.py CHANGED
@@ -5,9 +5,10 @@ import logging
5
  import time
6
 
7
  from models import OptimizeRequest, QARequest, AutotuneRequest
8
- from fastapi import FastAPI, HTTPException
9
  from fastapi.middleware.cors import CORSMiddleware
10
  import uvicorn
 
11
 
12
  try:
13
  from ragmint.autotuner import AutoRAGTuner
@@ -56,6 +57,22 @@ def health():
56
  }
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  @app.post("/optimize_rag")
60
  def optimize_rag(req: OptimizeRequest):
61
  logger.info("Received optimize_rag request: %s", req.json())
 
5
  import time
6
 
7
  from models import OptimizeRequest, QARequest, AutotuneRequest
8
+ from fastapi import FastAPI, HTTPException, UploadFile, File, Form
9
  from fastapi.middleware.cors import CORSMiddleware
10
  import uvicorn
11
+ import shutil
12
 
13
  try:
14
  from ragmint.autotuner import AutoRAGTuner
 
57
  }
58
 
59
 
60
+ @app.post("/upload_docs")
61
+ async def upload_docs(
62
+ docs_path: str = Form(...), # User specifies folder
63
+ files: list[UploadFile] = File(...)
64
+ ):
65
+ os.makedirs(docs_path, exist_ok=True) # create folder if missing
66
+ saved_files = []
67
+
68
+ for file in files:
69
+ file_path = os.path.join(docs_path, file.filename)
70
+ with open(file_path, "wb") as f:
71
+ shutil.copyfileobj(file.file, f)
72
+ saved_files.append(file.filename)
73
+
74
+ return {"status": "ok", "uploaded_files": saved_files, "docs_path": docs_path}
75
+
76
  @app.post("/optimize_rag")
77
  def optimize_rag(req: OptimizeRequest):
78
  logger.info("Received optimize_rag request: %s", req.json())
app.py CHANGED
@@ -1,50 +1,114 @@
1
  import gradio as gr
2
  import requests
3
  import json
4
- import server
 
5
 
6
  API_URL = "http://localhost:8000"
7
 
8
- def optimize_rag_tool(payload: str) -> str:
9
- """Run RAGMint full optimization workflow.
10
 
11
- Args:
12
- payload: JSON string containing OptimizeRequest parameters.
 
 
 
 
 
 
 
13
 
14
- Returns:
15
- JSON result with best config and leaderboard stats.
16
- """
17
- r = requests.post(f"{API_URL}/optimize_rag", json=json.loads(payload))
18
- return json.dumps(r.json(), indent=2)
19
 
20
- def autotune_tool(payload: str) -> str:
21
- """Run AutoRAG tuner to recommend best configs and optimize.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- Args:
24
- payload: JSON string for AutotuneRequest
25
 
26
- Returns:
27
- JSON result for tuning and full optimization.
28
- """
29
- r = requests.post(f"{API_URL}/autotune_rag", json=json.loads(payload))
30
- return json.dumps(r.json(), indent=2)
 
 
31
 
32
  def generate_qa_tool(payload: str) -> str:
33
- """Generate validation QA set automatically with Gemini or Anthropic.
34
-
35
- Args:
36
- payload: JSON string for QARequest
37
-
38
- Returns:
39
- JSON preview of generated dataset
40
- """
41
- r = requests.post(f"{API_URL}/generate_validation_qa", json=json.loads(payload))
42
- return json.dumps(r.json(), indent=2)
43
-
44
- demo = gr.Interface(
45
- fn=optimize_rag_tool,
46
- inputs=gr.Textbox(lines=12, label="OptimizeRequest JSON"),
47
- outputs=gr.Textbox(label="Response")
48
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  demo.launch(mcp_server=True)
 
1
  import gradio as gr
2
  import requests
3
  import json
4
+ from models import OptimizeRequest, AutotuneRequest, QARequest
5
+ import os
6
 
7
  API_URL = "http://localhost:8000"
8
 
 
 
9
 
10
+ # -------------------------------
11
+ # Helper to call API
12
+ # -------------------------------
13
+ def call_api(endpoint: str, payload: dict) -> str:
14
+ try:
15
+ r = requests.post(f"{API_URL}/{endpoint}", json=payload)
16
+ return json.dumps(r.json(), indent=2)
17
+ except Exception as e:
18
+ return str(e)
19
 
 
 
 
 
 
20
 
21
+ # -------------------------------
22
+ # MCP Tool Wrappers
23
+ # -------------------------------
24
+ def upload_docs_tool(files, docs_path="data/docs"):
25
+
26
+ os.makedirs(docs_path, exist_ok=True)
27
+ uploaded_files = []
28
+
29
+ for file_path in files:
30
+ filename = os.path.basename(file_path)
31
+ dest_path = os.path.join(docs_path, filename)
32
+ import shutil
33
+ shutil.copy(file_path, dest_path)
34
+ uploaded_files.append(filename)
35
+
36
+ return {"status": "ok", "uploaded_files": uploaded_files, "docs_path": docs_path}
37
 
 
 
38
 
39
+ def optimize_rag_tool(payload: str) -> str:
40
+ return call_api("optimize_rag", json.loads(payload))
41
+
42
+
43
+ def autotune_tool(payload: str) -> str:
44
+ return call_api("autotune_rag", json.loads(payload))
45
+
46
 
47
  def generate_qa_tool(payload: str) -> str:
48
+ return call_api("generate_validation_qa", json.loads(payload))
49
+
50
+
51
+ # -------------------------------
52
+ # Generate default JSON for models
53
+ # -------------------------------
54
+ def model_to_json(model_cls) -> str:
55
+ defaults = {k: v.default for k, v in model_cls.__fields__.items()}
56
+ return json.dumps(defaults, indent=2)
57
+
58
+
59
+ # Default payloads
60
+ DEFAULT_UPLOAD_PATH = "data/docs"
61
+ DEFAULT_UPLOAD_FILES = [] # No files by default
62
+ DEFAULT_OPTIMIZE_JSON = model_to_json(OptimizeRequest)
63
+ DEFAULT_AUTOTUNE_JSON = model_to_json(AutotuneRequest)
64
+ DEFAULT_QA_JSON = model_to_json(QARequest)
65
+
66
+ # -------------------------------
67
+ # Build Gradio interface
68
+ # -------------------------------
69
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
+ gr.Markdown("# Ragmint MCP Client")
71
+
72
+ # Upload files section
73
+ with gr.Column():
74
+ gr.Markdown("## Upload Documents\nUpload files to a folder on the server.")
75
+ upload_files = gr.File(
76
+ file_types=[".txt", ".md", ".pdf"], # allowed extensions
77
+ file_count="multiple", # allow multiple files
78
+ type="filepath", # returns local file path
79
+ label="Drag & Drop Files"
80
+ )
81
+ upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
82
+ upload_output = gr.Textbox(label="Response")
83
+ upload_btn = gr.Button("Upload",variant='primary')
84
+ upload_btn.click(upload_docs_tool, inputs=[upload_files, upload_path], outputs=upload_output)
85
+ gr.Markdown("---")
86
+
87
+ # Optimize RAG
88
+ with gr.Column():
89
+ gr.Markdown("## Optimize RAG\nRun full RAG optimization with custom parameters.")
90
+ optimize_input = gr.Textbox(lines=12, value=DEFAULT_OPTIMIZE_JSON, label="OptimizeRequest JSON")
91
+ optimize_output = gr.Textbox(lines=15, label="Response")
92
+ optimize_btn = gr.Button("Submit",variant='primary')
93
+ optimize_btn.click(optimize_rag_tool, inputs=optimize_input, outputs=optimize_output)
94
+ gr.Markdown("---")
95
+
96
+ # Autotune RAG
97
+ with gr.Column():
98
+ gr.Markdown("## Autotune RAG\nRun AutoRAG tuner and full optimization.")
99
+ autotune_input = gr.Textbox(lines=12, value=DEFAULT_AUTOTUNE_JSON, label="AutotuneRequest JSON")
100
+ autotune_output = gr.Textbox(lines=15, label="Response")
101
+ autotune_btn = gr.Button("Submit",variant='primary')
102
+ autotune_btn.click(autotune_tool, inputs=autotune_input, outputs=autotune_output)
103
+ gr.Markdown("---")
104
+
105
+ # Generate QA
106
+ with gr.Column():
107
+ gr.Markdown("## Generate QA\nGenerate validation QA dataset from documents.")
108
+ qa_input = gr.Textbox(lines=12, value=DEFAULT_QA_JSON, label="QARequest JSON")
109
+ qa_output = gr.Textbox(lines=15, label="Response")
110
+ qa_btn = gr.Button("Submit",variant='primary')
111
+ qa_btn.click(generate_qa_tool, inputs=qa_input, outputs=qa_output)
112
+ gr.Markdown("---")
113
 
114
  demo.launch(mcp_server=True)