Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -25,28 +25,33 @@ def respond(
|
|
25 |
|
26 |
# Build conversation history
|
27 |
for human_msg, assistant_msg in chat_history:
|
28 |
-
|
29 |
-
|
30 |
-
if assistant_msg:
|
31 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
32 |
|
33 |
messages.append({"role": "user", "content": message})
|
34 |
response = ""
|
35 |
|
36 |
try:
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
messages,
|
39 |
max_tokens=max_tokens,
|
40 |
stream=True,
|
41 |
temperature=temperature,
|
42 |
top_p=top_p,
|
43 |
):
|
44 |
-
token =
|
45 |
response += token
|
46 |
-
|
|
|
47 |
yield chat_history
|
|
|
48 |
except Exception as e:
|
49 |
-
|
|
|
50 |
yield chat_history
|
51 |
|
52 |
def create_chat_interface():
|
@@ -67,32 +72,42 @@ def create_chat_interface():
|
|
67 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
68 |
gr.Markdown("# 🤖 Advanced AI Chatbot")
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
with gr.Row():
|
71 |
-
with gr.Column(scale=
|
72 |
-
chatbot = gr.Chatbot(
|
73 |
-
height=600,
|
74 |
-
show_label=False,
|
75 |
-
container=True,
|
76 |
-
scale=2,
|
77 |
-
type="messages" # Set type to messages format
|
78 |
-
)
|
79 |
msg = gr.Textbox(
|
80 |
show_label=False,
|
81 |
placeholder="Type your message here...",
|
82 |
container=False
|
83 |
)
|
84 |
|
85 |
-
with gr.Column(scale=1):
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
)
|
97 |
max_tokens = gr.Slider(
|
98 |
minimum=50,
|
@@ -101,13 +116,7 @@ def create_chat_interface():
|
|
101 |
step=1,
|
102 |
label="Max Tokens"
|
103 |
)
|
104 |
-
|
105 |
-
minimum=0.1,
|
106 |
-
maximum=2.0,
|
107 |
-
value=0.7,
|
108 |
-
step=0.1,
|
109 |
-
label="Temperature"
|
110 |
-
)
|
111 |
top_p = gr.Slider(
|
112 |
minimum=0.1,
|
113 |
maximum=1.0,
|
@@ -115,28 +124,33 @@ def create_chat_interface():
|
|
115 |
step=0.1,
|
116 |
label="Top P"
|
117 |
)
|
118 |
-
|
119 |
-
with gr.Row():
|
120 |
clear = gr.Button("Clear Chat")
|
121 |
-
stop = gr.Button("Stop")
|
122 |
|
123 |
-
# Initialize chat history
|
124 |
-
state = gr.State([])
|
125 |
-
|
126 |
# Handle sending messages
|
127 |
msg.submit(
|
128 |
respond,
|
129 |
-
[msg,
|
130 |
-
[
|
131 |
-
api_name="chat"
|
132 |
).then(
|
133 |
-
lambda
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
)
|
137 |
|
138 |
# Clear chat history
|
139 |
-
clear.click(lambda:
|
140 |
|
141 |
# Example prompts
|
142 |
gr.Examples(
|
@@ -147,14 +161,13 @@ def create_chat_interface():
|
|
147 |
],
|
148 |
inputs=msg
|
149 |
)
|
150 |
-
|
151 |
return demo
|
152 |
|
153 |
# Create and launch the interface
|
154 |
if __name__ == "__main__":
|
155 |
demo = create_chat_interface()
|
156 |
demo.queue()
|
157 |
-
# Disable SSR and sharing for Spaces
|
158 |
demo.launch(
|
159 |
share=False, # Disable sharing on Spaces
|
160 |
-
)
|
|
|
25 |
|
26 |
# Build conversation history
|
27 |
for human_msg, assistant_msg in chat_history:
|
28 |
+
messages.append({"role": "user", "content": human_msg})
|
29 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
30 |
|
31 |
messages.append({"role": "user", "content": message})
|
32 |
response = ""
|
33 |
|
34 |
try:
|
35 |
+
# Add user message to history immediately
|
36 |
+
chat_history = chat_history + [(message, None)]
|
37 |
+
yield chat_history
|
38 |
+
|
39 |
+
for token_data in client.chat_completion(
|
40 |
messages,
|
41 |
max_tokens=max_tokens,
|
42 |
stream=True,
|
43 |
temperature=temperature,
|
44 |
top_p=top_p,
|
45 |
):
|
46 |
+
token = token_data.choices[0].delta.content
|
47 |
response += token
|
48 |
+
# Update the last assistant message
|
49 |
+
chat_history[-1] = (message, response)
|
50 |
yield chat_history
|
51 |
+
|
52 |
except Exception as e:
|
53 |
+
error_msg = f"Error: {str(e)}"
|
54 |
+
chat_history[-1] = (message, error_msg)
|
55 |
yield chat_history
|
56 |
|
57 |
def create_chat_interface():
|
|
|
72 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
73 |
gr.Markdown("# 🤖 Advanced AI Chatbot")
|
74 |
|
75 |
+
chatbot = gr.Chatbot(
|
76 |
+
height=600,
|
77 |
+
show_label=False,
|
78 |
+
container=True,
|
79 |
+
)
|
80 |
+
|
81 |
with gr.Row():
|
82 |
+
with gr.Column(scale=4):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
msg = gr.Textbox(
|
84 |
show_label=False,
|
85 |
placeholder="Type your message here...",
|
86 |
container=False
|
87 |
)
|
88 |
|
89 |
+
with gr.Column(scale=1, min_width=100):
|
90 |
+
send = gr.Button("Send")
|
91 |
+
|
92 |
+
with gr.Accordion("Settings", open=False):
|
93 |
+
system_msg = gr.Textbox(
|
94 |
+
label="System Message",
|
95 |
+
value=default_system,
|
96 |
+
lines=3
|
97 |
+
)
|
98 |
+
model = gr.Dropdown(
|
99 |
+
choices=models,
|
100 |
+
value=models[0],
|
101 |
+
label="Model"
|
102 |
+
)
|
103 |
+
with gr.Row():
|
104 |
+
with gr.Column():
|
105 |
+
temperature = gr.Slider(
|
106 |
+
minimum=0.1,
|
107 |
+
maximum=2.0,
|
108 |
+
value=0.7,
|
109 |
+
step=0.1,
|
110 |
+
label="Temperature"
|
111 |
)
|
112 |
max_tokens = gr.Slider(
|
113 |
minimum=50,
|
|
|
116 |
step=1,
|
117 |
label="Max Tokens"
|
118 |
)
|
119 |
+
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
top_p = gr.Slider(
|
121 |
minimum=0.1,
|
122 |
maximum=1.0,
|
|
|
124 |
step=0.1,
|
125 |
label="Top P"
|
126 |
)
|
|
|
|
|
127 |
clear = gr.Button("Clear Chat")
|
|
|
128 |
|
|
|
|
|
|
|
129 |
# Handle sending messages
|
130 |
msg.submit(
|
131 |
respond,
|
132 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p, model],
|
133 |
+
[chatbot]
|
|
|
134 |
).then(
|
135 |
+
lambda: "",
|
136 |
+
None,
|
137 |
+
msg,
|
138 |
+
queue=False
|
139 |
+
)
|
140 |
+
|
141 |
+
send.click(
|
142 |
+
respond,
|
143 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p, model],
|
144 |
+
[chatbot]
|
145 |
+
).then(
|
146 |
+
lambda: "",
|
147 |
+
None,
|
148 |
+
msg,
|
149 |
+
queue=False
|
150 |
)
|
151 |
|
152 |
# Clear chat history
|
153 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
154 |
|
155 |
# Example prompts
|
156 |
gr.Examples(
|
|
|
161 |
],
|
162 |
inputs=msg
|
163 |
)
|
164 |
+
|
165 |
return demo
|
166 |
|
167 |
# Create and launch the interface
|
168 |
if __name__ == "__main__":
|
169 |
demo = create_chat_interface()
|
170 |
demo.queue()
|
|
|
171 |
demo.launch(
|
172 |
share=False, # Disable sharing on Spaces
|
173 |
+
)
|