Update app.py
Browse files
app.py
CHANGED
@@ -4,30 +4,26 @@ import json
|
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
|
7 |
-
# API configuration
|
8 |
API_ENDPOINT = os.getenv("API_ENDPOINT", "none")
|
9 |
API_TOKEN = os.getenv("API_TOKEN")
|
10 |
|
11 |
def get_ai_response(message, history):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
messages.append({"role": "user", "content": message})
|
17 |
-
|
18 |
payload = {
|
19 |
-
"model": "RekaAI/reka-flash-3",
|
20 |
"messages": messages,
|
21 |
"stream": False,
|
22 |
"max_tokens": 1024,
|
23 |
"temperature": 0.7
|
24 |
}
|
25 |
-
|
26 |
headers = {
|
27 |
"Authorization": f"Bearer {API_TOKEN}",
|
28 |
"Content-Type": "application/json"
|
29 |
}
|
30 |
-
|
31 |
try:
|
32 |
response = requests.post(API_ENDPOINT, headers=headers, json=payload)
|
33 |
response.raise_for_status()
|
@@ -36,68 +32,56 @@ def get_ai_response(message, history):
|
|
36 |
return f"Error: {str(e)}"
|
37 |
|
38 |
def chat_interface(message, history, stored_history):
|
|
|
39 |
if history is None:
|
40 |
history = []
|
41 |
-
|
42 |
ai_response = get_ai_response(message, history)
|
43 |
-
history
|
44 |
-
|
|
|
|
|
45 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
46 |
if stored_history is None:
|
47 |
stored_history = []
|
48 |
-
stored_history.insert(0, {
|
49 |
"timestamp": timestamp,
|
50 |
"user": message,
|
51 |
"ai": ai_response
|
52 |
})
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
# Modern
|
57 |
custom_css = """
|
58 |
-
body {
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
}
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
border: 1px solid #404040;
|
67 |
-
border-radius: 8px;
|
68 |
-
}
|
69 |
-
#sidebar {
|
70 |
-
background-color: #242424;
|
71 |
-
padding: 10px;
|
72 |
-
border-right: 1px solid #404040;
|
73 |
-
height: 80vh;
|
74 |
-
overflow-y: auto;
|
75 |
-
}
|
76 |
-
#history_list {
|
77 |
-
list-style: none;
|
78 |
-
padding: 0;
|
79 |
-
}
|
80 |
-
.history-item {
|
81 |
-
background-color: #333333;
|
82 |
-
margin: 5px 0;
|
83 |
-
padding: 10px;
|
84 |
-
border-radius: 5px;
|
85 |
-
cursor: pointer;
|
86 |
-
}
|
87 |
-
.history-item:hover {
|
88 |
-
background-color: #404040;
|
89 |
-
}
|
90 |
-
input, button {
|
91 |
-
background-color: #333333;
|
92 |
-
color: #ffffff;
|
93 |
-
border: 1px solid #404040;
|
94 |
-
border-radius: 5px;
|
95 |
-
}
|
96 |
-
button:hover {
|
97 |
-
background-color: #404040;
|
98 |
-
}
|
99 |
"""
|
100 |
|
|
|
101 |
with gr.Blocks(css=custom_css, title="Modern AI Chatbot") as demo:
|
102 |
with gr.Row():
|
103 |
# Sidebar for history
|
@@ -108,35 +92,15 @@ with gr.Blocks(css=custom_css, title="Modern AI Chatbot") as demo:
|
|
108 |
|
109 |
# Main chat area
|
110 |
with gr.Column(scale=3):
|
111 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
112 |
with gr.Row():
|
113 |
-
message = gr.Textbox(
|
114 |
-
placeholder="Type your message...",
|
115 |
-
show_label=False,
|
116 |
-
container=False,
|
117 |
-
elem_classes="input-box"
|
118 |
-
)
|
119 |
submit_btn = gr.Button("Send", size="sm")
|
120 |
clear_chat_btn = gr.Button("Clear Chat")
|
121 |
|
122 |
-
#
|
123 |
-
chat_state = gr.State([])
|
124 |
-
history_state = gr.State([])
|
125 |
-
|
126 |
-
def update_history_display(stored_history):
|
127 |
-
if not stored_history:
|
128 |
-
return "<p>No history yet</p>"
|
129 |
-
html = "<ul id='history_list'>"
|
130 |
-
for item in stored_history[:10]: # Limit to last 10 conversations
|
131 |
-
html += f"""
|
132 |
-
<li class='history-item'>
|
133 |
-
<small>{item['timestamp']}</small><br>
|
134 |
-
<strong>You:</strong> {item['user'][:50]}...<br>
|
135 |
-
<strong>AI:</strong> {item['ai'][:50]}...
|
136 |
-
</li>
|
137 |
-
"""
|
138 |
-
html += "</ul>"
|
139 |
-
return html
|
140 |
|
141 |
# Event handlers
|
142 |
submit_btn.click(
|
@@ -148,7 +112,7 @@ with gr.Blocks(css=custom_css, title="Modern AI Chatbot") as demo:
|
|
148 |
history_state,
|
149 |
history_display
|
150 |
).then(
|
151 |
-
lambda: "",
|
152 |
None,
|
153 |
message
|
154 |
)
|
@@ -163,40 +127,20 @@ with gr.Blocks(css=custom_css, title="Modern AI Chatbot") as demo:
|
|
163 |
lambda: ([], None),
|
164 |
None,
|
165 |
[history_state, history_display]
|
166 |
-
)
|
167 |
-
|
168 |
-
# Initial load
|
169 |
-
demo.load(
|
170 |
-
lambda: ([], []),
|
171 |
-
None,
|
172 |
-
[chat_state, history_state]
|
173 |
).then(
|
174 |
update_history_display,
|
175 |
history_state,
|
176 |
history_display
|
177 |
)
|
178 |
|
179 |
-
#
|
180 |
-
demo.change(
|
181 |
-
None,
|
182 |
-
history_state,
|
183 |
-
None,
|
184 |
-
_js="""(stored_history) => {
|
185 |
-
localStorage.setItem('chat_history', JSON.stringify(stored_history));
|
186 |
-
}"""
|
187 |
-
).then(
|
188 |
-
update_history_display,
|
189 |
-
history_state,
|
190 |
-
history_display
|
191 |
-
)
|
192 |
-
|
193 |
-
# Load from local storage
|
194 |
demo.load(
|
195 |
-
|
196 |
None,
|
197 |
history_state,
|
198 |
_js="""() => {
|
199 |
-
|
|
|
200 |
}"""
|
201 |
).then(
|
202 |
update_history_display,
|
|
|
4 |
import os
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
# API configuration (replace with your actual endpoint and token)
|
8 |
API_ENDPOINT = os.getenv("API_ENDPOINT", "none")
|
9 |
API_TOKEN = os.getenv("API_TOKEN")
|
10 |
|
11 |
def get_ai_response(message, history):
|
12 |
+
"""Fetch AI response from the API using the modern messages format."""
|
13 |
+
messages = [
|
14 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
15 |
+
] + history + [{"role": "user", "content": message}]
|
|
|
|
|
16 |
payload = {
|
17 |
+
"model": "RekaAI/reka-flash-3",
|
18 |
"messages": messages,
|
19 |
"stream": False,
|
20 |
"max_tokens": 1024,
|
21 |
"temperature": 0.7
|
22 |
}
|
|
|
23 |
headers = {
|
24 |
"Authorization": f"Bearer {API_TOKEN}",
|
25 |
"Content-Type": "application/json"
|
26 |
}
|
|
|
27 |
try:
|
28 |
response = requests.post(API_ENDPOINT, headers=headers, json=payload)
|
29 |
response.raise_for_status()
|
|
|
32 |
return f"Error: {str(e)}"
|
33 |
|
34 |
def chat_interface(message, history, stored_history):
|
35 |
+
"""Handle chat interactions and update history."""
|
36 |
if history is None:
|
37 |
history = []
|
|
|
38 |
ai_response = get_ai_response(message, history)
|
39 |
+
new_history = history + [
|
40 |
+
{"role": "user", "content": message},
|
41 |
+
{"role": "assistant", "content": ai_response}
|
42 |
+
]
|
43 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
44 |
if stored_history is None:
|
45 |
stored_history = []
|
46 |
+
stored_history.insert(0, {
|
47 |
"timestamp": timestamp,
|
48 |
"user": message,
|
49 |
"ai": ai_response
|
50 |
})
|
51 |
+
return new_history, stored_history
|
52 |
+
|
53 |
+
def update_history_display(stored_history):
|
54 |
+
"""Generate HTML for history display and save to local storage."""
|
55 |
+
if not stored_history:
|
56 |
+
html = "<p>No history yet</p>"
|
57 |
+
else:
|
58 |
+
html = "<ul id='history_list'>"
|
59 |
+
for item in stored_history[:10]: # Limit to last 10 conversations
|
60 |
+
html += f"""
|
61 |
+
<li class='history-item'>
|
62 |
+
<small>{item['timestamp']}</small><br>
|
63 |
+
<strong>You:</strong> {item['user'][:50]}...<br>
|
64 |
+
<strong>AI:</strong> {item['ai'][:50]}...
|
65 |
+
</li>
|
66 |
+
"""
|
67 |
+
html += "</ul>"
|
68 |
+
# Embed script to save history to local storage
|
69 |
+
html += f"<script>localStorage.setItem('chat_history', JSON.stringify({json.dumps(stored_history)}))</script>"
|
70 |
+
return html
|
71 |
|
72 |
+
# Modern CSS for a clean UI
|
73 |
custom_css = """
|
74 |
+
body { background-color: #1a1a1a; color: #ffffff; font-family: 'Arial', sans-serif; }
|
75 |
+
#chatbot { height: 60vh; background-color: #2d2d2d; border: 1px solid #404040; border-radius: 8px; }
|
76 |
+
#sidebar { background-color: #242424; padding: 10px; border-right: 1px solid #404040; height: 80vh; overflow-y: auto; }
|
77 |
+
#history_list { list-style: none; padding: 0; }
|
78 |
+
.history-item { background-color: #333333; margin: 5px 0; padding: 10px; border-radius: 5px; cursor: pointer; }
|
79 |
+
.history-item:hover { background-color: #404040; }
|
80 |
+
input, button { background-color: #333333; color: #ffffff; border: 1px solid #404040; border-radius: 5px; }
|
81 |
+
button:hover { background-color: #404040; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
"""
|
83 |
|
84 |
+
# Build the Gradio app
|
85 |
with gr.Blocks(css=custom_css, title="Modern AI Chatbot") as demo:
|
86 |
with gr.Row():
|
87 |
# Sidebar for history
|
|
|
92 |
|
93 |
# Main chat area
|
94 |
with gr.Column(scale=3):
|
95 |
+
chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # Fix 1: Set type to 'messages'
|
96 |
with gr.Row():
|
97 |
+
message = gr.Textbox(placeholder="Type your message...", show_label=False, container=False)
|
|
|
|
|
|
|
|
|
|
|
98 |
submit_btn = gr.Button("Send", size="sm")
|
99 |
clear_chat_btn = gr.Button("Clear Chat")
|
100 |
|
101 |
+
# State management
|
102 |
+
chat_state = gr.State([]) # Current chat history
|
103 |
+
history_state = gr.State([]) # Stored history across sessions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
# Event handlers
|
106 |
submit_btn.click(
|
|
|
112 |
history_state,
|
113 |
history_display
|
114 |
).then(
|
115 |
+
lambda: "", # Clear the input box
|
116 |
None,
|
117 |
message
|
118 |
)
|
|
|
127 |
lambda: ([], None),
|
128 |
None,
|
129 |
[history_state, history_display]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
).then(
|
131 |
update_history_display,
|
132 |
history_state,
|
133 |
history_display
|
134 |
)
|
135 |
|
136 |
+
# Load initial history from local storage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
demo.load(
|
138 |
+
None,
|
139 |
None,
|
140 |
history_state,
|
141 |
_js="""() => {
|
142 |
+
const history = localStorage.getItem('chat_history');
|
143 |
+
return history ? JSON.parse(history) : [];
|
144 |
}"""
|
145 |
).then(
|
146 |
update_history_display,
|