Fixed the cannot enter the message bug
Browse files
app.py
CHANGED
@@ -16,12 +16,12 @@ logging.basicConfig(
|
|
16 |
logging.StreamHandler()
|
17 |
]
|
18 |
)
|
19 |
-
logger = logging.getLogger("
|
20 |
|
21 |
# Environment variables (for production use)
|
22 |
HF_MODEL = os.environ.get("HF_MODEL", "HuggingFaceH4/zephyr-7b-beta")
|
23 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", None) # Set your API token as env variable
|
24 |
-
COMPANY_NAME = os.environ.get("COMPANY_NAME", "
|
25 |
DEFAULT_SYSTEM_PROMPT = os.environ.get("DEFAULT_SYSTEM_PROMPT",
|
26 |
f"You are {COMPANY_NAME}'s professional AI assistant. Be helpful, accurate, and concise.")
|
27 |
|
@@ -41,10 +41,10 @@ def save_conversation(user_id, conversation):
|
|
41 |
json.dump(conversation, f)
|
42 |
logger.info(f"Saved conversation for user {user_id}")
|
43 |
|
44 |
-
# Main chat function
|
45 |
def respond(
|
46 |
message,
|
47 |
-
|
48 |
system_message,
|
49 |
max_tokens,
|
50 |
temperature,
|
@@ -52,16 +52,17 @@ def respond(
|
|
52 |
user_id
|
53 |
):
|
54 |
if not message.strip():
|
55 |
-
return
|
56 |
|
57 |
# Log the incoming request
|
58 |
logger.info(f"User {user_id} sent message - Length: {len(message)}")
|
59 |
|
60 |
try:
|
|
|
61 |
messages = [{"role": "system", "content": system_message}]
|
62 |
|
63 |
-
#
|
64 |
-
for user_msg, assistant_msg in
|
65 |
if user_msg:
|
66 |
messages.append({"role": "user", "content": user_msg})
|
67 |
if assistant_msg:
|
@@ -73,6 +74,7 @@ def respond(
|
|
73 |
full_response = ""
|
74 |
start_time = datetime.now()
|
75 |
|
|
|
76 |
for message_chunk in client.chat_completion(
|
77 |
messages,
|
78 |
max_tokens=max_tokens,
|
@@ -81,8 +83,9 @@ def respond(
|
|
81 |
top_p=top_p,
|
82 |
):
|
83 |
token = message_chunk.choices[0].delta.content
|
84 |
-
|
85 |
-
|
|
|
86 |
|
87 |
# Log completion
|
88 |
time_taken = (datetime.now() - start_time).total_seconds()
|
@@ -103,10 +106,16 @@ def respond(
|
|
103 |
}
|
104 |
save_conversation(user_id, conversation_data)
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
except Exception as e:
|
107 |
error_msg = f"An error occurred: {str(e)}"
|
108 |
logger.error(f"Error generating response for user {user_id}: {str(e)}")
|
109 |
-
return
|
110 |
|
111 |
# Authentication function (replace with your actual auth system)
|
112 |
def authenticate(username, password):
|
@@ -372,8 +381,8 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
372 |
msg_and_submit.then(lambda: gr.update(value=""), None, [msg])
|
373 |
submit_click.then(lambda: gr.update(value=""), None, [msg])
|
374 |
|
375 |
-
# Clear chat button
|
376 |
-
clear_btn.click(lambda:
|
377 |
|
378 |
# Export conversation
|
379 |
def export_conversation(chat_history, uid):
|
|
|
16 |
logging.StreamHandler()
|
17 |
]
|
18 |
)
|
19 |
+
logger = logging.getLogger("CompanyChatbot")
|
20 |
|
21 |
# Environment variables (for production use)
|
22 |
HF_MODEL = os.environ.get("HF_MODEL", "HuggingFaceH4/zephyr-7b-beta")
|
23 |
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", None) # Set your API token as env variable
|
24 |
+
COMPANY_NAME = os.environ.get("COMPANY_NAME", "Your Company")
|
25 |
DEFAULT_SYSTEM_PROMPT = os.environ.get("DEFAULT_SYSTEM_PROMPT",
|
26 |
f"You are {COMPANY_NAME}'s professional AI assistant. Be helpful, accurate, and concise.")
|
27 |
|
|
|
41 |
json.dump(conversation, f)
|
42 |
logger.info(f"Saved conversation for user {user_id}")
|
43 |
|
44 |
+
# Main chat function - fixed to properly handle message format
|
45 |
def respond(
|
46 |
message,
|
47 |
+
history,
|
48 |
system_message,
|
49 |
max_tokens,
|
50 |
temperature,
|
|
|
52 |
user_id
|
53 |
):
|
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})
|
68 |
if assistant_msg:
|
|
|
74 |
full_response = ""
|
75 |
start_time = datetime.now()
|
76 |
|
77 |
+
# Stream the response
|
78 |
for message_chunk in client.chat_completion(
|
79 |
messages,
|
80 |
max_tokens=max_tokens,
|
|
|
83 |
top_p=top_p,
|
84 |
):
|
85 |
token = message_chunk.choices[0].delta.content
|
86 |
+
if token:
|
87 |
+
full_response += token
|
88 |
+
yield history + [[message, full_response]]
|
89 |
|
90 |
# Log completion
|
91 |
time_taken = (datetime.now() - start_time).total_seconds()
|
|
|
106 |
}
|
107 |
save_conversation(user_id, conversation_data)
|
108 |
|
109 |
+
# Return final history with the complete response
|
110 |
+
# This is necessary if streaming didn't yield anything
|
111 |
+
if full_response:
|
112 |
+
return history + [[message, full_response]]
|
113 |
+
return history
|
114 |
+
|
115 |
except Exception as e:
|
116 |
error_msg = f"An error occurred: {str(e)}"
|
117 |
logger.error(f"Error generating response for user {user_id}: {str(e)}")
|
118 |
+
return history + [[message, error_msg]]
|
119 |
|
120 |
# Authentication function (replace with your actual auth system)
|
121 |
def authenticate(username, password):
|
|
|
381 |
msg_and_submit.then(lambda: gr.update(value=""), None, [msg])
|
382 |
submit_click.then(lambda: gr.update(value=""), None, [msg])
|
383 |
|
384 |
+
# Clear chat button - fixed to properly reset the history list
|
385 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
386 |
|
387 |
# Export conversation
|
388 |
def export_conversation(chat_history, uid):
|