EGYADMIN/kimi-k2-thinking-dev
0
1import gradio as gr2import os3from huggingface_hub import InferenceClient4 5# Model configuration - Using Inference API6MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3"7DEFAULT_SYSTEM_PROMPT = "You are a helpful AI assistant powered by Mistral."8client = None9 10def init_client():11 """Initialize the Hugging Face Inference Client"""""12 global client13 hf_token = os.environ.get("HF_TOKEN")14 if hf_token:15 client = InferenceClient(token=hf_token)16 print("Inference client initialized successfully")17 return True18 else:19 print("Warning: HF_TOKEN not found. Please set it in Space secrets.")20 return False21 22def generate_response(message, history, system_prompt, max_tokens, temperature):23 """Generate response using Hugging Face Inference API"""""24 global client25 26 if client is None:27 if not init_client():28 return "Error: HF_TOKEN not configured. Please add it in Space settings."29 30 try:31 # Build messages32 messages = [{"role": "system", "content": system_prompt or DEFAULT_SYSTEM_PROMPT}]33 34 for h in history:35 if h[0]:36 messages.append({"role": "user", "content": h[0]})37 if h[1]:38 messages.append({"role": "assistant", "content": h[1]})39 40 messages.append({"role": "user", "content": message})41 42 # Call Inference API43 response = client.chat_completion(44 model=MODEL_NAME,45 messages=messages,46 max_tokens=int(max_tokens),47 temperature=float(temperature)48 )49 50 return response.choices[0].message.content51 52 except Exception as e:53 return f"Error: {str(e)}"54 55# Create interface56print("===== Kimi K2 Thinking Dev =====")57print(f"Using Inference API with model: {MODEL_NAME}")58 59# Initialize client at startup60client_ready = init_client()61 62with gr.Blocks(title="Kimi-K2 Chat", theme=gr.themes.Soft()) as iface:63 gr.Markdown("""64 # ๐ค Kimi-K2 Instruct Chat65 **Powered by Hugging Face Inference API**66 67 This space uses the Kimi-K2-Instruct quantized model via API for efficient inference.68 """)69 70 if not client_ready:71 gr.Markdown("โ ๏ธ **Warning:** HF_TOKEN not found. Please configure it in Space secrets.")72 73 chatbot = gr.Chatbot(height=450, label="Chat")74 75 with gr.Row():76 msg = gr.Textbox(77 placeholder="Type your message here...",78 label="Your Message",79 scale=4,80 lines=281 )82 submit_btn = gr.Button("Send ๐", variant="primary", scale=1)83 84 with gr.Accordion("โ๏ธ Settings", open=False):85 system_prompt = gr.Textbox(86 value=DEFAULT_SYSTEM_PROMPT,87 label="System Prompt",88 lines=289 )90 with gr.Row():91 max_tokens = gr.Slider(92 minimum=64,93 maximum=2048,94 value=512,95 step=64,96 label="Max Tokens"97 )98 temperature = gr.Slider(99 minimum=0.1,100 maximum=2.0,101 value=0.7,102 step=0.1,103 label="Temperature"104 )105 106 clear_btn = gr.Button("๐๏ธ Clear Chat")107 108 def respond(message, history, system_prompt, max_tokens, temperature):109 if not message.strip():110 return "", history111 response = generate_response(message, history, system_prompt, max_tokens, temperature)112 history.append((message, response))113 return "", history114 115 msg.submit(respond, [msg, chatbot, system_prompt, max_tokens, temperature], [msg, chatbot])116 submit_btn.click(respond, [msg, chatbot, system_prompt, max_tokens, temperature], [msg, chatbot])117 clear_btn.click(lambda: [], None, chatbot)118 119if __name__ == "__main__":120 iface.launch(server_name="0.0.0.0", server_port=7860)