Spaces:
Runtime error
Runtime error
File size: 5,732 Bytes
1f653d2 8d1dac1 f97a8f6 190f654 8d1dac1 f97a8f6 8d1dac1 1f653d2 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 190f654 f97a8f6 190f654 f97a8f6 8d1dac1 f97a8f6 8d1dac1 190f654 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 8d1dac1 f97a8f6 190f654 24f2d00 f97a8f6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import json
import traceback
import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, LogitsProcessorList
from trl import PPOTrainer, PPOConfig
import gradio as gr
# -----------------------------------------------------------------------------
# 1. Helpers
# -----------------------------------------------------------------------------
def make_json_serializable(obj):
"""
Recursively convert any torch.Tensor in obj to Python lists.
"""
if isinstance(obj, torch.Tensor):
return obj.cpu().tolist()
elif isinstance(obj, dict):
return {k: make_json_serializable(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [make_json_serializable(v) for v in obj]
return obj
def safe_json_dumps(data):
"""
Dump JSON with our converter to avoid Tensor serialization errors.
"""
return json.dumps(
make_json_serializable(data),
indent=2,
ensure_ascii=False
)
# -----------------------------------------------------------------------------
# 2. Load Models and Initialize PPO Agent
# -----------------------------------------------------------------------------
MODEL_NAME = "google/flan-t5-base"
# Core seq2seq model & tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
# PPO configuration
ppo_config = PPOConfig(
model_name=MODEL_NAME,
learning_rate=1e-5,
batch_size=1,
log_with=None # switch to "wandb" or "tensorboard" if you like
)
# Wrap FLAN-T5 in a PPO agent
ppo_trainer = PPOTrainer(
config=ppo_config,
model=model,
tokenizer=tokenizer
)
# -----------------------------------------------------------------------------
# 3. Session State
# -----------------------------------------------------------------------------
current_session = {
"dialog": [] # each entry: {"user": str, "bot": str, "reward": float or None}
}
# -----------------------------------------------------------------------------
# 4. Core Callback Functions
# -----------------------------------------------------------------------------
def reset_session():
"""
Clear the conversation and return an empty chat history.
"""
global current_session
current_session = {"dialog": []}
return []
def chat_with_agent(user_input: str):
"""
Generate the model's reply, append to session, and return full chat history.
"""
global current_session
try:
# Tokenize user prompt and generate
inputs = tokenizer(user_input, return_tensors="pt").input_ids
outputs = model.generate(
inputs,
max_new_tokens=128,
do_sample=True,
top_p=0.9,
temperature=0.8
)
bot_reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Store in session
current_session["dialog"].append({
"user": user_input,
"bot": bot_reply,
"reward": None
})
# Prepare for Gradio Chatbot: list of (user, bot)
history = [
(turn["user"], turn["bot"])
for turn in current_session["dialog"]
]
return history
except Exception as e:
print("π₯ Error in chat_with_agent:", e)
traceback.print_exc()
# On failure, leave session untouched
return [("Error:", "Failed to generate reply. Check logs.")]
def rate_and_train(rating: float):
"""
Take the last bot replyβs rating, run a PPO step, and return serialized session.
"""
global current_session
try:
if not current_session["dialog"]:
return "No dialog to rate. Chat first."
# Attach reward
last = current_session["dialog"][-1]
last["reward"] = float(rating)
# Prepare for PPO step
user_text = last["user"]
bot_text = last["bot"]
# Token IDs for PPO
query_ids = tokenizer(user_text, return_tensors="pt").input_ids.squeeze(0)
response_ids = tokenizer(bot_text, return_tensors="pt").input_ids.squeeze(0)
# Run PPO optimization with this single example
stats = ppo_trainer.step(
[query_ids],
[response_ids],
[last["reward"]]
)
print("π PPO step stats:", stats)
# Return the entire session as JSON
return safe_json_dumps(current_session)
except Exception as e:
print("π₯ Error in rate_and_train:", e)
traceback.print_exc()
return "Failed to apply training step. See logs."
# -----------------------------------------------------------------------------
# 5. Gradio UI
# -----------------------------------------------------------------------------
with gr.Blocks() as demo:
gr.Markdown("## FLAN-T5 Chatbot with On-the-Fly Reinforcement Learning")
chat_box = gr.Chatbot(label="Chat History")
user_input = gr.Textbox(placeholder="Type your message hereβ¦", label="You")
send_btn = gr.Button("Send")
reset_btn = gr.Button("Reset Conversation")
with gr.Row():
rating = gr.Slider(0, 5, step=1, value=0, label="Rate Last Reply")
rate_btn = gr.Button("Apply Rating & Train")
export_json = gr.Textbox(label="Session JSON", lines=10)
# Reset chat
reset_btn.click(
fn=reset_session,
inputs=None,
outputs=chat_box
)
# Send user message
send_btn.click(
fn=chat_with_agent,
inputs=user_input,
outputs=chat_box
)
# Rate & train
rate_btn.click(
fn=rate_and_train,
inputs=rating,
outputs=export_json
)
if __name__ == "__main__":
demo.launch()
|