abdussamikhan commited on
Commit
6446e2f
·
verified ·
1 Parent(s): 0b18ba1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -6,27 +6,29 @@ import os
6
  # IMPORTANT: Ensure MOONSHOT_API_KEY is in Hugging Face Settings > Secrets
7
  client = OpenAI(
8
  api_key=os.getenv("MOONSHOT_API_KEY"),
9
- base_url="https://api.moonshot.ai/v1", # Official endpoint
10
  )
11
 
12
- def predict(message, history):
13
- # 1. Initialize the messages list (Fixes NameError)
14
- # Start with the System Prompt to define the bot's persona
 
 
15
  messages = [{"role": "system", "content": "You are Kimi, a helpful AI assistant."}]
16
 
17
- # 2. Format History (Gradio 6.x Standard)
18
- # History is now a list of dictionaries by default: [{"role": "user", "content": "..."}, ...]
19
  for entry in history:
20
- # We ensure we only pass role and content to the Kimi API
21
  messages.append({"role": entry["role"], "content": entry["content"]})
22
 
23
- # 3. Add the newest user message
24
  messages.append({"role": "user", "content": message})
25
 
26
  try:
27
- # 4. Call the API
 
28
  response = client.chat.completions.create(
29
- model='moonshot-v1-8k',
30
  messages=messages,
31
  stream=True
32
  )
@@ -43,18 +45,23 @@ def predict(message, history):
43
  # Define UI Structure
44
  with gr.Blocks() as demo:
45
  gr.Markdown("# 🤖 Kimi AI Chatbot")
46
- gr.Markdown("Directly using Moonshot AI's Kimi model.")
47
 
48
- # Note: 'type' argument is removed in 6.0 as 'messages' is now the only default
49
  gr.ChatInterface(
50
  predict,
51
- examples=["Tell me a joke.", "Explain how an airplane flies."],
 
 
 
 
 
 
 
 
52
  cache_examples=False,
53
  )
54
 
55
  if __name__ == "__main__":
56
- # Theme and CSS move to launch() in Gradio 6.0+
57
- # (Fixes the UserWarning in your logs)
58
  demo.launch(
59
  theme=gr.themes.Soft(),
60
  css=".gradio-container {background-color: #f7f9fc}"
 
6
  # IMPORTANT: Ensure MOONSHOT_API_KEY is in Hugging Face Settings > Secrets
7
  client = OpenAI(
8
  api_key=os.getenv("MOONSHOT_API_KEY"),
9
+ base_url="https://api.moonshot.ai/v1",
10
  )
11
 
12
+ # Define the available Kimi model versions
13
+ KIMI_MODELS = ["moonshot-v1-8k", "moonshot-v1-32k", "moonshot-v1-128k"]
14
+
15
+ def predict(message, history, model_name):
16
+ # --- 1. INITIALIZE THE MESSAGES LIST ---
17
  messages = [{"role": "system", "content": "You are Kimi, a helpful AI assistant."}]
18
 
19
+ # --- 2. FORMAT HISTORY ---
20
+ # Gradio 6.x history is a list of dictionaries
21
  for entry in history:
 
22
  messages.append({"role": entry["role"], "content": entry["content"]})
23
 
24
+ # Add the current user message
25
  messages.append({"role": "user", "content": message})
26
 
27
  try:
28
+ # --- 3. CALL THE API ---
29
+ # The model is now determined by the user's selection in the UI
30
  response = client.chat.completions.create(
31
+ model=model_name,
32
  messages=messages,
33
  stream=True
34
  )
 
45
  # Define UI Structure
46
  with gr.Blocks() as demo:
47
  gr.Markdown("# 🤖 Kimi AI Chatbot")
48
+ gr.Markdown("Directly using Moonshot AI's Kimi model with selectable versions.")
49
 
 
50
  gr.ChatInterface(
51
  predict,
52
+ # Adding a dropdown to the 'Additional Inputs' section
53
+ additional_inputs=[
54
+ gr.Dropdown(
55
+ choices=KIMI_MODELS,
56
+ value="moonshot-v1-8k",
57
+ label="Select KIMI Model Version"
58
+ )
59
+ ],
60
+ examples=["Explain how an airplane flies.", "Tell me a fun fact about space."],
61
  cache_examples=False,
62
  )
63
 
64
  if __name__ == "__main__":
 
 
65
  demo.launch(
66
  theme=gr.themes.Soft(),
67
  css=".gradio-container {background-color: #f7f9fc}"