File size: 4,401 Bytes
59e6760 9d761b8 ec21b79 59e6760 ec21b79 59e6760 ec21b79 59e6760 9d761b8 59e6760 9d761b8 59e6760 9d761b8 59e6760 9d761b8 ec21b79 9d761b8 ec21b79 9d761b8 ec21b79 9d761b8 ec21b79 9d761b8 59e6760 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import gradio as gr
import requests
import json
from models import OptimizeRequest, AutotuneRequest, QARequest
import os
import threading
import uvicorn
from api import app as fastapi_app
API_URL = "http://127.0.0.1:7861"
def start_fastapi():
uvicorn.run(fastapi_app, host="0.0.0.0", port=7861, log_level="info")
# Start FastAPI in a background thread
threading.Thread(target=start_fastapi, daemon=True).start()
# -------------------------------
# Helper to call API
# -------------------------------
def call_api(endpoint: str, payload: dict) -> str:
try:
r = requests.post(f"{API_URL}/{endpoint}", json=payload)
return json.dumps(r.json(), indent=2)
except Exception as e:
return str(e)
# -------------------------------
# MCP Tool Wrappers
# -------------------------------
def upload_docs_tool(files, docs_path="data/docs"):
os.makedirs(docs_path, exist_ok=True)
uploaded_files = []
for file_path in files:
filename = os.path.basename(file_path)
dest_path = os.path.join(docs_path, filename)
import shutil
shutil.copy(file_path, dest_path)
uploaded_files.append(filename)
return {"status": "ok", "uploaded_files": uploaded_files, "docs_path": docs_path}
def optimize_rag_tool(payload: str) -> str:
return call_api("optimize_rag", json.loads(payload))
def autotune_tool(payload: str) -> str:
return call_api("autotune_rag", json.loads(payload))
def generate_qa_tool(payload: str) -> str:
return call_api("generate_validation_qa", json.loads(payload))
# -------------------------------
# Generate default JSON for models
# -------------------------------
def model_to_json(model_cls) -> str:
defaults = {k: v.default for k, v in model_cls.__fields__.items()}
return json.dumps(defaults, indent=2)
# Default payloads
DEFAULT_UPLOAD_PATH = "data/docs"
DEFAULT_UPLOAD_FILES = [] # No files by default
DEFAULT_OPTIMIZE_JSON = model_to_json(OptimizeRequest)
DEFAULT_AUTOTUNE_JSON = model_to_json(AutotuneRequest)
DEFAULT_QA_JSON = model_to_json(QARequest)
# -------------------------------
# Build Gradio interface
# -------------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Ragmint MCP Client")
# Upload files section
with gr.Column():
gr.Markdown("## Upload Documents\nUpload files to a folder on the server.")
upload_files = gr.File(
file_types=[".txt", ".md", ".pdf"], # allowed extensions
file_count="multiple", # allow multiple files
type="filepath", # returns local file path
label="Drag & Drop Files"
)
upload_path = gr.Textbox(value=DEFAULT_UPLOAD_PATH, label="Docs Path")
upload_btn = gr.Button("Upload",variant='primary')
upload_output = gr.Textbox(label="Response")
upload_btn.click(upload_docs_tool, inputs=[upload_files, upload_path], outputs=upload_output)
gr.Markdown("---")
# Optimize RAG
with gr.Column():
gr.Markdown("## Optimize RAG\nRun full RAG optimization with custom parameters.")
optimize_input = gr.Textbox(lines=12, value=DEFAULT_OPTIMIZE_JSON, label="OptimizeRequest JSON")
optimize_btn = gr.Button("Submit",variant='primary')
optimize_output = gr.Textbox(lines=15, label="Response")
optimize_btn.click(optimize_rag_tool, inputs=optimize_input, outputs=optimize_output)
gr.Markdown("---")
# Autotune RAG
with gr.Column():
gr.Markdown("## Autotune RAG\nRun AutoRAG tuner and full optimization.")
autotune_input = gr.Textbox(lines=12, value=DEFAULT_AUTOTUNE_JSON, label="AutotuneRequest JSON")
autotune_btn = gr.Button("Submit",variant='primary')
autotune_output = gr.Textbox(lines=15, label="Response")
autotune_btn.click(autotune_tool, inputs=autotune_input, outputs=autotune_output)
gr.Markdown("---")
# Generate QA
with gr.Column():
gr.Markdown("## Generate QA\nGenerate validation QA dataset from documents.")
qa_input = gr.Textbox(lines=12, value=DEFAULT_QA_JSON, label="QARequest JSON")
qa_btn = gr.Button("Submit",variant='primary')
qa_output = gr.Textbox(lines=15, label="Response")
qa_btn.click(generate_qa_tool, inputs=qa_input, outputs=qa_output)
gr.Markdown("---")
demo.launch(mcp_server=True)
|