|
|
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") |
|
|
|
|
|
|
|
|
threading.Thread(target=start_fastapi, daemon=True).start() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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_UPLOAD_PATH = "data/docs" |
|
|
DEFAULT_UPLOAD_FILES = [] |
|
|
DEFAULT_OPTIMIZE_JSON = model_to_json(OptimizeRequest) |
|
|
DEFAULT_AUTOTUNE_JSON = model_to_json(AutotuneRequest) |
|
|
DEFAULT_QA_JSON = model_to_json(QARequest) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown("# Ragmint MCP Client") |
|
|
|
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("## Upload Documents\nUpload files to a folder on the server.") |
|
|
upload_files = gr.File( |
|
|
file_types=[".txt", ".md", ".pdf"], |
|
|
file_count="multiple", |
|
|
type="filepath", |
|
|
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("---") |
|
|
|
|
|
|
|
|
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("---") |
|
|
|
|
|
|
|
|
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("---") |
|
|
|
|
|
|
|
|
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) |
|
|
|