AryanRathod3097 commited on
Commit
e1d1986
·
verified ·
1 Parent(s): 5c9e4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -47
app.py CHANGED
@@ -1,46 +1,36 @@
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
3
  import torch
4
 
5
- model = None
6
- tokenizer = None
7
- streamer = None
8
 
9
- # This function is run after token is submitted
10
- def load_model(token):
11
- global model, tokenizer, streamer
 
 
 
12
 
13
- try:
14
- tokenizer = AutoTokenizer.from_pretrained(
15
- "moonshotai/Kimi-K2-Instruct",
16
- use_auth_token=token,
17
- trust_remote_code=True
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
- streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
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
- # Main chat function
40
- def chat(user_input, history, token):
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
- # Build Gradio UI
62
  with gr.Blocks(css="footer {visibility: hidden}") as demo:
63
- gr.Markdown("## 🤖 Kimi-K2 AI Chat\nEnter your Hugging Face token to start chatting.")
64
 
65
- token_box = gr.Textbox(label="🔐 Hugging Face Token", type="password", placeholder="Paste your Hugging Face Access Token here")
66
- load_button = gr.Button("🔓 Load Model")
 
 
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
- # Connect logic
78
- load_button.click(load_model, inputs=token_box, outputs=[token_box, chat_row, token_status, saved_token])
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()