Update app.py
Browse files
app.py
CHANGED
@@ -33,13 +33,20 @@ def get_ai_response(message, history):
|
|
33 |
|
34 |
def chat_interface(message, history, stored_history):
|
35 |
"""Handle chat interactions and update history."""
|
36 |
-
if history
|
37 |
history = []
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
44 |
if stored_history is None:
|
45 |
stored_history = []
|
@@ -48,7 +55,7 @@ def chat_interface(message, history, stored_history):
|
|
48 |
"user": message,
|
49 |
"ai": ai_response
|
50 |
})
|
51 |
-
return
|
52 |
|
53 |
def update_history_display(stored_history):
|
54 |
"""Generate HTML for history display and save to local storage."""
|
@@ -69,9 +76,10 @@ def update_history_display(stored_history):
|
|
69 |
html += f"<script>localStorage.setItem('chat_history', JSON.stringify({json.dumps(stored_history)}))</script>"
|
70 |
return html
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
75 |
|
76 |
# Modern CSS for a clean UI
|
77 |
custom_css = """
|
@@ -96,7 +104,7 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3 Demo") as demo:
|
|
96 |
|
97 |
# Main chat area
|
98 |
with gr.Column(scale=3):
|
99 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
100 |
with gr.Row():
|
101 |
message = gr.Textbox(placeholder="Type your message...", show_label=False, container=False)
|
102 |
submit_btn = gr.Button("Send", size="sm")
|
@@ -106,11 +114,23 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3 Demo") as demo:
|
|
106 |
chat_state = gr.State([]) # Current chat history
|
107 |
history_state = gr.State([]) # Stored history across sessions
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
# Event handlers
|
110 |
submit_btn.click(
|
111 |
chat_interface,
|
112 |
[message, chat_state, history_state],
|
113 |
-
[
|
|
|
|
|
|
|
|
|
114 |
).then(
|
115 |
update_history_display,
|
116 |
history_state,
|
@@ -124,34 +144,29 @@ with gr.Blocks(css=custom_css, title="Reka Flash 3 Demo") as demo:
|
|
124 |
clear_chat_btn.click(
|
125 |
lambda: ([], None),
|
126 |
None,
|
127 |
-
[
|
128 |
)
|
129 |
|
130 |
clear_history_btn.click(
|
131 |
-
lambda:
|
132 |
None,
|
133 |
-
|
134 |
).then(
|
135 |
update_history_display,
|
136 |
history_state,
|
137 |
history_display
|
138 |
)
|
139 |
|
140 |
-
# Load initial history
|
141 |
-
history_from_js = gr.JSON(visible=False)
|
142 |
-
|
143 |
demo.load(
|
144 |
-
|
145 |
inputs=None,
|
146 |
-
outputs=
|
147 |
-
|
148 |
-
lambda x: x,
|
149 |
-
inputs=history_from_js,
|
150 |
-
outputs=history_state
|
151 |
).then(
|
152 |
update_history_display,
|
153 |
-
|
154 |
-
|
155 |
)
|
156 |
|
157 |
demo.launch()
|
|
|
33 |
|
34 |
def chat_interface(message, history, stored_history):
|
35 |
"""Handle chat interactions and update history."""
|
36 |
+
if not history:
|
37 |
history = []
|
38 |
+
|
39 |
+
# Convert history to the format expected by the API
|
40 |
+
api_history = []
|
41 |
+
for user_msg, ai_msg in history:
|
42 |
+
api_history.append({"role": "user", "content": user_msg})
|
43 |
+
api_history.append({"role": "assistant", "content": ai_msg})
|
44 |
+
|
45 |
+
ai_response = get_ai_response(message, api_history)
|
46 |
+
|
47 |
+
# Update history in the format expected by Gradio chatbot
|
48 |
+
history.append((message, ai_response))
|
49 |
+
|
50 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
51 |
if stored_history is None:
|
52 |
stored_history = []
|
|
|
55 |
"user": message,
|
56 |
"ai": ai_response
|
57 |
})
|
58 |
+
return history, stored_history
|
59 |
|
60 |
def update_history_display(stored_history):
|
61 |
"""Generate HTML for history display and save to local storage."""
|
|
|
76 |
html += f"<script>localStorage.setItem('chat_history', JSON.stringify({json.dumps(stored_history)}))</script>"
|
77 |
return html
|
78 |
|
79 |
+
def load_history_from_storage():
|
80 |
+
"""Function to load history from JavaScript"""
|
81 |
+
# This is a placeholder that will be replaced by the JavaScript function
|
82 |
+
return []
|
83 |
|
84 |
# Modern CSS for a clean UI
|
85 |
custom_css = """
|
|
|
104 |
|
105 |
# Main chat area
|
106 |
with gr.Column(scale=3):
|
107 |
+
chatbot = gr.Chatbot(elem_id="chatbot") # Removed type="messages" which was causing errors
|
108 |
with gr.Row():
|
109 |
message = gr.Textbox(placeholder="Type your message...", show_label=False, container=False)
|
110 |
submit_btn = gr.Button("Send", size="sm")
|
|
|
114 |
chat_state = gr.State([]) # Current chat history
|
115 |
history_state = gr.State([]) # Stored history across sessions
|
116 |
|
117 |
+
# JavaScript for loading history from local storage
|
118 |
+
load_history_js = """
|
119 |
+
function() {
|
120 |
+
const history = localStorage.getItem('chat_history');
|
121 |
+
return history ? JSON.parse(history) : [];
|
122 |
+
}
|
123 |
+
"""
|
124 |
+
|
125 |
# Event handlers
|
126 |
submit_btn.click(
|
127 |
chat_interface,
|
128 |
[message, chat_state, history_state],
|
129 |
+
[chat_state, history_state]
|
130 |
+
).then(
|
131 |
+
lambda history: history,
|
132 |
+
chat_state,
|
133 |
+
chatbot
|
134 |
).then(
|
135 |
update_history_display,
|
136 |
history_state,
|
|
|
144 |
clear_chat_btn.click(
|
145 |
lambda: ([], None),
|
146 |
None,
|
147 |
+
[chat_state, chatbot]
|
148 |
)
|
149 |
|
150 |
clear_history_btn.click(
|
151 |
+
lambda: [],
|
152 |
None,
|
153 |
+
history_state
|
154 |
).then(
|
155 |
update_history_display,
|
156 |
history_state,
|
157 |
history_display
|
158 |
)
|
159 |
|
160 |
+
# Load initial history from local storage using JavaScript
|
|
|
|
|
161 |
demo.load(
|
162 |
+
fn=load_history_from_storage,
|
163 |
inputs=None,
|
164 |
+
outputs=history_state,
|
165 |
+
js=load_history_js
|
|
|
|
|
|
|
166 |
).then(
|
167 |
update_history_display,
|
168 |
+
history_state,
|
169 |
+
history_display
|
170 |
)
|
171 |
|
172 |
demo.launch()
|