AiCoderv2 commited on
Commit
ff1152e
Β·
verified Β·
1 Parent(s): e276a2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load the model and tokenizer once to avoid reloading
6
+ model_name = "moonshotai/Kimi-K2-Instruct"
7
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.float16)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ def chat_with_kimi_k2(input_text, chat_history=None):
11
+ if chat_history is None:
12
+ chat_history = []
13
+ # Append user input to chat history
14
+ chat_history.append(f"User: {input_text}")
15
+
16
+ # Prepare prompt
17
+ prompt = "\n".join(chat_history) + "\nAI:"
18
+ inputs = tokenizer(prompt, return_tensors="pt")
19
+ outputs = model.generate(**inputs, max_new_tokens=150, do_sample=True, temperature=0.7)
20
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
21
+
22
+ # Extract AI response
23
+ response_text = response.split("AI:")[-1].strip()
24
+ chat_history.append(f"AI: {response_text}")
25
+
26
+ return response_text, chat_history
27
+
28
+ # Main menu interface
29
+ def main_menu():
30
+ return gr.Interface(
31
+ fn=show_model_selection,
32
+ title="AI Models Hub",
33
+ description="Select an AI model to chat with:",
34
+ inputs=[],
35
+ outputs=gr.Button("Kimi K2"),
36
+ live=True
37
+ )
38
+
39
+ # Show model options
40
+ def show_model_selection():
41
+ with gr.Row():
42
+ kimi_button = gr.Button("Chat with Kimi K2")
43
+ return kimi_button
44
+
45
+ # Chat interface for Kimi K2
46
+ def kimi_chat():
47
+ chat_history = []
48
+
49
+ def respond(user_input, history):
50
+ response, chat_history_updated = chat_with_kimi_k2(user_input, chat_history=history)
51
+ return response, chat_history_updated
52
+
53
+ with gr.Blocks():
54
+ gr.Markdown("### Chat with Kimi K2")
55
+ chatbox = gr.Textbox(placeholder="Type your message...", label="You")
56
+ response_box = gr.Textbox(label="Kimi K2")
57
+ chat_history_state = gr.State([])
58
+ back_button = gr.Button("Back to Menu")
59
+
60
+ def back():
61
+ return "Back", None
62
+
63
+ back_button.click(fn=main_menu, inputs=None, outputs=None)
64
+
65
+ user_input = chatbox
66
+ user_input.submit(respond, [user_input, chat_history_state], [response_box, chat_history_state])
67
+ back_button.click(lambda: None, None, None)
68
+
69
+ return gr.Column([chatbox, response_box, back_button])
70
+
71
+ # Assemble the main app
72
+ def app():
73
+ with gr.Blocks() as demo:
74
+ menu = gr.Button("Go to Main Menu")
75
+ kimichat = gr.Button("Chat with Kimi K2")
76
+
77
+ def show_kimi():
78
+ return kimi_chat()
79
+
80
+ menu.click(show_kimi)
81
+ kimichat.click(show_kimi)
82
+
83
+ return demo
84
+
85
+ # Run the app
86
+ demo = app()
87
+ demo.launch()