akhaliq HF Staff commited on
Commit
f0ef68c
·
verified ·
1 Parent(s): 07b80de

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +8 -7
  2. __pycache__/app.cpython-314.pyc +0 -0
  3. app.py +100 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
  title: Kimi K3 Chat
3
- emoji: 🏃
4
- colorFrom: blue
5
- colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 6.20.0
8
- python_version: '3.13'
9
  app_file: app.py
10
- pinned: false
 
 
 
 
11
  ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Kimi K3 Chat
3
+ emoji: 🌙
4
+ colorFrom: indigo
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 6.20.0
 
8
  app_file: app.py
9
+ short_description: Chat with Moonshot AI's Kimi K3 via Inference Providers
10
+ python_version: "3.12"
11
+ hf_oauth: true
12
+ hf_oauth_scopes:
13
+ - inference-api
14
  ---
 
 
__pycache__/app.cpython-314.pyc ADDED
Binary file (5.63 kB). View file
 
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import mimetypes
3
+
4
+ import gradio as gr
5
+ from huggingface_hub import InferenceClient
6
+
7
+ MODEL = "moonshotai/Kimi-K3"
8
+ PROVIDER = "together"
9
+
10
+ SYSTEM_PROMPT = (
11
+ "You are Kimi K3, Moonshot AI's open-weight native multimodal agentic model. "
12
+ "Be helpful, concise, and accurate."
13
+ )
14
+
15
+
16
+ def to_data_url(path: str) -> str:
17
+ mime = mimetypes.guess_type(path)[0] or "image/png"
18
+ with open(path, "rb") as f:
19
+ b64 = base64.b64encode(f.read()).decode()
20
+ return f"data:{mime};base64,{b64}"
21
+
22
+
23
+ def respond(message, history, token: gr.OAuthToken | None):
24
+ """Chat with Kimi K3 (Moonshot AI) via HF Inference Providers. Supports text and images."""
25
+ if token is None:
26
+ yield "Please sign in with your Hugging Face account (sidebar) to chat — inference is billed to your own account."
27
+ return
28
+
29
+ client = InferenceClient(api_key=token.token, provider=PROVIDER, timeout=600)
30
+
31
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
32
+ for msg in history:
33
+ if msg["role"] == "assistant" and isinstance(msg.get("content"), str):
34
+ # strip rendered reasoning before sending back
35
+ content = msg["content"]
36
+ if "</think>" in content:
37
+ content = content.split("</think>", 1)[1]
38
+ messages.append({"role": "assistant", "content": content.strip()})
39
+ elif msg["role"] == "user":
40
+ messages.append({"role": "user", "content": msg["content"]})
41
+
42
+ content = []
43
+ for path in message.get("files", []):
44
+ content.append({"type": "image_url", "image_url": {"url": to_data_url(path)}})
45
+ if message.get("text"):
46
+ content.append({"type": "text", "text": message["text"]})
47
+ messages.append({"role": "user", "content": content if len(content) > 1 else (message.get("text") or "")})
48
+
49
+ stream = client.chat.completions.create(
50
+ model=MODEL,
51
+ messages=messages,
52
+ max_tokens=8192,
53
+ stream=True,
54
+ )
55
+
56
+ reasoning, answer, in_think = "", "", False
57
+ for chunk in stream:
58
+ if not chunk.choices:
59
+ continue
60
+ delta = chunk.choices[0].delta
61
+ r = getattr(delta, "reasoning_content", None)
62
+ if r:
63
+ reasoning += r
64
+ if delta.content:
65
+ answer += delta.content
66
+ out = ""
67
+ if reasoning:
68
+ out += f"<think>{reasoning}</think>"
69
+ out += answer
70
+ yield out
71
+
72
+
73
+ with gr.Blocks(title="Kimi K3 Chat", fill_height=True) as demo:
74
+ with gr.Sidebar():
75
+ gr.LoginButton("Sign in with Hugging Face")
76
+ gr.Markdown(
77
+ "**Kimi K3** — Moonshot AI's open 3T-class multimodal agentic model "
78
+ "(2.8T params, 104B active MoE, 1M context).\n\n"
79
+ "Served via [HF Inference Providers](https://huggingface.co/moonshotai/Kimi-K3) "
80
+ "(Together AI). Sign-in required; usage is billed to your own HF account.\n\n"
81
+ "🖼️ Attach images to try multimodal chat."
82
+ )
83
+ gr.ChatInterface(
84
+ fn=respond,
85
+ multimodal=True,
86
+ chatbot=gr.Chatbot(reasoning_tags=[("<think>", "</think>")], scale=1),
87
+ textbox=gr.MultimodalTextbox(
88
+ placeholder="Message Kimi K3 — attach an image for multimodal chat…",
89
+ file_types=["image"],
90
+ sources=["upload", "clipboard"],
91
+ ),
92
+ examples=[
93
+ {"text": "Explain mixture-of-experts routing like I'm five."},
94
+ {"text": "Write a Python function that streams tokens from an SSE endpoint."},
95
+ {"text": "What are the tradeoffs of MXFP4 quantization for a 3T-param model?"},
96
+ ],
97
+ cache_examples=False,
98
+ )
99
+
100
+ demo.launch(mcp_server=True)