Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
| 1 |
# β
Imports
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# β
OpenRouter API client setup
|
| 6 |
client = OpenAI(
|
| 7 |
base_url="https://openrouter.ai/api/v1",
|
| 8 |
-
api_key=
|
| 9 |
)
|
| 10 |
|
| 11 |
# β
Chat function using Kimi-K2
|
| 12 |
-
def ask_kimi(prompt, history=
|
|
|
|
|
|
|
|
|
|
| 13 |
messages = [{"role": "system", "content": "Wewe ni msaidizi mzuri wa kujifunza programu kwa Kiswahili."}]
|
| 14 |
for user, bot in history:
|
| 15 |
messages.append({"role": "user", "content": user})
|
|
@@ -29,11 +38,30 @@ def ask_kimi(prompt, history=[]):
|
|
| 29 |
history.append((prompt, reply))
|
| 30 |
return reply, history
|
| 31 |
|
| 32 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
with gr.Blocks() as demo:
|
|
|
|
| 34 |
gr.Markdown("## π€ Kimi K2 Chatbot kwa Kiswahili (Powered by OpenRouter)")
|
| 35 |
|
| 36 |
-
chatbot = gr.Chatbot()
|
| 37 |
state = gr.State([])
|
| 38 |
|
| 39 |
with gr.Row():
|
|
@@ -46,16 +74,16 @@ with gr.Blocks() as demo:
|
|
| 46 |
|
| 47 |
clear = gr.Button("π Anza upya")
|
| 48 |
|
| 49 |
-
# Chat submission logic
|
| 50 |
def user_submit(user_message, history):
|
| 51 |
response, updated_history = ask_kimi(user_message, history)
|
| 52 |
-
return updated_history, updated_history
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
msg.submit(user_submit, [msg, state], [chatbot, state])
|
| 56 |
-
submit.click(user_submit, [msg, state], [chatbot, state])
|
| 57 |
|
| 58 |
-
|
| 59 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
| 1 |
# β
Imports
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# β
Load API key from Hugging Face Secrets (environment variables)
|
| 7 |
+
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 8 |
+
if not api_key:
|
| 9 |
+
raise ValueError("β οΈ OPENROUTER_API_KEY not found. Add it in HF Spaces > Settings > Secrets.")
|
| 10 |
|
| 11 |
# β
OpenRouter API client setup
|
| 12 |
client = OpenAI(
|
| 13 |
base_url="https://openrouter.ai/api/v1",
|
| 14 |
+
api_key=api_key,
|
| 15 |
)
|
| 16 |
|
| 17 |
# β
Chat function using Kimi-K2
|
| 18 |
+
def ask_kimi(prompt, history=None):
|
| 19 |
+
if history is None:
|
| 20 |
+
history = []
|
| 21 |
+
|
| 22 |
messages = [{"role": "system", "content": "Wewe ni msaidizi mzuri wa kujifunza programu kwa Kiswahili."}]
|
| 23 |
for user, bot in history:
|
| 24 |
messages.append({"role": "user", "content": user})
|
|
|
|
| 38 |
history.append((prompt, reply))
|
| 39 |
return reply, history
|
| 40 |
|
| 41 |
+
# β
Optional inline CSS
|
| 42 |
+
custom_css = """
|
| 43 |
+
<style>
|
| 44 |
+
body {
|
| 45 |
+
background-color: #f9f9f9;
|
| 46 |
+
}
|
| 47 |
+
.gradio-container {
|
| 48 |
+
max-width: 700px;
|
| 49 |
+
margin: auto;
|
| 50 |
+
}
|
| 51 |
+
.gradio-chatbot {
|
| 52 |
+
background-color: #eef2f7;
|
| 53 |
+
border-radius: 10px;
|
| 54 |
+
padding: 10px;
|
| 55 |
+
}
|
| 56 |
+
</style>
|
| 57 |
+
"""
|
| 58 |
+
|
| 59 |
+
# β
Gradio UI with proper layout and "messages" type
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
+
gr.HTML(custom_css) # Inject custom styles
|
| 62 |
gr.Markdown("## π€ Kimi K2 Chatbot kwa Kiswahili (Powered by OpenRouter)")
|
| 63 |
|
| 64 |
+
chatbot = gr.Chatbot(label="Kimi K2", type="messages")
|
| 65 |
state = gr.State([])
|
| 66 |
|
| 67 |
with gr.Row():
|
|
|
|
| 74 |
|
| 75 |
clear = gr.Button("π Anza upya")
|
| 76 |
|
| 77 |
+
# Chat submission logic - return updated chat history and clear input box
|
| 78 |
def user_submit(user_message, history):
|
| 79 |
response, updated_history = ask_kimi(user_message, history)
|
| 80 |
+
return updated_history, updated_history, ""
|
| 81 |
|
| 82 |
+
# Bind submit with Enter or button click
|
| 83 |
+
msg.submit(user_submit, [msg, state], [chatbot, state, msg])
|
| 84 |
+
submit.click(user_submit, [msg, state], [chatbot, state, msg])
|
| 85 |
|
| 86 |
+
clear.click(lambda: ([], [], ""), None, [chatbot, state, msg])
|
|
|
|
| 87 |
|
| 88 |
+
# β
Launch the app (do NOT use css= arg here)
|
| 89 |
+
demo.launch()
|