runebloodstone commited on
Commit
622720c
·
verified ·
1 Parent(s): 8b7cf3c

Update app.py

Browse files

Updated to a chat interface.

Files changed (1) hide show
  1. app.py +70 -14
app.py CHANGED
@@ -1,12 +1,14 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
- def send_message(message):
 
 
 
5
  API_URL = "https://api.kindroid.ai/v1/send-message"
6
- AUTH_TOKEN = "kn_df28dc59-57ec-442d-8983-b3f7ccdcb4b5"
7
-
8
  headers = {
9
- "Authorization": f"Bearer {AUTH_TOKEN}",
10
  "Content-Type": "application/json"
11
  }
12
 
@@ -24,15 +26,69 @@ def send_message(message):
24
  except requests.exceptions.RequestException as e:
25
  return f"An error occurred: {e}"
26
 
27
- # Create the Gradio interface
28
- iface = gr.Interface(
29
- fn=send_message,
30
- inputs=gr.Textbox(lines=3, placeholder="Enter your message here..."),
31
- outputs=gr.Textbox(),
32
- title="AI Message Sender",
33
- description="Send messages to the AI and see responses."
34
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Launch the app
37
  if __name__ == "__main__":
38
- iface.launch()
 
1
  import gradio as gr
2
  import requests
3
+ from functools import partial
4
 
5
+ def send_message_with_secret(message, history, api_secret):
6
+ if not api_secret.strip():
7
+ return "Please enter your API secret in the field above."
8
+
9
  API_URL = "https://api.kindroid.ai/v1/send-message"
 
 
10
  headers = {
11
+ "Authorization": f"Bearer {api_secret}",
12
  "Content-Type": "application/json"
13
  }
14
 
 
26
  except requests.exceptions.RequestException as e:
27
  return f"An error occurred: {e}"
28
 
29
+ def create_chat_interface(api_secret):
30
+ # Partially apply the api_secret to the send_message function
31
+ send_fn = partial(send_message_with_secret, api_secret=api_secret)
32
+
33
+ return gr.ChatInterface(
34
+ fn=send_fn,
35
+ title="Chat with Your Kin",
36
+ description="Send messages and receive responses in a chat-like interface",
37
+ examples=[
38
+ ["Hello! How are you today?"],
39
+ ["Tell me about yourself"],
40
+ ],
41
+ theme=gr.themes.Soft(
42
+ primary_hue="blue",
43
+ secondary_hue="gray",
44
+ ),
45
+ css="""
46
+ .message.user {
47
+ background-color: #2563eb !important;
48
+ color: white !important;
49
+ border-radius: 12px 12px 0 12px !important;
50
+ margin-left: auto !important;
51
+ margin-right: 1rem !important;
52
+ }
53
+ .message.bot {
54
+ background-color: #f3f4f6 !important;
55
+ border-radius: 12px 12px 12px 0 !important;
56
+ margin-right: auto !important;
57
+ margin-left: 1rem !important;
58
+ }
59
+ .message.user, .message.bot {
60
+ padding: 1rem !important;
61
+ margin-bottom: 1rem !important;
62
+ max-width: 80% !important;
63
+ display: inline-block !important;
64
+ }
65
+ .message-wrap {
66
+ display: flex !important;
67
+ flex-direction: column !important;
68
+ }
69
+ """
70
+ )
71
+
72
+ with gr.Blocks() as app:
73
+ gr.Markdown("## AI Chat Interface")
74
+
75
+ api_secret = gr.Textbox(
76
+ label="API Secret",
77
+ placeholder="Enter your API secret...",
78
+ type="password",
79
+ show_label=True
80
+ )
81
+
82
+ chat_bot = gr.Chatbot(height=500)
83
+
84
+ def update_chat(api_key):
85
+ return create_chat_interface(api_key)
86
+
87
+ api_secret.change(
88
+ fn=update_chat,
89
+ inputs=[api_secret],
90
+ outputs=[chat_bot]
91
+ )
92
 
 
93
  if __name__ == "__main__":
94
+ app.launch()