| import os |
| import gradio as gr |
| from huggingface_hub import InferenceClient |
| import spaces |
|
|
| |
| |
| @spaces.GPU |
| def dummy_gpu_probe(): |
| return "ZeroGPU Initialised" |
|
|
| |
| client = InferenceClient( |
| model="moonshotai/Kimi-K3", |
| token=os.environ.get("HF_TOKEN") |
| ) |
|
|
| def respond(message, chat_history): |
| messages = [{"role": "system", "content": "You are Kimi-K3, a native multimodal agentic frontier model developed by Moonshot AI."}] |
| |
| for val in chat_history: |
| if val: |
| messages.append({"role": "user", "content": val}) |
| if val: |
| messages.append({"role": "assistant", "content": val}) |
| |
| messages.append({"role": "user", "content": message}) |
| |
| response = "" |
| try: |
| for message_chunk in client.chat_completion( |
| messages, |
| max_tokens=2048, |
| stream=True, |
| temperature=0.7, |
| top_p=0.95, |
| ): |
| token = message_chunk.choices.delta.content |
| if token: |
| response += token |
| yield response |
| except Exception as e: |
| yield f"Error calling Hugging Face Inference Provider: {str(e)}. Ensure your HF_TOKEN is configured correctly in Space Secrets." |
|
|
| |
| demo = gr.ChatInterface( |
| fn=respond, |
| title="🤖 Kimi-K3 Demo Space", |
| description="Interface for Moonshot AI's Kimi-K3 2.8T Parameter Frontier Model.", |
| examples=["Write an optimized GPU kernel processing loop.", "Draft a comprehensive research document comparing MoE architectures."], |
| textbox=gr.Textbox(placeholder="Ask Kimi-K3 anything...", container=False, scale=7), |
| ) |
|
|
| if __name__ == "__main__": |
| |
| dummy_gpu_probe() |
| demo.launch() |
|
|