Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,36 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
streamer = None
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
-
"moonshotai/Kimi-K2-Instruct",
|
| 22 |
-
trust_remote_code=True,
|
| 23 |
-
torch_dtype=torch.float16,
|
| 24 |
-
low_cpu_mem_usage=True,
|
| 25 |
-
use_auth_token=token
|
| 26 |
-
).eval()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
return gr.update(visible=False), gr.update(visible=True), "✅ Model loaded! Start chatting.", token
|
| 30 |
-
except Exception as e:
|
| 31 |
-
return gr.update(visible=True), gr.update(visible=False), f"❌ Error: {str(e)}", ""
|
| 32 |
|
| 33 |
-
# Format chat
|
| 34 |
def format_prompt(history, user_input):
|
| 35 |
system_prompt = "You are Kimi, a helpful and conversational AI assistant."
|
| 36 |
history_text = "\n".join([f"User: {u}\nAI: {a}" for u, a in history])
|
| 37 |
return f"{system_prompt}\n{history_text}\nUser: {user_input}\nAI:"
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
if model is None or tokenizer is None:
|
| 42 |
-
return history, history, "❌ Model not loaded yet. Please enter your token."
|
| 43 |
-
|
| 44 |
prompt = format_prompt(history, user_input)
|
| 45 |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 46 |
|
|
@@ -53,30 +43,22 @@ def chat(user_input, history, token):
|
|
| 53 |
top_p=0.9,
|
| 54 |
pad_token_id=tokenizer.eos_token_id,
|
| 55 |
)
|
| 56 |
-
|
| 57 |
response = tokenizer.decode(output[0], skip_special_tokens=True).split("AI:")[-1].strip()
|
| 58 |
history.append((user_input, response))
|
| 59 |
-
return history, history
|
| 60 |
|
| 61 |
-
#
|
| 62 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 63 |
-
gr.Markdown("#
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
token_status = gr.Textbox(visible=False)
|
| 69 |
-
chatbot = gr.Chatbot(label="Kimi-K2 Chat", visible=False)
|
| 70 |
state = gr.State([])
|
| 71 |
-
saved_token = gr.State("")
|
| 72 |
-
|
| 73 |
-
with gr.Row(visible=False) as chat_row:
|
| 74 |
-
msg = gr.Textbox(placeholder="Type your message...", scale=10)
|
| 75 |
-
send = gr.Button("Send", scale=2)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
send.click(chat, inputs=[msg, state, saved_token], outputs=[chatbot, state, token_status])
|
| 80 |
-
msg.submit(chat, inputs=[msg, state, saved_token], outputs=[chatbot, state, token_status])
|
| 81 |
|
| 82 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Automatically load token from secret
|
| 7 |
+
hf_token = os.environ.get("HF_TOKEN")
|
|
|
|
| 8 |
|
| 9 |
+
# Load model
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
+
"moonshotai/Kimi-K2-Instruct",
|
| 12 |
+
use_auth_token=hf_token,
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
"moonshotai/Kimi-K2-Instruct",
|
| 18 |
+
trust_remote_code=True,
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
low_cpu_mem_usage=True,
|
| 21 |
+
use_auth_token=hf_token
|
| 22 |
+
).eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Format and chat
|
| 27 |
def format_prompt(history, user_input):
|
| 28 |
system_prompt = "You are Kimi, a helpful and conversational AI assistant."
|
| 29 |
history_text = "\n".join([f"User: {u}\nAI: {a}" for u, a in history])
|
| 30 |
return f"{system_prompt}\n{history_text}\nUser: {user_input}\nAI:"
|
| 31 |
|
| 32 |
+
def chat(user_input, history):
|
| 33 |
+
history = history or []
|
|
|
|
|
|
|
|
|
|
| 34 |
prompt = format_prompt(history, user_input)
|
| 35 |
inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 36 |
|
|
|
|
| 43 |
top_p=0.9,
|
| 44 |
pad_token_id=tokenizer.eos_token_id,
|
| 45 |
)
|
|
|
|
| 46 |
response = tokenizer.decode(output[0], skip_special_tokens=True).split("AI:")[-1].strip()
|
| 47 |
history.append((user_input, response))
|
| 48 |
+
return history, history
|
| 49 |
|
| 50 |
+
# UI
|
| 51 |
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 52 |
+
gr.Markdown("# 🤖 Kimi-K2 AI Assistant\nChat naturally with Kimi!")
|
| 53 |
|
| 54 |
+
chatbot = gr.Chatbot(height=400)
|
| 55 |
+
with gr.Row():
|
| 56 |
+
user_input = gr.Textbox(placeholder="Type your message...", scale=10)
|
| 57 |
+
submit_btn = gr.Button("Send", scale=2)
|
| 58 |
|
|
|
|
|
|
|
| 59 |
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
submit_btn.click(chat, [user_input, state], [chatbot, state])
|
| 62 |
+
user_input.submit(chat, [user_input, state], [chatbot, state])
|
|
|
|
|
|
|
| 63 |
|
| 64 |
demo.launch()
|