Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,22 @@ from openai import OpenAI
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Initialize Client
|
| 6 |
-
# IMPORTANT: MOONSHOT_API_KEY
|
| 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 |
-
#
|
| 14 |
messages = [{"role": "system", "content": "You are Kimi, a helpful AI assistant."}]
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
#
|
| 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 |
-
#
|
| 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
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# 🤖 Kimi AI Chatbot")
|
|
|
|
|
|
|
|
|
|
| 43 |
gr.ChatInterface(
|
| 44 |
predict,
|
| 45 |
-
|
| 46 |
-
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
#
|
| 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}"
|