Update app.py
Browse files
app.py
CHANGED
@@ -139,7 +139,15 @@ def create_agent():
|
|
139 |
logger.error(f"Failed to create agent: {str(e)}")
|
140 |
raise
|
141 |
|
142 |
-
def respond(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
updated_history = history + [{"role": "user", "content": message}]
|
144 |
print("\n==== DEBUG ====")
|
145 |
print("User Message:", message)
|
@@ -147,7 +155,6 @@ def respond(message, history, temperature, max_new_tokens, max_tokens, multi_age
|
|
147 |
print("================\n")
|
148 |
|
149 |
try:
|
150 |
-
# Ensure correct format for run_gradio_chat
|
151 |
formatted_history = [(m["role"], m["content"]) for m in updated_history]
|
152 |
|
153 |
response_generator = agent.run_gradio_chat(
|
@@ -160,7 +167,7 @@ def respond(message, history, temperature, max_new_tokens, max_tokens, multi_age
|
|
160 |
max_round
|
161 |
)
|
162 |
except Exception as e:
|
163 |
-
return
|
164 |
|
165 |
collected = ""
|
166 |
for chunk in response_generator:
|
@@ -169,13 +176,13 @@ def respond(message, history, temperature, max_new_tokens, max_tokens, multi_age
|
|
169 |
else:
|
170 |
collected += str(chunk)
|
171 |
|
172 |
-
return
|
173 |
|
174 |
def create_demo(agent):
|
175 |
with gr.Blocks(css=chat_css) as demo:
|
176 |
chatbot = gr.Chatbot(label="TxAgent", type="messages")
|
177 |
with gr.Row():
|
178 |
-
msg = gr.Textbox(label="Your question")
|
179 |
with gr.Row():
|
180 |
temp = gr.Slider(0, 1, value=0.3, label="Temperature")
|
181 |
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
@@ -187,9 +194,10 @@ def create_demo(agent):
|
|
187 |
|
188 |
submit.click(
|
189 |
respond,
|
190 |
-
inputs=[
|
191 |
outputs=[chatbot]
|
192 |
)
|
|
|
193 |
return demo
|
194 |
|
195 |
def main():
|
|
|
139 |
logger.error(f"Failed to create agent: {str(e)}")
|
140 |
raise
|
141 |
|
142 |
+
def respond(chat_history, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
143 |
+
if not chat_history:
|
144 |
+
return history + [{"role": "assistant", "content": "Please provide a message."}]
|
145 |
+
|
146 |
+
message = chat_history[-1][1] if isinstance(chat_history[-1], (list, tuple)) else chat_history[-1]
|
147 |
+
|
148 |
+
if not isinstance(message, str) or len(message.strip()) <= 10:
|
149 |
+
return history + [{"role": "user", "content": message}, {"role": "assistant", "content": "Please provide a valid message with a string longer than 10 characters."}]
|
150 |
+
|
151 |
updated_history = history + [{"role": "user", "content": message}]
|
152 |
print("\n==== DEBUG ====")
|
153 |
print("User Message:", message)
|
|
|
155 |
print("================\n")
|
156 |
|
157 |
try:
|
|
|
158 |
formatted_history = [(m["role"], m["content"]) for m in updated_history]
|
159 |
|
160 |
response_generator = agent.run_gradio_chat(
|
|
|
167 |
max_round
|
168 |
)
|
169 |
except Exception as e:
|
170 |
+
return updated_history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
171 |
|
172 |
collected = ""
|
173 |
for chunk in response_generator:
|
|
|
176 |
else:
|
177 |
collected += str(chunk)
|
178 |
|
179 |
+
return updated_history + [{"role": "assistant", "content": collected}]
|
180 |
|
181 |
def create_demo(agent):
|
182 |
with gr.Blocks(css=chat_css) as demo:
|
183 |
chatbot = gr.Chatbot(label="TxAgent", type="messages")
|
184 |
with gr.Row():
|
185 |
+
msg = gr.Textbox(label="Your question") # not used in inputs anymore
|
186 |
with gr.Row():
|
187 |
temp = gr.Slider(0, 1, value=0.3, label="Temperature")
|
188 |
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
|
|
194 |
|
195 |
submit.click(
|
196 |
respond,
|
197 |
+
inputs=[chatbot, chatbot, temp, max_new_tokens, max_tokens, multi_agent, gr.State([]), max_rounds],
|
198 |
outputs=[chatbot]
|
199 |
)
|
200 |
+
|
201 |
return demo
|
202 |
|
203 |
def main():
|