Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,6 +49,7 @@ def embedding_to_text(embedding_tensor):
|
|
| 49 |
# 3. GRADIO UI (Same as before)
|
| 50 |
# ==========================================
|
| 51 |
|
|
|
|
| 52 |
def run_mixing(text1, text2):
|
| 53 |
if not text1 or not text2:
|
| 54 |
return "Please enter two sentences."
|
|
@@ -67,10 +68,33 @@ def run_mixing(text1, text2):
|
|
| 67 |
|
| 68 |
return embedding_to_text(v_mixed)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
with gr.Blocks(title="FLAN-T5 Latent Explorer", theme=gr.themes.Soft()) as demo:
|
| 71 |
gr.Markdown("# 🧠 FLAN-T5 Latent Space Mixer")
|
| 72 |
gr.Markdown(f"Running `{model_name}`. This model is instruction-tuned and much smarter than BART.")
|
| 73 |
-
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column():
|
| 76 |
t1 = gr.Textbox(label="Concept A", value="The King is powerful.")
|
|
@@ -80,6 +104,21 @@ with gr.Blocks(title="FLAN-T5 Latent Explorer", theme=gr.themes.Soft()) as demo:
|
|
| 80 |
out = gr.Textbox(label="Result", lines=2)
|
| 81 |
|
| 82 |
btn.click(run_mixing, inputs=[t1, t2], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch()
|
|
|
|
| 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."
|
|
|
|
| 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.")
|
|
|
|
| 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()
|