jesstest / app.py
runebloodstone's picture
Update app.py
41f9a38 verified
raw
history blame
2.03 kB
import gradio as gr
import requests
def send_message_with_secret(message, history, api_secret, bot_id):
if not api_secret.strip():
return "Please enter your API secret in the field above."
API_URL = "https://api.kindroid.ai/v1/send-message"
headers = {
"Authorization": f"Bearer {api_secret}",
"Content-Type": "application/json"
}
payload = {
"ai_id": bot_id,
"message": message
}
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status()
return response.content.decode()
except requests.exceptions.RequestException as e:
return f"An error occurred: {e}"
with gr.Blocks() as app:
gr.Markdown("## Chat with your Kin")
api_secret = gr.Textbox(
label="API Secret",
placeholder="Enter your API secret...",
type="password",
show_label=True
)
bot_id = gr.Textbox(
label="Bot ID",
placeholder="Enter your BotID...",
show_label=True
)
chatbot = gr.Chatbot(
value=[],
type='messages',
height=400
)
msg = gr.Textbox(
label="Message",
placeholder="Type your message here...",
show_label=False
)
def respond(message, chat_history, api_secret):
if not message:
return chat_history, ""
bot_response = send_message_with_secret(message, chat_history, api_secret, bot_id)
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": bot_response})
return chat_history, ""
msg.submit(
respond,
inputs=[msg, chatbot, api_secret, bot_id],
outputs=[chatbot, msg]
)
send_btn = gr.Button("Send")
send_btn.click(
respond,
inputs=[msg, chatbot, api_secret],
outputs=[chatbot, msg]
)
if __name__ == "__main__":
app.launch()