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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("moonshotai/Kimi-K2-Instruct")
|
| 5 |
+
|
| 6 |
+
ARCHITECT_SYSTEM = (
|
| 7 |
+
"You are a senior software architect specializing in Python/FastAPI backends, "
|
| 8 |
+
"Next.js/React frontends, and LangGraph/LangChain AI systems. "
|
| 9 |
+
"You think in systems — data flow, component boundaries, and interfaces. "
|
| 10 |
+
"You always consider: scalability, testability, separation of concerns, "
|
| 11 |
+
"and developer experience. "
|
| 12 |
+
"Output file trees when relevant. Use markdown tables for comparisons. "
|
| 13 |
+
"Reference specific patterns: repository pattern, service layer, CQRS, "
|
| 14 |
+
"event-driven architecture, App Router conventions, React Server Components. "
|
| 15 |
+
"Be opinionated but justify your choices."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def analyze_design(
|
| 20 |
+
requirements: str,
|
| 21 |
+
stack: str = "Python/FastAPI + Next.js + LangGraph",
|
| 22 |
+
max_tokens: int = 2048,
|
| 23 |
+
) -> str:
|
| 24 |
+
"""
|
| 25 |
+
Produce an architecture plan from requirements.
|
| 26 |
+
Outputs component diagram, data flow, API design, and tech decisions.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
requirements: Project requirements or feature description.
|
| 30 |
+
Be as detailed as possible for better results.
|
| 31 |
+
stack: Tech stack to use (default "Python/FastAPI + Next.js + LangGraph").
|
| 32 |
+
max_tokens: Maximum tokens for the response (default 2048).
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
Architecture plan with components, data flow, API design, and rationale.
|
| 36 |
+
"""
|
| 37 |
+
response = client.chat_completion(
|
| 38 |
+
messages=[
|
| 39 |
+
{"role": "system", "content": ARCHITECT_SYSTEM},
|
| 40 |
+
{
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": (
|
| 43 |
+
f"Requirements:\n{requirements}\n\n"
|
| 44 |
+
f"Stack: {stack}\n\n"
|
| 45 |
+
"Produce an architecture plan covering:\n"
|
| 46 |
+
"1. **Components**: List each service/module with its responsibility\n"
|
| 47 |
+
"2. **Data Flow**: How data moves through the system\n"
|
| 48 |
+
"3. **API Design**: Key endpoints with method, path, request/response\n"
|
| 49 |
+
"4. **File Structure**: Directory tree for the project\n"
|
| 50 |
+
"5. **Tech Decisions**: Libraries chosen and why\n"
|
| 51 |
+
"6. **Risks**: What could go wrong and mitigations"
|
| 52 |
+
),
|
| 53 |
+
},
|
| 54 |
+
],
|
| 55 |
+
max_tokens=max_tokens,
|
| 56 |
+
)
|
| 57 |
+
return response.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def suggest_patterns(
|
| 61 |
+
problem: str,
|
| 62 |
+
stack: str = "Python/FastAPI",
|
| 63 |
+
max_tokens: int = 2048,
|
| 64 |
+
) -> str:
|
| 65 |
+
"""
|
| 66 |
+
Recommend design patterns for a specific problem and stack.
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
problem: The design problem to solve (e.g. "handle complex business logic
|
| 70 |
+
with multiple validation steps", "manage state across LangGraph nodes").
|
| 71 |
+
stack: The technology stack context. One of: "Python/FastAPI",
|
| 72 |
+
"Next.js/React", "LangGraph/LangChain", or a combination.
|
| 73 |
+
|
| 74 |
+
Returns:
|
| 75 |
+
Recommended patterns with code examples and trade-offs.
|
| 76 |
+
"""
|
| 77 |
+
response = client.chat_completion(
|
| 78 |
+
messages=[
|
| 79 |
+
{"role": "system", "content": ARCHITECT_SYSTEM},
|
| 80 |
+
{
|
| 81 |
+
"role": "user",
|
| 82 |
+
"content": (
|
| 83 |
+
f"Problem: {problem}\n"
|
| 84 |
+
f"Stack: {stack}\n\n"
|
| 85 |
+
"Recommend design patterns. For each pattern:\n"
|
| 86 |
+
"1. **Name** and brief description\n"
|
| 87 |
+
"2. **Why** it fits this problem\n"
|
| 88 |
+
"3. **Code example** (concise, idiomatic for the stack)\n"
|
| 89 |
+
"4. **Trade-offs** (when NOT to use it)\n\n"
|
| 90 |
+
"Suggest 2-3 patterns ranked by fit."
|
| 91 |
+
),
|
| 92 |
+
},
|
| 93 |
+
],
|
| 94 |
+
max_tokens=max_tokens,
|
| 95 |
+
)
|
| 96 |
+
return response.choices[0].message.content
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def plan_implementation(
|
| 100 |
+
task: str,
|
| 101 |
+
stack: str = "Python/FastAPI + Next.js + LangGraph",
|
| 102 |
+
max_tokens: int = 2048,
|
| 103 |
+
) -> str:
|
| 104 |
+
"""
|
| 105 |
+
Create a step-by-step implementation plan with file structure.
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
task: The feature or project to implement (e.g. "RAG pipeline with
|
| 109 |
+
document upload, chunking, embedding, and retrieval via FastAPI").
|
| 110 |
+
stack: Tech stack to use (default "Python/FastAPI + Next.js + LangGraph").
|
| 111 |
+
max_tokens: Maximum tokens for the response (default 2048).
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
Ordered implementation steps with file paths, dependencies, and code snippets.
|
| 115 |
+
"""
|
| 116 |
+
response = client.chat_completion(
|
| 117 |
+
messages=[
|
| 118 |
+
{"role": "system", "content": ARCHITECT_SYSTEM},
|
| 119 |
+
{
|
| 120 |
+
"role": "user",
|
| 121 |
+
"content": (
|
| 122 |
+
f"Task: {task}\n"
|
| 123 |
+
f"Stack: {stack}\n\n"
|
| 124 |
+
"Create a step-by-step implementation plan:\n"
|
| 125 |
+
"1. **File tree**: Complete directory structure\n"
|
| 126 |
+
"2. **Dependencies**: All packages needed (requirements.txt / package.json)\n"
|
| 127 |
+
"3. **Implementation order**: Numbered steps, each with:\n"
|
| 128 |
+
" - File to create/modify\n"
|
| 129 |
+
" - What to implement\n"
|
| 130 |
+
" - Key code snippet or interface\n"
|
| 131 |
+
"4. **Integration points**: How components connect\n"
|
| 132 |
+
"5. **Verification**: How to test each step works"
|
| 133 |
+
),
|
| 134 |
+
},
|
| 135 |
+
],
|
| 136 |
+
max_tokens=max_tokens,
|
| 137 |
+
)
|
| 138 |
+
return response.choices[0].message.content
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def review_architecture(
|
| 142 |
+
code: str,
|
| 143 |
+
max_tokens: int = 2048,
|
| 144 |
+
) -> str:
|
| 145 |
+
"""
|
| 146 |
+
Review existing code for architectural issues, coupling, and scalability.
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
code: The source code to review architecturally.
|
| 150 |
+
Can be a single file or multiple files separated by file path comments.
|
| 151 |
+
|
| 152 |
+
Returns:
|
| 153 |
+
Architectural review with issues, recommendations, and refactoring suggestions.
|
| 154 |
+
"""
|
| 155 |
+
response = client.chat_completion(
|
| 156 |
+
messages=[
|
| 157 |
+
{"role": "system", "content": ARCHITECT_SYSTEM},
|
| 158 |
+
{
|
| 159 |
+
"role": "user",
|
| 160 |
+
"content": (
|
| 161 |
+
f"Review this code architecturally:\n\n{code}\n\n"
|
| 162 |
+
"Analyze:\n"
|
| 163 |
+
"1. **Coupling**: Are components too tightly coupled?\n"
|
| 164 |
+
"2. **Cohesion**: Does each module have a single responsibility?\n"
|
| 165 |
+
"3. **Scalability**: What breaks under load?\n"
|
| 166 |
+
"4. **Testability**: Can each component be tested in isolation?\n"
|
| 167 |
+
"5. **Patterns**: Are appropriate patterns used (or missing)?\n"
|
| 168 |
+
"6. **Recommendations**: Specific refactoring steps, ordered by impact"
|
| 169 |
+
),
|
| 170 |
+
},
|
| 171 |
+
],
|
| 172 |
+
max_tokens=max_tokens,
|
| 173 |
+
)
|
| 174 |
+
return response.choices[0].message.content
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
demo = gr.TabbedInterface(
|
| 178 |
+
[
|
| 179 |
+
gr.Interface(
|
| 180 |
+
fn=analyze_design,
|
| 181 |
+
inputs=[
|
| 182 |
+
gr.Textbox(label="Requirements", placeholder="Describe the project or feature...", lines=6),
|
| 183 |
+
gr.Textbox(label="Stack", value="Python/FastAPI + Next.js + LangGraph"),
|
| 184 |
+
gr.Slider(minimum=256, maximum=4096, value=2048, step=256, label="Max Tokens"),
|
| 185 |
+
],
|
| 186 |
+
outputs=gr.Textbox(label="Architecture Plan", lines=20),
|
| 187 |
+
title="Analyze Design",
|
| 188 |
+
api_name="analyze_design",
|
| 189 |
+
),
|
| 190 |
+
gr.Interface(
|
| 191 |
+
fn=suggest_patterns,
|
| 192 |
+
inputs=[
|
| 193 |
+
gr.Textbox(label="Problem", placeholder="Describe the design problem...", lines=4),
|
| 194 |
+
gr.Textbox(label="Stack", value="Python/FastAPI"),
|
| 195 |
+
],
|
| 196 |
+
outputs=gr.Textbox(label="Pattern Recommendations", lines=20),
|
| 197 |
+
title="Suggest Patterns",
|
| 198 |
+
api_name="suggest_patterns",
|
| 199 |
+
),
|
| 200 |
+
gr.Interface(
|
| 201 |
+
fn=plan_implementation,
|
| 202 |
+
inputs=[
|
| 203 |
+
gr.Textbox(label="Task", placeholder="What to implement...", lines=4),
|
| 204 |
+
gr.Textbox(label="Stack", value="Python/FastAPI + Next.js + LangGraph"),
|
| 205 |
+
gr.Slider(minimum=256, maximum=4096, value=2048, step=256, label="Max Tokens"),
|
| 206 |
+
],
|
| 207 |
+
outputs=gr.Textbox(label="Implementation Plan", lines=20),
|
| 208 |
+
title="Plan Implementation",
|
| 209 |
+
api_name="plan_implementation",
|
| 210 |
+
),
|
| 211 |
+
gr.Interface(
|
| 212 |
+
fn=review_architecture,
|
| 213 |
+
inputs=gr.Code(label="Code to Review", language="python", lines=20),
|
| 214 |
+
outputs=gr.Textbox(label="Architectural Review", lines=20),
|
| 215 |
+
title="Review Architecture",
|
| 216 |
+
api_name="review_architecture",
|
| 217 |
+
),
|
| 218 |
+
],
|
| 219 |
+
["Design", "Patterns", "Plan", "Review"],
|
| 220 |
+
title="Kimi Architect (MCP)",
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
if __name__ == "__main__":
|
| 224 |
+
demo.launch(mcp_server=True, ssr_mode=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]>=5.0
|
| 2 |
+
huggingface_hub>=0.25.0
|