Spaces:
Sleeping
Sleeping
Initial Kimi-K2-Instruct MCP server space
Browse files- README.md +6 -6
- app.py +186 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title: Kimi
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.5.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Kimi-K2 MCP
|
| 3 |
+
emoji: 🌙
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.5.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
tags:
|
| 11 |
+
- mcp-server
|
| 12 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("moonshotai/Kimi-K2-Instruct")
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def generate_code(
|
| 8 |
+
prompt: str,
|
| 9 |
+
language: str = "python",
|
| 10 |
+
max_tokens: int = 2048,
|
| 11 |
+
) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Generate code using Kimi-K2-Instruct (1T MoE model by Moonshot AI).
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
prompt: Description of the code you want to generate.
|
| 17 |
+
Be specific about what the code should do.
|
| 18 |
+
language: The programming language to generate code in
|
| 19 |
+
(e.g. python, javascript, rust, go, typescript).
|
| 20 |
+
max_tokens: Maximum number of tokens to generate (default 2048).
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
The generated code as a string.
|
| 24 |
+
"""
|
| 25 |
+
system_msg = (
|
| 26 |
+
f"You are an expert {language} programmer. "
|
| 27 |
+
"Return only clean, well-structured code with brief inline comments. "
|
| 28 |
+
"Do not include markdown fences or explanations outside the code."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
response = client.chat_completion(
|
| 32 |
+
messages=[
|
| 33 |
+
{"role": "system", "content": system_msg},
|
| 34 |
+
{"role": "user", "content": prompt},
|
| 35 |
+
],
|
| 36 |
+
max_tokens=max_tokens,
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].message.content
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def review_code(code: str) -> str:
|
| 42 |
+
"""
|
| 43 |
+
Review code for bugs, security issues, and improvements using Kimi-K2-Instruct.
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
code: The source code to review.
|
| 47 |
+
|
| 48 |
+
Returns:
|
| 49 |
+
A concise review with issues found and suggested improvements.
|
| 50 |
+
"""
|
| 51 |
+
response = client.chat_completion(
|
| 52 |
+
messages=[
|
| 53 |
+
{
|
| 54 |
+
"role": "system",
|
| 55 |
+
"content": (
|
| 56 |
+
"You are a senior code reviewer. Analyze the code for: "
|
| 57 |
+
"1) Bugs and logic errors "
|
| 58 |
+
"2) Security vulnerabilities "
|
| 59 |
+
"3) Performance issues "
|
| 60 |
+
"4) Style and best practices. "
|
| 61 |
+
"Be concise and actionable."
|
| 62 |
+
),
|
| 63 |
+
},
|
| 64 |
+
{"role": "user", "content": f"Review this code:\n\n{code}"},
|
| 65 |
+
],
|
| 66 |
+
max_tokens=2048,
|
| 67 |
+
)
|
| 68 |
+
return response.choices[0].message.content
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def explain_code(code: str) -> str:
|
| 72 |
+
"""
|
| 73 |
+
Explain what a piece of code does in plain language using Kimi-K2-Instruct.
|
| 74 |
+
|
| 75 |
+
Args:
|
| 76 |
+
code: The source code to explain.
|
| 77 |
+
|
| 78 |
+
Returns:
|
| 79 |
+
A clear explanation of what the code does.
|
| 80 |
+
"""
|
| 81 |
+
response = client.chat_completion(
|
| 82 |
+
messages=[
|
| 83 |
+
{
|
| 84 |
+
"role": "system",
|
| 85 |
+
"content": (
|
| 86 |
+
"Explain the given code clearly and concisely. "
|
| 87 |
+
"Cover what it does, how it works, and any notable patterns."
|
| 88 |
+
),
|
| 89 |
+
},
|
| 90 |
+
{"role": "user", "content": f"Explain this code:\n\n{code}"},
|
| 91 |
+
],
|
| 92 |
+
max_tokens=1024,
|
| 93 |
+
)
|
| 94 |
+
return response.choices[0].message.content
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def solve_problem(
|
| 98 |
+
problem: str,
|
| 99 |
+
max_tokens: int = 2048,
|
| 100 |
+
) -> str:
|
| 101 |
+
"""
|
| 102 |
+
Solve a coding or reasoning problem step by step using Kimi-K2-Instruct.
|
| 103 |
+
|
| 104 |
+
Args:
|
| 105 |
+
problem: The problem to solve. Can be algorithmic, mathematical,
|
| 106 |
+
or a general coding challenge.
|
| 107 |
+
max_tokens: Maximum number of tokens to generate (default 2048).
|
| 108 |
+
|
| 109 |
+
Returns:
|
| 110 |
+
A step-by-step solution with explanation and code.
|
| 111 |
+
"""
|
| 112 |
+
response = client.chat_completion(
|
| 113 |
+
messages=[
|
| 114 |
+
{
|
| 115 |
+
"role": "system",
|
| 116 |
+
"content": (
|
| 117 |
+
"You are an expert problem solver. "
|
| 118 |
+
"Break down the problem, explain your approach, "
|
| 119 |
+
"then provide a clean solution with code if applicable."
|
| 120 |
+
),
|
| 121 |
+
},
|
| 122 |
+
{"role": "user", "content": problem},
|
| 123 |
+
],
|
| 124 |
+
max_tokens=max_tokens,
|
| 125 |
+
)
|
| 126 |
+
return response.choices[0].message.content
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
demo = gr.TabbedInterface(
|
| 130 |
+
[
|
| 131 |
+
gr.Interface(
|
| 132 |
+
fn=generate_code,
|
| 133 |
+
inputs=[
|
| 134 |
+
gr.Textbox(
|
| 135 |
+
label="Prompt",
|
| 136 |
+
placeholder="Describe the code you want...",
|
| 137 |
+
lines=4,
|
| 138 |
+
),
|
| 139 |
+
gr.Textbox(label="Language", value="python"),
|
| 140 |
+
gr.Slider(
|
| 141 |
+
minimum=256, maximum=4096, value=2048, step=256,
|
| 142 |
+
label="Max Tokens",
|
| 143 |
+
),
|
| 144 |
+
],
|
| 145 |
+
outputs=gr.Code(label="Generated Code"),
|
| 146 |
+
title="Code Generator",
|
| 147 |
+
api_name="generate_code",
|
| 148 |
+
),
|
| 149 |
+
gr.Interface(
|
| 150 |
+
fn=review_code,
|
| 151 |
+
inputs=gr.Code(label="Code to Review", language="python", lines=15),
|
| 152 |
+
outputs=gr.Textbox(label="Review", lines=15),
|
| 153 |
+
title="Code Reviewer",
|
| 154 |
+
api_name="review_code",
|
| 155 |
+
),
|
| 156 |
+
gr.Interface(
|
| 157 |
+
fn=explain_code,
|
| 158 |
+
inputs=gr.Code(label="Code to Explain", language="python", lines=15),
|
| 159 |
+
outputs=gr.Textbox(label="Explanation", lines=10),
|
| 160 |
+
title="Code Explainer",
|
| 161 |
+
api_name="explain_code",
|
| 162 |
+
),
|
| 163 |
+
gr.Interface(
|
| 164 |
+
fn=solve_problem,
|
| 165 |
+
inputs=[
|
| 166 |
+
gr.Textbox(
|
| 167 |
+
label="Problem",
|
| 168 |
+
placeholder="Describe the problem to solve...",
|
| 169 |
+
lines=6,
|
| 170 |
+
),
|
| 171 |
+
gr.Slider(
|
| 172 |
+
minimum=256, maximum=4096, value=2048, step=256,
|
| 173 |
+
label="Max Tokens",
|
| 174 |
+
),
|
| 175 |
+
],
|
| 176 |
+
outputs=gr.Textbox(label="Solution", lines=15),
|
| 177 |
+
title="Problem Solver",
|
| 178 |
+
api_name="solve_problem",
|
| 179 |
+
),
|
| 180 |
+
],
|
| 181 |
+
["Generate", "Review", "Explain", "Solve"],
|
| 182 |
+
title="Kimi-K2-Instruct Coding Assistant (MCP)",
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
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
|