Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ SYSTEM_INSTRUCTION = "You are a friendly and concise general-purpose AI assistan
|
|
| 16 |
# --- END SYSTEM PROMPT ---
|
| 17 |
|
| 18 |
# --- NEW: Online Learning Hyperparameter ---
|
| 19 |
-
ONLINE_LEARNING_EPOCHS = 1
|
| 20 |
# --- END NEW HYPERPARAMETER ---
|
| 21 |
|
| 22 |
|
|
@@ -43,9 +43,9 @@ class Voc:
|
|
| 43 |
|
| 44 |
# English-only sanitation:
|
| 45 |
# Define characters that should stick to words (letters, digits, apostrophe, hyphen)
|
| 46 |
-
_WORD_TOKEN_CHARS = r"a-z0-9\'\-"
|
| 47 |
# Define punctuation that should be split as separate tokens (e.g., comma, period, question mark)
|
| 48 |
-
_SPLIT_PUNCT = r"\.\,\!\?\:\;\(\)\[\]\{\}\"\/\\@#\$%\^&\*\+<>=~`"
|
| 49 |
|
| 50 |
_token_re = re.compile(rf"[{_WORD_TOKEN_CHARS}]+|[{_SPLIT_PUNCT}]", flags=re.IGNORECASE)
|
| 51 |
|
|
@@ -146,10 +146,10 @@ if not os.path.exists(CHECKPOINT_PATH):
|
|
| 146 |
n_layers = 12
|
| 147 |
dropout = 0.1
|
| 148 |
# FIX: Increased max_length for longer thinking/response time
|
| 149 |
-
max_length = 512
|
| 150 |
k_sampling = 20
|
| 151 |
# FIX 1: Reduced LEARNING_RATE
|
| 152 |
-
LEARNING_RATE = 1e-6
|
| 153 |
|
| 154 |
voc = Voc()
|
| 155 |
gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)
|
|
@@ -164,10 +164,10 @@ else:
|
|
| 164 |
n_layers = 12
|
| 165 |
dropout = 0.1
|
| 166 |
# FIX: Increased max_length for longer thinking/response time
|
| 167 |
-
max_length = 512
|
| 168 |
k_sampling = 20
|
| 169 |
# FIX 1: Reduced LEARNING_RATE
|
| 170 |
-
LEARNING_RATE = 1e-6
|
| 171 |
|
| 172 |
gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)
|
| 173 |
# Only load state if we have a real checkpoint
|
|
@@ -351,7 +351,7 @@ def process_feedback(history, rating):
|
|
| 351 |
if not history or not history[-1][1]:
|
| 352 |
return history, "Cannot process feedback: No recent AI response found."
|
| 353 |
|
| 354 |
-
last_user_message, last_ai_response = history[-1]
|
| 355 |
|
| 356 |
# 1. Log the event (always)
|
| 357 |
print(f"\n--- FEEDBACK RECEIVED ---")
|
|
@@ -365,7 +365,7 @@ def process_feedback(history, rating):
|
|
| 365 |
conversation_context = ""
|
| 366 |
# The history passed here is the full chat history, so the previous turn is history[-2]
|
| 367 |
if len(history) >= 2 and history[-2][1]:
|
| 368 |
-
last_user, last_ai = history[-2]
|
| 369 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 370 |
|
| 371 |
# 3. Trigger online update (training) only for good responses
|
|
@@ -400,6 +400,7 @@ def on_submit(user_message, history, uploaded_file, web_search_enabled):
|
|
| 400 |
# 1. Handle file upload (Placeholder)
|
| 401 |
file_context = ""
|
| 402 |
if uploaded_file:
|
|
|
|
| 403 |
file_name = uploaded_file.name.split("/")[-1] # Simple extraction of filename
|
| 404 |
file_context = f"The user has provided a file named '{file_name}'. Analyze or respond to the user's prompt *about this file*."
|
| 405 |
print(f"File attached: {file_name}")
|
|
@@ -423,8 +424,8 @@ def on_submit(user_message, history, uploaded_file, web_search_enabled):
|
|
| 423 |
# The history passed in already includes the current user message, so we look at the one before that
|
| 424 |
context_history = history[:-1] if not is_instruction else history
|
| 425 |
|
| 426 |
-
if context_history and context_history[-1][1]:
|
| 427 |
-
last_user, last_ai = context_history[-1]
|
| 428 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 429 |
print(f"Context included: {conversation_context}")
|
| 430 |
|
|
@@ -481,7 +482,6 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 481 |
# --- 1. MAIN CHAT LOGIC (Using submit handler, NOT gr.Interface) ---
|
| 482 |
# We use a helper function to set the user message in history before calling the main API handler
|
| 483 |
def add_user_message_to_history(message, history, file_ref, web_enabled):
|
| 484 |
-
# The JS front-end sends the cleaned message to the UI here.
|
| 485 |
# This function updates the history to show the user message (with a temporary None for response)
|
| 486 |
# We ensure the file context is visible in the history if a file was attached
|
| 487 |
if file_ref:
|
|
|
|
| 16 |
# --- END SYSTEM PROMPT ---
|
| 17 |
|
| 18 |
# --- NEW: Online Learning Hyperparameter ---
|
| 19 |
+
ONLINE_LEARNING_EPOCHS = 1
|
| 20 |
# --- END NEW HYPERPARAMETER ---
|
| 21 |
|
| 22 |
|
|
|
|
| 43 |
|
| 44 |
# English-only sanitation:
|
| 45 |
# Define characters that should stick to words (letters, digits, apostrophe, hyphen)
|
| 46 |
+
_WORD_TOKEN_CHARS = r"a-z0-9\'\-"
|
| 47 |
# Define punctuation that should be split as separate tokens (e.g., comma, period, question mark)
|
| 48 |
+
_SPLIT_PUNCT = r"\.\,\!\?\:\;\(\)\[\]\{\}\"\/\\@#\$%\^&\*\+\<>=~`"
|
| 49 |
|
| 50 |
_token_re = re.compile(rf"[{_WORD_TOKEN_CHARS}]+|[{_SPLIT_PUNCT}]", flags=re.IGNORECASE)
|
| 51 |
|
|
|
|
| 146 |
n_layers = 12
|
| 147 |
dropout = 0.1
|
| 148 |
# FIX: Increased max_length for longer thinking/response time
|
| 149 |
+
max_length = 512
|
| 150 |
k_sampling = 20
|
| 151 |
# FIX 1: Reduced LEARNING_RATE
|
| 152 |
+
LEARNING_RATE = 1e-6
|
| 153 |
|
| 154 |
voc = Voc()
|
| 155 |
gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)
|
|
|
|
| 164 |
n_layers = 12
|
| 165 |
dropout = 0.1
|
| 166 |
# FIX: Increased max_length for longer thinking/response time
|
| 167 |
+
max_length = 512
|
| 168 |
k_sampling = 20
|
| 169 |
# FIX 1: Reduced LEARNING_RATE
|
| 170 |
+
LEARNING_RATE = 1e-6
|
| 171 |
|
| 172 |
gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)
|
| 173 |
# Only load state if we have a real checkpoint
|
|
|
|
| 351 |
if not history or not history[-1][1]:
|
| 352 |
return history, "Cannot process feedback: No recent AI response found."
|
| 353 |
|
| 354 |
+
last_user_message, last_ai_response = history[-1][0], history[-1][1]
|
| 355 |
|
| 356 |
# 1. Log the event (always)
|
| 357 |
print(f"\n--- FEEDBACK RECEIVED ---")
|
|
|
|
| 365 |
conversation_context = ""
|
| 366 |
# The history passed here is the full chat history, so the previous turn is history[-2]
|
| 367 |
if len(history) >= 2 and history[-2][1]:
|
| 368 |
+
last_user, last_ai = history[-2][0], history[-2][1]
|
| 369 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 370 |
|
| 371 |
# 3. Trigger online update (training) only for good responses
|
|
|
|
| 400 |
# 1. Handle file upload (Placeholder)
|
| 401 |
file_context = ""
|
| 402 |
if uploaded_file:
|
| 403 |
+
# uploaded_file is a Gradio file object; its path is in uploaded_file.name
|
| 404 |
file_name = uploaded_file.name.split("/")[-1] # Simple extraction of filename
|
| 405 |
file_context = f"The user has provided a file named '{file_name}'. Analyze or respond to the user's prompt *about this file*."
|
| 406 |
print(f"File attached: {file_name}")
|
|
|
|
| 424 |
# The history passed in already includes the current user message, so we look at the one before that
|
| 425 |
context_history = history[:-1] if not is_instruction else history
|
| 426 |
|
| 427 |
+
if context_history and len(context_history[-1]) >= 2 and context_history[-1][1]:
|
| 428 |
+
last_user, last_ai = context_history[-1][0], context_history[-1][1]
|
| 429 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 430 |
print(f"Context included: {conversation_context}")
|
| 431 |
|
|
|
|
| 482 |
# --- 1. MAIN CHAT LOGIC (Using submit handler, NOT gr.Interface) ---
|
| 483 |
# We use a helper function to set the user message in history before calling the main API handler
|
| 484 |
def add_user_message_to_history(message, history, file_ref, web_enabled):
|
|
|
|
| 485 |
# This function updates the history to show the user message (with a temporary None for response)
|
| 486 |
# We ensure the file context is visible in the history if a file was attached
|
| 487 |
if file_ref:
|