change the model
Browse files
app.py
CHANGED
|
@@ -18,16 +18,20 @@ logging.basicConfig(
|
|
| 18 |
)
|
| 19 |
logger = logging.getLogger("CompanyChatbot")
|
| 20 |
|
| 21 |
-
# Environment variables
|
| 22 |
-
HF_MODEL = os.environ.get("HF_MODEL", "
|
| 23 |
-
HF_API_TOKEN = os.environ.get("HF_API_TOKEN"
|
| 24 |
COMPANY_NAME = os.environ.get("COMPANY_NAME", "AI")
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
# Initialize the client
|
| 29 |
try:
|
| 30 |
-
client = InferenceClient(HF_MODEL, token=HF_API_TOKEN)
|
| 31 |
logger.info(f"Successfully initialized InferenceClient with model: {HF_MODEL}")
|
| 32 |
except Exception as e:
|
| 33 |
logger.error(f"Failed to initialize InferenceClient: {str(e)}")
|
|
@@ -63,7 +67,7 @@ def respond(message, chat_history, user_id):
|
|
| 63 |
|
| 64 |
for user_msg, assistant_msg in chat_history:
|
| 65 |
messages.append({"role": "user", "content": user_msg})
|
| 66 |
-
if assistant_msg:
|
| 67 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 68 |
|
| 69 |
messages.append({"role": "user", "content": message})
|
|
@@ -161,7 +165,7 @@ def login(username, password):
|
|
| 161 |
gr.update(visible=True, value="Invalid username or password")
|
| 162 |
)
|
| 163 |
|
| 164 |
-
# CSS styling
|
| 165 |
css = """
|
| 166 |
body {
|
| 167 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
@@ -251,7 +255,6 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 251 |
with gr.Row():
|
| 252 |
gr.Markdown(f"<div class='company-header'><h1>{COMPANY_NAME} AI Assistant</h1></div>")
|
| 253 |
|
| 254 |
-
# Login Group
|
| 255 |
with gr.Group(visible=True) as login_group:
|
| 256 |
with gr.Column(elem_classes=["login-container"]):
|
| 257 |
gr.Markdown(f"<div class='login-header'><h2>Welcome to {COMPANY_NAME}</h2><p>Please log in to continue</p></div>")
|
|
@@ -260,10 +263,8 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 260 |
password = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
|
| 261 |
login_button = gr.Button("Login", variant="primary", size="lg")
|
| 262 |
|
| 263 |
-
# Chat Group
|
| 264 |
with gr.Group(visible=False) as chat_group:
|
| 265 |
with gr.Row():
|
| 266 |
-
# Settings Panel
|
| 267 |
with gr.Column(scale=1, elem_classes=["setting-panel"]):
|
| 268 |
role_indicator = gr.Markdown("", elem_id="role-indicator")
|
| 269 |
|
|
@@ -273,7 +274,7 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 273 |
value=DEFAULT_SYSTEM_PROMPT,
|
| 274 |
label="System Instructions",
|
| 275 |
lines=4,
|
| 276 |
-
interactive=False
|
| 277 |
)
|
| 278 |
max_tokens = gr.Slider(
|
| 279 |
minimum=1,
|
|
@@ -310,7 +311,6 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 310 |
|
| 311 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 312 |
|
| 313 |
-
# Chat Interface
|
| 314 |
with gr.Column(scale=2, elem_classes=["chat-container"]):
|
| 315 |
chatbot = gr.Chatbot(elem_classes=["chatbox"])
|
| 316 |
with gr.Row():
|
|
@@ -321,7 +321,6 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 321 |
)
|
| 322 |
submit_btn = gr.Button("Send", variant="primary")
|
| 323 |
|
| 324 |
-
# Event handlers
|
| 325 |
def update_role_display(role):
|
| 326 |
badge_class = "admin-badge" if role == "admin" else "user-badge"
|
| 327 |
role_display = "Administrator" if role == "admin" else "Standard User"
|
|
@@ -330,14 +329,13 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 330 |
def handle_role_permissions(role):
|
| 331 |
is_admin = role == "admin"
|
| 332 |
return [
|
| 333 |
-
gr.update(interactive=is_admin),
|
| 334 |
-
gr.update(interactive=is_admin),
|
| 335 |
-
gr.update(interactive=is_admin),
|
| 336 |
-
gr.update(interactive=is_admin),
|
| 337 |
-
gr.update(visible=is_admin),
|
| 338 |
]
|
| 339 |
|
| 340 |
-
# Login handler
|
| 341 |
login_button.click(
|
| 342 |
login,
|
| 343 |
inputs=[username, password],
|
|
@@ -352,14 +350,12 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 352 |
outputs=[system_message, max_tokens, temperature, top_p, update_config_btn]
|
| 353 |
)
|
| 354 |
|
| 355 |
-
# Configuration update handler
|
| 356 |
update_config_btn.click(
|
| 357 |
update_config,
|
| 358 |
inputs=[system_message, max_tokens, temperature, top_p, user_role],
|
| 359 |
outputs=[config_status]
|
| 360 |
)
|
| 361 |
|
| 362 |
-
# Chat handlers
|
| 363 |
msg.submit(
|
| 364 |
respond,
|
| 365 |
inputs=[msg, chatbot, user_id],
|
|
@@ -372,10 +368,8 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 372 |
outputs=[chatbot]
|
| 373 |
).then(lambda: "", None, [msg])
|
| 374 |
|
| 375 |
-
# Clear chat
|
| 376 |
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 377 |
|
| 378 |
-
# Export conversation
|
| 379 |
def export_conversation(chat_history, uid):
|
| 380 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 381 |
filename = f"conversations/export_{uid}_{timestamp}.json"
|
|
@@ -391,7 +385,6 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 391 |
outputs=[error_message]
|
| 392 |
)
|
| 393 |
|
| 394 |
-
# Logout handler
|
| 395 |
def logout():
|
| 396 |
return gr.update(visible=True), gr.update(visible=False), None, None
|
| 397 |
|
|
@@ -400,14 +393,10 @@ with gr.Blocks(css=css, title=f"{COMPANY_NAME} AI Assistant") as demo:
|
|
| 400 |
outputs=[login_group, chat_group, user_id, user_role]
|
| 401 |
)
|
| 402 |
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
auth=None,
|
| 411 |
-
)
|
| 412 |
-
else:
|
| 413 |
-
demo.launch(show_error=True)
|
|
|
|
| 18 |
)
|
| 19 |
logger = logging.getLogger("CompanyChatbot")
|
| 20 |
|
| 21 |
+
# Environment variables (Set these in Hugging Face Spaces settings)
|
| 22 |
+
HF_MODEL = os.environ.get("HF_MODEL", "mistralai/Mixtral-8x7B-Instruct-v0.1") # Powerful free model
|
| 23 |
+
HF_API_TOKEN = os.environ.get("HF_API_TOKEN") # Must be set in Spaces settings
|
| 24 |
COMPANY_NAME = os.environ.get("COMPANY_NAME", "AI")
|
| 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 |
|
| 28 |
+
# Validate API token
|
| 29 |
+
if not HF_API_TOKEN:
|
| 30 |
+
raise RuntimeError("HF_API_TOKEN environment variable is not set. Please configure it in Spaces settings.")
|
| 31 |
+
|
| 32 |
# Initialize the client
|
| 33 |
try:
|
| 34 |
+
client = InferenceClient(model=HF_MODEL, token=HF_API_TOKEN)
|
| 35 |
logger.info(f"Successfully initialized InferenceClient with model: {HF_MODEL}")
|
| 36 |
except Exception as e:
|
| 37 |
logger.error(f"Failed to initialize InferenceClient: {str(e)}")
|
|
|
|
| 67 |
|
| 68 |
for user_msg, assistant_msg in chat_history:
|
| 69 |
messages.append({"role": "user", "content": user_msg})
|
| 70 |
+
if assistant_msg:
|
| 71 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 72 |
|
| 73 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 165 |
gr.update(visible=True, value="Invalid username or password")
|
| 166 |
)
|
| 167 |
|
| 168 |
+
# CSS styling
|
| 169 |
css = """
|
| 170 |
body {
|
| 171 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
|
|
| 255 |
with gr.Row():
|
| 256 |
gr.Markdown(f"<div class='company-header'><h1>{COMPANY_NAME} AI Assistant</h1></div>")
|
| 257 |
|
|
|
|
| 258 |
with gr.Group(visible=True) as login_group:
|
| 259 |
with gr.Column(elem_classes=["login-container"]):
|
| 260 |
gr.Markdown(f"<div class='login-header'><h2>Welcome to {COMPANY_NAME}</h2><p>Please log in to continue</p></div>")
|
|
|
|
| 263 |
password = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
|
| 264 |
login_button = gr.Button("Login", variant="primary", size="lg")
|
| 265 |
|
|
|
|
| 266 |
with gr.Group(visible=False) as chat_group:
|
| 267 |
with gr.Row():
|
|
|
|
| 268 |
with gr.Column(scale=1, elem_classes=["setting-panel"]):
|
| 269 |
role_indicator = gr.Markdown("", elem_id="role-indicator")
|
| 270 |
|
|
|
|
| 274 |
value=DEFAULT_SYSTEM_PROMPT,
|
| 275 |
label="System Instructions",
|
| 276 |
lines=4,
|
| 277 |
+
interactive=False
|
| 278 |
)
|
| 279 |
max_tokens = gr.Slider(
|
| 280 |
minimum=1,
|
|
|
|
| 311 |
|
| 312 |
logout_btn = gr.Button("Logout", variant="stop")
|
| 313 |
|
|
|
|
| 314 |
with gr.Column(scale=2, elem_classes=["chat-container"]):
|
| 315 |
chatbot = gr.Chatbot(elem_classes=["chatbox"])
|
| 316 |
with gr.Row():
|
|
|
|
| 321 |
)
|
| 322 |
submit_btn = gr.Button("Send", variant="primary")
|
| 323 |
|
|
|
|
| 324 |
def update_role_display(role):
|
| 325 |
badge_class = "admin-badge" if role == "admin" else "user-badge"
|
| 326 |
role_display = "Administrator" if role == "admin" else "Standard User"
|
|
|
|
| 329 |
def handle_role_permissions(role):
|
| 330 |
is_admin = role == "admin"
|
| 331 |
return [
|
| 332 |
+
gr.update(interactive=is_admin),
|
| 333 |
+
gr.update(interactive=is_admin),
|
| 334 |
+
gr.update(interactive=is_admin),
|
| 335 |
+
gr.update(interactive=is_admin),
|
| 336 |
+
gr.update(visible=is_admin),
|
| 337 |
]
|
| 338 |
|
|
|
|
| 339 |
login_button.click(
|
| 340 |
login,
|
| 341 |
inputs=[username, password],
|
|
|
|
| 350 |
outputs=[system_message, max_tokens, temperature, top_p, update_config_btn]
|
| 351 |
)
|
| 352 |
|
|
|
|
| 353 |
update_config_btn.click(
|
| 354 |
update_config,
|
| 355 |
inputs=[system_message, max_tokens, temperature, top_p, user_role],
|
| 356 |
outputs=[config_status]
|
| 357 |
)
|
| 358 |
|
|
|
|
| 359 |
msg.submit(
|
| 360 |
respond,
|
| 361 |
inputs=[msg, chatbot, user_id],
|
|
|
|
| 368 |
outputs=[chatbot]
|
| 369 |
).then(lambda: "", None, [msg])
|
| 370 |
|
|
|
|
| 371 |
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 372 |
|
|
|
|
| 373 |
def export_conversation(chat_history, uid):
|
| 374 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 375 |
filename = f"conversations/export_{uid}_{timestamp}.json"
|
|
|
|
| 385 |
outputs=[error_message]
|
| 386 |
)
|
| 387 |
|
|
|
|
| 388 |
def logout():
|
| 389 |
return gr.update(visible=True), gr.update(visible=False), None, None
|
| 390 |
|
|
|
|
| 393 |
outputs=[login_group, chat_group, user_id, user_role]
|
| 394 |
)
|
| 395 |
|
| 396 |
+
# Launch the app for Hugging Face Spaces
|
| 397 |
+
demo.launch(
|
| 398 |
+
server_name="0.0.0.0",
|
| 399 |
+
server_port=7860,
|
| 400 |
+
share=False,
|
| 401 |
+
show_error=True
|
| 402 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|