Spaces:
Runtime error
Runtime error
Update app.py
Browse files15 unmodified lines1617181920212220 unmodified lines43444546474849505194 unmodified lines1461471481491501511521531541558 unmodified lines164165166167168169170171172173177 unmodified lines3513523533543553563577 unmodified lines36536636736836937037128 unmodified lines40040140240340440517 unmodified lines42342442542642742842943050 unmodified lines48148248348448548648715 unmodified lines# --- END SYSTEM PROMPT ---# --- NEW: Online Learning Hyperparameter ---ONLINE_LEARNING_EPOCHS = 1# --- END NEW HYPERPARAMETER ---20 unmodified lines# English-only sanitation:# Define characters that should stick to words (letters, digits, apostrophe, hyphen)_WORD_TOKEN_CHARS = r"a-z0-9\'\-"# Define punctuation that should be split as separate tokens (e.g., comma, period, question mark)_SPLIT_PUNCT = r"\.\,\!\?\:\;\(\)\[\]\{\}\"\/\\@#\$%\^&\*\+<>=~`"_token_re = re.compile(rf"[{_WORD_TOKEN_CHARS}]+|[{_SPLIT_PUNCT}]", flags=re.IGNORECASE)94 unmodified linesn_layers = 12dropout = 0.1# FIX: Increased max_length for longer thinking/response timemax_length = 512k_sampling = 20# FIX 1: Reduced LEARNING_RATELEARNING_RATE = 1e-6voc = Voc()gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)8 unmodified linesn_layers = 12dropout = 0.1# FIX: Increased max_length for longer thinking/response timemax_length = 512k_sampling = 20# FIX 1: Reduced LEARNING_RATELEARNING_RATE = 1e-6gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)# Only load state if we have a real checkpoint177 unmodified linesif not history or not history[-1][1]:return history, "Cannot process feedback: No recent AI response found."last_user_message, last_ai_response = history[-1]# 1. Log the event (always)print(f"\n--- FEEDBACK RECEIVED ---")7 unmodified linesconversation_context = ""# The history passed here is the full chat history, so the previous turn is history[-2]if len(history) >= 2 and history[-2][1]:last_user, last_ai = history[-2]conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"# 3. Trigger online update (training) only for good responses28 unmodified lines# 1. Handle file upload (Placeholder)file_context = ""if uploaded_file:file_name = uploaded_file.name.split("/")[-1] # Simple extraction of filenamefile_context = f"The user has provided a file named '{file_name}'. Analyze or respond to the user's prompt *about this file*."print(f"File attached: {file_name}")17 unmodified lines# The history passed in already includes the current user message, so we look at the one before thatcontext_history = history[:-1] if not is_instruction else historyif context_history and context_history[-1][1]:last_user, last_ai = context_history[-1]conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"print(f"Context included: {conversation_context}")50 unmodified lines# --- 1. MAIN CHAT LOGIC (Using submit handler, NOT gr.Interface) ---# We use a helper function to set the user message in history before calling the main API handlerdef add_user_message_to_history(message, history, file_ref, web_enabled):# The JS front-end sends the cleaned message to the UI here.# This function updates the history to show the user message (with a temporary None for response)# We ensure the file context is visible in the history if a file was attachedif file_ref:15 unmodified lines1617181920212220 unmodified lines43444546474849505194 unmodified lines1461471481491501511521531541558 unmodified lines164165166167168169170171172173177 unmodified lines3513523533543553563577 unmodified lines36536636736836937037128 unmodified lines40040140240340440540617 unmodified lines42442542642742842943043150 unmodified lines48248348448548648715 unmodified lines# --- END SYSTEM PROMPT ---# --- NEW: Online Learning Hyperparameter ---ONLINE_LEARNING_EPOCHS = 1# --- END NEW HYPERPARAMETER ---20 unmodified lines# English-only sanitation:# Define characters that should stick to words (letters, digits, apostrophe, hyphen)_WORD_TOKEN_CHARS = r"a-z0-9\'\-"# Define punctuation that should be split as separate tokens (e.g., comma, period, question mark)_SPLIT_PUNCT = r"\.\,\!\?\:\;\(\)\[\]\{\}\"\/\\@#\$%\^&\*\+\<>=~`"_token_re = re.compile(rf"[{_WORD_TOKEN_CHARS}]+|[{_SPLIT_PUNCT}]", flags=re.IGNORECASE)94 unmodified linesn_layers = 12dropout = 0.1# FIX: Increased max_length for longer thinking/response timemax_length = 512k_sampling = 20# FIX 1: Reduced LEARNING_RATELEARNING_RATE = 1e-6voc = Voc()gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)8 unmodified linesn_layers = 12dropout = 0.1# FIX: Increased max_length for longer thinking/response timemax_length = 512k_sampling = 20# FIX 1: Reduced LEARNING_RATELEARNING_RATE = 1e-6gpt_model = GPT(voc.n_words, d_model, n_layers, n_head, d_ff, dropout).to(device)# Only load state if we have a real checkpoint177 unmodified linesif not history or not history[-1][1]:return history, "Cannot process feedback: No recent AI response found."last_user_message, last_ai_response = history[-1][0], history[-1][1]# 1. Log the event (always)print(f"\n--- FEEDBACK RECEIVED ---")7 unmodified linesconversation_context = ""# The history passed here is the full chat history, so the previous turn is history[-2]if len(history) >= 2 and history[-2][1]:last_user, last_ai = history[-2][0], history[-2][1]conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"# 3. Trigger online update (training) only for good responses28 unmodified lines# 1. Handle file upload (Placeholder)file_context = ""if uploaded_file:# uploaded_file is a Gradio file object; its path is in uploaded_file.namefile_name = uploaded_file.name.split("/")[-1] # Simple extraction of filenamefile_context = f"The user has provided a file named '{file_name}'. Analyze or respond to the user's prompt *about this file*."print(f"File attached: {file_name}")17 unmodified lines# The history passed in already includes the current user message, so we look at the one before thatcontext_history = history[:-1] if not is_instruction else historyif context_history and len(context_history[-1]) >= 2 and context_history[-1][1]:last_user, last_ai = context_history[-1][0], context_history[-1][1]conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"print(f"Context included: {conversation_context}")50 unmodified lines# --- 1. MAIN CHAT LOGIC (Using submit handler, NOT gr.Interface) ---# We use a helper function to set the user message in history before calling the main API handlerdef add_user_message_to_history(message, history, file_ref, web_enabled):# This function updates the history to show the user message (with a temporary None for response)# We ensure the file context is visible in the history if a file was attachedif file_ref:
app.py+12-12