Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,21 +56,10 @@ MODEL_NAMES = list(MODELS.keys())
|
|
| 56 |
def generate_response(history, system_message, max_tokens, selected_model):
|
| 57 |
"""
|
| 58 |
Generates the response.
|
| 59 |
-
History is expected to be a list of dicts: [{'role':'user', 'content':'...'}, ...]
|
| 60 |
"""
|
| 61 |
-
# Get the last user message
|
| 62 |
if not history:
|
| 63 |
return history
|
| 64 |
|
| 65 |
-
last_msg = history[-1]["content"]
|
| 66 |
-
# History passed to API should exclude the very last message we just added locally,
|
| 67 |
-
# but OpenAI API expects the full context.
|
| 68 |
-
# We slice history for the context to avoid duplicating if logic separates them,
|
| 69 |
-
# but here 'history' contains the new prompt already appended by the UI event.
|
| 70 |
-
|
| 71 |
-
# Separate the last message (current prompt) from context if needed,
|
| 72 |
-
# but standard OpenAI chat structure includes it.
|
| 73 |
-
|
| 74 |
try:
|
| 75 |
model_config = MODELS[selected_model]
|
| 76 |
provider = model_config["provider"]
|
|
@@ -88,10 +77,8 @@ def generate_response(history, system_message, max_tokens, selected_model):
|
|
| 88 |
else:
|
| 89 |
final_system_message = system_message
|
| 90 |
|
| 91 |
-
# Construct messages for API
|
| 92 |
messages = [{"role": "system", "content": final_system_message}]
|
| 93 |
-
|
| 94 |
-
# Sanitize history for API (ensure only role/content keys exist if gradio adds metadata)
|
| 95 |
api_history = [{"role": m["role"], "content": m["content"]} for m in history]
|
| 96 |
messages.extend(api_history)
|
| 97 |
|
|
@@ -126,7 +113,6 @@ def generate_response(history, system_message, max_tokens, selected_model):
|
|
| 126 |
else:
|
| 127 |
final_response = english_response
|
| 128 |
|
| 129 |
-
# Append bot response to history
|
| 130 |
history.append({"role": "assistant", "content": final_response})
|
| 131 |
return history
|
| 132 |
|
|
@@ -135,7 +121,6 @@ def generate_response(history, system_message, max_tokens, selected_model):
|
|
| 135 |
return history
|
| 136 |
|
| 137 |
def user_input(user_message, history):
|
| 138 |
-
"""Updates history with user message immediately"""
|
| 139 |
if not user_message:
|
| 140 |
return history, ""
|
| 141 |
history.append({"role": "user", "content": user_message})
|
|
@@ -354,7 +339,7 @@ with gr.Blocks(
|
|
| 354 |
chatbot = gr.Chatbot(
|
| 355 |
type="messages",
|
| 356 |
show_label=False,
|
| 357 |
-
avatar_images=None,
|
| 358 |
render_markdown=True,
|
| 359 |
elem_id="main-chatbot"
|
| 360 |
)
|
|
@@ -391,29 +376,24 @@ with gr.Blocks(
|
|
| 391 |
system_message = gr.Textbox(
|
| 392 |
value="Anda adalah asisten AI. Jawab dalam Bahasa Indonesia, di bawah 100 kata. Berikan label yang jelas antara fakta dan inferensi.",
|
| 393 |
label="PRIME DIRECTIVE (SYSTEM PROMPT)",
|
| 394 |
-
lines=1,
|
| 395 |
max_lines=3
|
| 396 |
)
|
| 397 |
|
| 398 |
-
# 5. Examples (
|
| 399 |
-
gr.
|
| 400 |
-
|
| 401 |
-
[
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
|
| 409 |
# ------------------------------------------------------------------
|
| 410 |
# Event Wiring
|
| 411 |
# ------------------------------------------------------------------
|
| 412 |
-
|
| 413 |
-
# When user submits:
|
| 414 |
-
# 1. Clear input box immediately & append user msg to chatbot (user_input)
|
| 415 |
-
# 2. Call generate_response to get bot answer & update chatbot
|
| 416 |
-
|
| 417 |
msg_input.submit(
|
| 418 |
user_input,
|
| 419 |
inputs=[msg_input, chatbot],
|
|
|
|
| 56 |
def generate_response(history, system_message, max_tokens, selected_model):
|
| 57 |
"""
|
| 58 |
Generates the response.
|
|
|
|
| 59 |
"""
|
|
|
|
| 60 |
if not history:
|
| 61 |
return history
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
model_config = MODELS[selected_model]
|
| 65 |
provider = model_config["provider"]
|
|
|
|
| 77 |
else:
|
| 78 |
final_system_message = system_message
|
| 79 |
|
|
|
|
| 80 |
messages = [{"role": "system", "content": final_system_message}]
|
| 81 |
+
# Sanitize history
|
|
|
|
| 82 |
api_history = [{"role": m["role"], "content": m["content"]} for m in history]
|
| 83 |
messages.extend(api_history)
|
| 84 |
|
|
|
|
| 113 |
else:
|
| 114 |
final_response = english_response
|
| 115 |
|
|
|
|
| 116 |
history.append({"role": "assistant", "content": final_response})
|
| 117 |
return history
|
| 118 |
|
|
|
|
| 121 |
return history
|
| 122 |
|
| 123 |
def user_input(user_message, history):
|
|
|
|
| 124 |
if not user_message:
|
| 125 |
return history, ""
|
| 126 |
history.append({"role": "user", "content": user_message})
|
|
|
|
| 339 |
chatbot = gr.Chatbot(
|
| 340 |
type="messages",
|
| 341 |
show_label=False,
|
| 342 |
+
avatar_images=None,
|
| 343 |
render_markdown=True,
|
| 344 |
elem_id="main-chatbot"
|
| 345 |
)
|
|
|
|
| 376 |
system_message = gr.Textbox(
|
| 377 |
value="Anda adalah asisten AI. Jawab dalam Bahasa Indonesia, di bawah 100 kata. Berikan label yang jelas antara fakta dan inferensi.",
|
| 378 |
label="PRIME DIRECTIVE (SYSTEM PROMPT)",
|
| 379 |
+
lines=1,
|
| 380 |
max_lines=3
|
| 381 |
)
|
| 382 |
|
| 383 |
+
# 5. Examples (Fixed: Wrapped in Column to apply CSS class)
|
| 384 |
+
with gr.Column(elem_classes="examples-container"):
|
| 385 |
+
gr.Examples(
|
| 386 |
+
examples=[
|
| 387 |
+
["Jelaskan penggunaan King's Safety Stock dalam inventory management."],
|
| 388 |
+
["Rancang strategi optimasi layout toko retail menggunakan Market Basket Analysis."],
|
| 389 |
+
["Analisis fenomena 'Quiet Quitting' di startup teknologi Indonesia."],
|
| 390 |
+
],
|
| 391 |
+
inputs=[msg_input]
|
| 392 |
+
)
|
| 393 |
|
| 394 |
# ------------------------------------------------------------------
|
| 395 |
# Event Wiring
|
| 396 |
# ------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
msg_input.submit(
|
| 398 |
user_input,
|
| 399 |
inputs=[msg_input, chatbot],
|