Update app.py
Browse files
app.py
CHANGED
@@ -31,10 +31,15 @@ GREETING_MESSAGES = [
|
|
31 |
|
32 |
def user(user_message, history):
|
33 |
"""Add user message to chat history."""
|
|
|
|
|
34 |
return "", history + [{"role": "user", "content": user_message}]
|
35 |
|
36 |
def bot(history):
|
37 |
"""Generate and stream the bot's response."""
|
|
|
|
|
|
|
38 |
# Prepare the messages for the model
|
39 |
messages = [
|
40 |
{
|
@@ -67,6 +72,10 @@ def bot(history):
|
|
67 |
history[-1]["content"] += chunk["choices"][0]["delta"]["content"]
|
68 |
yield history
|
69 |
|
|
|
|
|
|
|
|
|
70 |
# Custom CSS for a space theme
|
71 |
custom_css = """
|
72 |
#component-0 {
|
@@ -148,12 +157,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="indigo", neutra
|
|
148 |
clear.click(lambda: None, None, chatbot, queue=False)
|
149 |
|
150 |
# Initial greeting
|
151 |
-
demo.load(
|
152 |
-
lambda: [[{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]],
|
153 |
-
None,
|
154 |
-
chatbot,
|
155 |
-
queue=False
|
156 |
-
)
|
157 |
|
158 |
# Launch the app
|
159 |
if __name__ == "__main__":
|
|
|
31 |
|
32 |
def user(user_message, history):
|
33 |
"""Add user message to chat history."""
|
34 |
+
if history is None:
|
35 |
+
history = []
|
36 |
return "", history + [{"role": "user", "content": user_message}]
|
37 |
|
38 |
def bot(history):
|
39 |
"""Generate and stream the bot's response."""
|
40 |
+
if not history:
|
41 |
+
history = []
|
42 |
+
|
43 |
# Prepare the messages for the model
|
44 |
messages = [
|
45 |
{
|
|
|
72 |
history[-1]["content"] += chunk["choices"][0]["delta"]["content"]
|
73 |
yield history
|
74 |
|
75 |
+
def initial_greeting():
|
76 |
+
"""Return properly formatted initial greeting."""
|
77 |
+
return [{"role": "assistant", "content": random.choice(GREETING_MESSAGES)}]
|
78 |
+
|
79 |
# Custom CSS for a space theme
|
80 |
custom_css = """
|
81 |
#component-0 {
|
|
|
157 |
clear.click(lambda: None, None, chatbot, queue=False)
|
158 |
|
159 |
# Initial greeting
|
160 |
+
demo.load(initial_greeting, None, chatbot, queue=False)
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
# Launch the app
|
163 |
if __name__ == "__main__":
|