abdussamikhan commited on
Commit
c161e8a
·
verified ·
1 Parent(s): f9f5884

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -3,23 +3,22 @@ from openai import OpenAI
3
  import os
4
 
5
  # Initialize Client
6
- # IMPORTANT: MOONSHOT_API_KEY must be in Hugging Face Settings > Secrets
7
  client = OpenAI(
8
  api_key=os.getenv("MOONSHOT_API_KEY"),
9
  base_url="https://api.moonshot.cn/v1",
10
  )
11
 
12
  def predict(message, history):
13
- # 1. Start with the System Prompt
14
  messages = [{"role": "system", "content": "You are Kimi, a helpful AI assistant."}]
15
 
16
- # 2. Add History (Fixed for Gradio 6.0 format)
17
- # History is now a list of dicts: [{"role": "user", "content": "..."}, ...]
18
  for entry in history:
19
- # We ensure we only pass role and content to the Kimi API
20
  messages.append({"role": entry["role"], "content": entry["content"]})
21
 
22
- # 3. Add current message
23
  messages.append({"role": "user", "content": message})
24
 
25
  try:
@@ -31,23 +30,26 @@ def predict(message, history):
31
 
32
  partial_message = ""
33
  for chunk in response:
34
- if chunk.choices[0].delta.content:
35
  partial_message += chunk.choices[0].delta.content
36
  yield partial_message
37
  except Exception as e:
38
  yield f"❌ API Error: {str(e)}"
39
 
40
- # Define the UI Structure
41
  with gr.Blocks() as demo:
42
  gr.Markdown("# 🤖 Kimi AI Chatbot")
 
 
 
43
  gr.ChatInterface(
44
  predict,
45
- type="messages", # Explicitly set format for Gradio 6.0
46
- examples=["Tell me a story about a brave cat.", "How do I make pasta?"],
47
  )
48
 
49
  if __name__ == "__main__":
50
- # Gradio 6.0 requires theme and css in launch()
51
  demo.launch(
52
  theme=gr.themes.Soft(),
53
  css=".gradio-container {background-color: #f7f9fc}"
 
3
  import os
4
 
5
  # Initialize Client
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.cn/v1",
10
  )
11
 
12
  def predict(message, history):
13
+ # Initial system message
14
  messages = [{"role": "system", "content": "You are Kimi, a helpful AI assistant."}]
15
 
16
+ # In Gradio 6.0, history is a list of dictionaries.
17
+ # We loop through them and pass them directly to the API.
18
  for entry in history:
 
19
  messages.append({"role": entry["role"], "content": entry["content"]})
20
 
21
+ # Add the newest user message
22
  messages.append({"role": "user", "content": message})
23
 
24
  try:
 
30
 
31
  partial_message = ""
32
  for chunk in response:
33
+ if chunk.choices[0].delta.content is not None:
34
  partial_message += chunk.choices[0].delta.content
35
  yield partial_message
36
  except Exception as e:
37
  yield f"❌ API Error: {str(e)}"
38
 
39
+ # Define UI Structure
40
  with gr.Blocks() as demo:
41
  gr.Markdown("# 🤖 Kimi AI Chatbot")
42
+ gr.Markdown("Directly using Moonshot AI's Kimi model.")
43
+
44
+ # 'type' argument is removed because 'messages' is now the default
45
  gr.ChatInterface(
46
  predict,
47
+ examples=["Tell me a joke.", "Explain how an airplane flies."],
48
+ cache_examples=False,
49
  )
50
 
51
  if __name__ == "__main__":
52
+ # Launch with theme and CSS (New standard for Gradio 6.0)
53
  demo.launch(
54
  theme=gr.themes.Soft(),
55
  css=".gradio-container {background-color: #f7f9fc}"