Update app.py
Browse files
app.py
CHANGED
|
@@ -54,14 +54,11 @@ def respond(
|
|
| 54 |
if not message.strip():
|
| 55 |
return history + [[message, "I'm sorry, I didn't receive any input. How can I help you today?"]]
|
| 56 |
|
| 57 |
-
# Log the incoming request
|
| 58 |
logger.info(f"User {user_id} sent message - Length: {len(message)}")
|
| 59 |
|
| 60 |
try:
|
| 61 |
-
# Prepare messages for the model
|
| 62 |
messages = [{"role": "system", "content": system_message}]
|
| 63 |
|
| 64 |
-
# Format history correctly
|
| 65 |
for user_msg, assistant_msg in history:
|
| 66 |
if user_msg:
|
| 67 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -70,11 +67,12 @@ def respond(
|
|
| 70 |
|
| 71 |
messages.append({"role": "user", "content": message})
|
| 72 |
|
| 73 |
-
# Generate response
|
| 74 |
full_response = ""
|
| 75 |
start_time = datetime.now()
|
| 76 |
|
| 77 |
-
#
|
|
|
|
|
|
|
| 78 |
for message_chunk in client.chat_completion(
|
| 79 |
messages,
|
| 80 |
max_tokens=max_tokens,
|
|
@@ -85,13 +83,13 @@ def respond(
|
|
| 85 |
token = message_chunk.choices[0].delta.content
|
| 86 |
if token:
|
| 87 |
full_response += token
|
| 88 |
-
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
# Log completion
|
| 91 |
time_taken = (datetime.now() - start_time).total_seconds()
|
| 92 |
logger.info(f"Response generated for user {user_id} in {time_taken:.2f}s - Length: {len(full_response)}")
|
| 93 |
|
| 94 |
-
# Save conversation for audit/analytics
|
| 95 |
conversation_data = {
|
| 96 |
"timestamp": datetime.now().isoformat(),
|
| 97 |
"user_id": user_id,
|
|
@@ -106,11 +104,10 @@ def respond(
|
|
| 106 |
}
|
| 107 |
save_conversation(user_id, conversation_data)
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
return history
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
error_msg = f"An error occurred: {str(e)}"
|
|
@@ -334,8 +331,9 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 334 |
return f"<h3>Role: <span class='role-badge user-badge'>Standard User</span></h3>"
|
| 335 |
|
| 336 |
def handle_role_permissions(role):
|
|
|
|
| 337 |
if role == "admin":
|
| 338 |
-
return gr.update(visible=True)
|
| 339 |
else:
|
| 340 |
return gr.update(visible=True, elem_classes=["setting-disabled"])
|
| 341 |
|
|
@@ -356,11 +354,12 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 356 |
|
| 357 |
# Chat functionality
|
| 358 |
def chat_with_saved_params(message, history, uid, role):
|
| 359 |
-
#
|
| 360 |
system_msg = system_message.value
|
| 361 |
-
tokens = max_tokens.value
|
| 362 |
-
temp = temperature.value
|
| 363 |
-
topp = top_p.value
|
|
|
|
| 364 |
return respond(message, history, system_msg, tokens, temp, topp, uid)
|
| 365 |
|
| 366 |
msg_and_submit = msg.submit(
|
|
@@ -413,6 +412,11 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 413 |
with open("styles.css", "w") as f:
|
| 414 |
f.write(css)
|
| 415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
if __name__ == "__main__":
|
| 417 |
# Check if we're running in production
|
| 418 |
if os.environ.get("PRODUCTION", "false").lower() == "true":
|
|
|
|
| 54 |
if not message.strip():
|
| 55 |
return history + [[message, "I'm sorry, I didn't receive any input. How can I help you today?"]]
|
| 56 |
|
|
|
|
| 57 |
logger.info(f"User {user_id} sent message - Length: {len(message)}")
|
| 58 |
|
| 59 |
try:
|
|
|
|
| 60 |
messages = [{"role": "system", "content": system_message}]
|
| 61 |
|
|
|
|
| 62 |
for user_msg, assistant_msg in history:
|
| 63 |
if user_msg:
|
| 64 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 67 |
|
| 68 |
messages.append({"role": "user", "content": message})
|
| 69 |
|
|
|
|
| 70 |
full_response = ""
|
| 71 |
start_time = datetime.now()
|
| 72 |
|
| 73 |
+
# Ensure proper response format for streaming
|
| 74 |
+
current_history = history.copy()
|
| 75 |
+
|
| 76 |
for message_chunk in client.chat_completion(
|
| 77 |
messages,
|
| 78 |
max_tokens=max_tokens,
|
|
|
|
| 83 |
token = message_chunk.choices[0].delta.content
|
| 84 |
if token:
|
| 85 |
full_response += token
|
| 86 |
+
# Always ensure a list of lists with pairs
|
| 87 |
+
new_history = current_history + [[message, full_response]]
|
| 88 |
+
yield new_history
|
| 89 |
|
|
|
|
| 90 |
time_taken = (datetime.now() - start_time).total_seconds()
|
| 91 |
logger.info(f"Response generated for user {user_id} in {time_taken:.2f}s - Length: {len(full_response)}")
|
| 92 |
|
|
|
|
| 93 |
conversation_data = {
|
| 94 |
"timestamp": datetime.now().isoformat(),
|
| 95 |
"user_id": user_id,
|
|
|
|
| 104 |
}
|
| 105 |
save_conversation(user_id, conversation_data)
|
| 106 |
|
| 107 |
+
# Ensure final response is properly formatted
|
| 108 |
+
if not full_response:
|
| 109 |
+
return history
|
| 110 |
+
return current_history + [[message, full_response]]
|
|
|
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
error_msg = f"An error occurred: {str(e)}"
|
|
|
|
| 331 |
return f"<h3>Role: <span class='role-badge user-badge'>Standard User</span></h3>"
|
| 332 |
|
| 333 |
def handle_role_permissions(role):
|
| 334 |
+
# Only disable settings for non-admin users
|
| 335 |
if role == "admin":
|
| 336 |
+
return gr.update(visible=True, elem_classes=[]) # Remove any disabled classes
|
| 337 |
else:
|
| 338 |
return gr.update(visible=True, elem_classes=["setting-disabled"])
|
| 339 |
|
|
|
|
| 354 |
|
| 355 |
# Chat functionality
|
| 356 |
def chat_with_saved_params(message, history, uid, role):
|
| 357 |
+
# Get current values of parameters
|
| 358 |
system_msg = system_message.value
|
| 359 |
+
tokens = max_tokens.value if role == "admin" else 512 # Default value for non-admin
|
| 360 |
+
temp = temperature.value if role == "admin" else 0.7 # Default value for non-admin
|
| 361 |
+
topp = top_p.value if role == "admin" else 0.95 # Default value for non-admin
|
| 362 |
+
|
| 363 |
return respond(message, history, system_msg, tokens, temp, topp, uid)
|
| 364 |
|
| 365 |
msg_and_submit = msg.submit(
|
|
|
|
| 412 |
with open("styles.css", "w") as f:
|
| 413 |
f.write(css)
|
| 414 |
|
| 415 |
+
css = css.replace(
|
| 416 |
+
".setting-disabled {",
|
| 417 |
+
".setting-disabled input, .setting-disabled button, .setting-disabled select, .setting-disabled textarea {"
|
| 418 |
+
)
|
| 419 |
+
|
| 420 |
if __name__ == "__main__":
|
| 421 |
# Check if we're running in production
|
| 422 |
if os.environ.get("PRODUCTION", "false").lower() == "true":
|