everydaytok commited on
Commit
509cc45
·
verified ·
1 Parent(s): c9308b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -65
app.py CHANGED
@@ -4,10 +4,9 @@ from transformers import T5Tokenizer, T5ForConditionalGeneration
4
  from transformers.modeling_outputs import BaseModelOutput
5
 
6
  # ==========================================
7
- # 1. SETUP: Load FLAN-T5 (Smarter than BART)
8
  # ==========================================
9
- # Options: "google/flan-t5-base" (fast) or "google/flan-t5-large" (smart)
10
- model_name = "google/flan-t5-large" # "google/flan-t5-large"
11
 
12
  print(f"Loading {model_name}...")
13
  tokenizer = T5Tokenizer.from_pretrained(model_name)
@@ -15,110 +14,92 @@ model = T5ForConditionalGeneration.from_pretrained(model_name)
15
  model.eval()
16
 
17
  # ==========================================
18
- # 2. CORE LOGIC
19
  # ==========================================
20
 
21
  def text_to_embedding(text):
22
- # T5 requires a "prompt" structure usually, but for raw encoding, just text is fine
23
  inputs = tokenizer(text, return_tensors="pt")
24
-
25
  with torch.no_grad():
26
- # Get the output of the T5 Encoder
27
  encoder_outputs = model.encoder(**inputs)
28
-
29
  return encoder_outputs.last_hidden_state
30
 
31
  def embedding_to_text(embedding_tensor):
32
- # Wrap the vector so T5's decoder accepts it
33
  encoder_outputs_wrapped = BaseModelOutput(last_hidden_state=embedding_tensor)
34
-
35
  with torch.no_grad():
36
- # Generate text using the decoder
37
  generated_ids = model.generate(
38
  encoder_outputs=encoder_outputs_wrapped,
39
- max_length=100, # T5 can handle slightly longer outputs
40
  num_beams=5,
41
- repetition_penalty=2.5, # T5 likes to repeat, this penalty stops "And cats. And cats."
42
  early_stopping=True
43
  )
44
-
45
- decoded_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
46
- return decoded_text
47
-
48
- # ==========================================
49
- # 3. GRADIO UI (Same as before)
50
- # ==========================================
51
-
52
 
53
  def run_mixing(text1, text2):
54
- if not text1 or not text2:
55
- return "Please enter two sentences."
56
-
57
- print(f"Mixing: '{text1}' + '{text2}'")
58
  v1 = text_to_embedding(text1)
59
  v2 = text_to_embedding(text2)
60
-
61
- # Truncate to match lengths
62
  min_len = min(v1.shape[1], v2.shape[1])
63
  v1 = v1[:, :min_len, :]
64
  v2 = v2[:, :min_len, :]
65
-
66
- # Average them
67
  v_mixed = (v1 + v2) / 2.0
68
-
69
  return embedding_to_text(v_mixed)
70
 
71
- # Update the mixing function to accept a 'ratio' (0.0 to 1.0)
72
  def run_weighted_mixing(text1, text2, mix_ratio):
73
- if not text1 or not text2:
74
- return "Please enter two sentences."
75
 
76
- # 1. Get vectors
77
  v1 = text_to_embedding(text1)
78
  v2 = text_to_embedding(text2)
79
 
80
- # 2. Align lengths
81
  min_len = min(v1.shape[1], v2.shape[1])
82
  v1 = v1[:, :min_len, :]
83
  v2 = v2[:, :min_len, :]
84
 
85
- # 3. Weighted Average
86
- # if ratio is 0.0 -> 100% Text1
87
- # if ratio is 1.0 -> 100% Text2
88
- # if ratio is 0.5 -> 50/50
89
  v_mixed = (v1 * (1 - mix_ratio)) + (v2 * mix_ratio)
90
-
91
  return embedding_to_text(v_mixed)
92
 
 
 
 
93
 
94
  with gr.Blocks(title="FLAN-T5 Latent Explorer", theme=gr.themes.Soft()) as demo:
95
  gr.Markdown("# 🧠 FLAN-T5 Latent Space Mixer")
96
- gr.Markdown(f"Running `{model_name}`. This model is instruction-tuned and much smarter than BART.")
97
-
98
- with gr.Row():
99
- with gr.Column():
100
- t1 = gr.Textbox(label="Concept A", value="The King is powerful.")
101
- t2 = gr.Textbox(label="Concept B", value="The woman is beautiful.")
102
- btn = gr.Button("Mix Vectors", variant="primary")
103
- with gr.Column():
104
- out = gr.Textbox(label="Result", lines=2)
105
-
106
- btn.click(run_mixing, inputs=[t1, t2], outputs=out)
107
- # Update the mixing function to accept a 'ratio' (0.0 to 1.0)
108
-
109
- # In the Gradio UI section:
110
- with gr.TabItem("2. Vector Mixing"):
111
- with gr.Row():
112
- t1 = gr.Textbox(label="Start Sentence", value="The dog is happy.")
113
- t2 = gr.Textbox(label="End Sentence", value="The cat is angry.")
114
-
115
- # Add a slider
116
- slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.1, label="Mixing Ratio (Left = Start, Right = End)")
117
-
118
- btn_mix = gr.Button("Morph")
119
- out = gr.Textbox(label="Result")
120
 
121
- btn_mix.click(run_weighted_mixing, inputs=[t1, t2, slider], outputs=out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  if __name__ == "__main__":
124
  demo.launch()
 
4
  from transformers.modeling_outputs import BaseModelOutput
5
 
6
  # ==========================================
7
+ # 1. SETUP
8
  # ==========================================
9
+ model_name = "google/flan-t5-large"
 
10
 
11
  print(f"Loading {model_name}...")
12
  tokenizer = T5Tokenizer.from_pretrained(model_name)
 
14
  model.eval()
15
 
16
  # ==========================================
17
+ # 2. LOGIC
18
  # ==========================================
19
 
20
  def text_to_embedding(text):
 
21
  inputs = tokenizer(text, return_tensors="pt")
 
22
  with torch.no_grad():
 
23
  encoder_outputs = model.encoder(**inputs)
 
24
  return encoder_outputs.last_hidden_state
25
 
26
  def embedding_to_text(embedding_tensor):
 
27
  encoder_outputs_wrapped = BaseModelOutput(last_hidden_state=embedding_tensor)
 
28
  with torch.no_grad():
 
29
  generated_ids = model.generate(
30
  encoder_outputs=encoder_outputs_wrapped,
31
+ max_length=100,
32
  num_beams=5,
33
+ repetition_penalty=2.5,
34
  early_stopping=True
35
  )
36
+ return tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
 
 
 
 
 
 
37
 
38
  def run_mixing(text1, text2):
39
+ if not text1 or not text2: return "Please enter two sentences."
40
+
 
 
41
  v1 = text_to_embedding(text1)
42
  v2 = text_to_embedding(text2)
43
+
44
+ # Truncate to min length
45
  min_len = min(v1.shape[1], v2.shape[1])
46
  v1 = v1[:, :min_len, :]
47
  v2 = v2[:, :min_len, :]
48
+
49
+ # 50/50 Average
50
  v_mixed = (v1 + v2) / 2.0
 
51
  return embedding_to_text(v_mixed)
52
 
 
53
  def run_weighted_mixing(text1, text2, mix_ratio):
54
+ if not text1 or not text2: return "Please enter two sentences."
 
55
 
 
56
  v1 = text_to_embedding(text1)
57
  v2 = text_to_embedding(text2)
58
 
 
59
  min_len = min(v1.shape[1], v2.shape[1])
60
  v1 = v1[:, :min_len, :]
61
  v2 = v2[:, :min_len, :]
62
 
63
+ # Weighted Average formula
 
 
 
64
  v_mixed = (v1 * (1 - mix_ratio)) + (v2 * mix_ratio)
 
65
  return embedding_to_text(v_mixed)
66
 
67
+ # ==========================================
68
+ # 3. GRADIO UI (FIXED STRUCTURE)
69
+ # ==========================================
70
 
71
  with gr.Blocks(title="FLAN-T5 Latent Explorer", theme=gr.themes.Soft()) as demo:
72
  gr.Markdown("# 🧠 FLAN-T5 Latent Space Mixer")
73
+ gr.Markdown(f"Running `{model_name}`.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ # We need a Tabs container to hold the TabItems
76
+ with gr.Tabs():
77
+
78
+ # --- TAB 1: 50/50 MIX ---
79
+ with gr.TabItem("1. Simple Mix (50/50)"):
80
+ with gr.Row():
81
+ with gr.Column():
82
+ t1_simple = gr.Textbox(label="Concept A", value="The King is powerful.")
83
+ t2_simple = gr.Textbox(label="Concept B", value="The woman is beautiful.")
84
+ btn_simple = gr.Button("Mix Vectors", variant="primary")
85
+ with gr.Column():
86
+ out_simple = gr.Textbox(label="Result", lines=2)
87
+
88
+ btn_simple.click(run_mixing, inputs=[t1_simple, t2_simple], outputs=out_simple)
89
+
90
+ # --- TAB 2: WEIGHTED SLIDER ---
91
+ with gr.TabItem("2. Weighted Morph (Slider)"):
92
+ gr.Markdown("Slide between the two sentences to see how the meaning shifts.")
93
+ with gr.Row():
94
+ t1_morph = gr.Textbox(label="Start Sentence", value="The dog is happy.")
95
+ t2_morph = gr.Textbox(label="End Sentence", value="The cat is angry.")
96
+
97
+ slider = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, step=0.1, label="Mixing Ratio (Left = Start, Right = End)")
98
+
99
+ btn_morph = gr.Button("Morph", variant="primary")
100
+ out_morph = gr.Textbox(label="Result")
101
+
102
+ btn_morph.click(run_weighted_mixing, inputs=[t1_morph, t2_morph, slider], outputs=out_morph)
103
 
104
  if __name__ == "__main__":
105
  demo.launch()