Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Initialize the client pointing to MoonshotAI Kimi-K3
|
| 6 |
+
# Automatically grabs the secret token from the Space environment
|
| 7 |
+
client = InferenceClient(
|
| 8 |
+
model="moonshotai/Kimi-K3",
|
| 9 |
+
token=os.environ.get("HF_TOKEN")
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def respond(message, chat_history):
|
| 13 |
+
# Format system and chat history for the agentic model
|
| 14 |
+
messages = [{"role": "system", "content": "You are Kimi-K3, a native multimodal agentic frontier model developed by Moonshot AI."}]
|
| 15 |
+
|
| 16 |
+
for val in chat_history:
|
| 17 |
+
if val[0]:
|
| 18 |
+
messages.append({"role": "user", "content": val[0]})
|
| 19 |
+
if val[1]:
|
| 20 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 21 |
+
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
+
# Stream the output chunks safely
|
| 25 |
+
response = ""
|
| 26 |
+
try:
|
| 27 |
+
for message_chunk in client.chat_completion(
|
| 28 |
+
messages,
|
| 29 |
+
max_tokens=2048,
|
| 30 |
+
stream=True,
|
| 31 |
+
temperature=0.7,
|
| 32 |
+
top_p=0.95,
|
| 33 |
+
):
|
| 34 |
+
token = message_chunk.choices[0].delta.content
|
| 35 |
+
if token:
|
| 36 |
+
response += token
|
| 37 |
+
yield response
|
| 38 |
+
except Exception as e:
|
| 39 |
+
yield f"Error calling Hugging Face Inference Provider: {str(e)}. Ensure your HF_TOKEN is configured correctly in Space Secrets."
|
| 40 |
+
|
| 41 |
+
# Build a clean, modern Gradio Chat interface
|
| 42 |
+
demo = gr.ChatInterface(
|
| 43 |
+
fn=respond,
|
| 44 |
+
title="🤖 Kimi-K3 Demo Space",
|
| 45 |
+
description="Interface for Moonshot AI's Kimi-K3 2.8T Parameter Frontier Model.",
|
| 46 |
+
examples=["Write an optimized GPU kernel processing loop.", "Draft a comprehensive research document comparing MoE architectures."],
|
| 47 |
+
textbox=gr.Textbox(placeholder="Ask Kimi-K3 anything...", container=False, scale=7),
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
| 52 |
+
|