vikhyatk commited on
Commit
1250026
1 Parent(s): f5730b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -2
app.py CHANGED
@@ -20,6 +20,24 @@ moondream = AutoModelForCausalLM.from_pretrained(
20
  )
21
  moondream.eval()
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  @spaces.GPU(duration=10)
25
  def answer_question(img, prompt):
@@ -74,8 +92,8 @@ def process_answer(img, answer):
74
  with gr.Blocks() as demo:
75
  gr.Markdown(
76
  """
77
- # 🌔 moondream2
78
- A tiny vision language model. [GitHub](https://github.com/vikhyat/moondream)
79
  """
80
  )
81
  with gr.Row():
 
20
  )
21
  moondream.eval()
22
 
23
+ control_vectors = torch.load("control_vectors.pt")
24
+ control_vectors = [t.to('cuda', dtype=torch.bfloat16) for t in control_vectors]
25
+
26
+ class LayerWrapper(torch.nn.Module):
27
+ def __init__(self, og_layer, control_vectors, scale=4.2):
28
+ super().__init__()
29
+ self.og_layer = og_layer
30
+ self.control_vectors = control_vectors
31
+ self.scale = scale
32
+
33
+ def forward(self, *args, **kwargs):
34
+ layer_outputs = self.og_layer(*args, **kwargs)
35
+ layer_outputs = (layer_outputs[0] + self.scale * self.control_vectors, *layer_outputs[1:])
36
+ return layer_outputs
37
+
38
+ moondream.text_model.transformer.h = torch.nn.ModuleList([
39
+ LayerWrapper(layer, vector, 4.2) for layer, vector in zip(og_h, control_vectors)
40
+ ])
41
 
42
  @spaces.GPU(duration=10)
43
  def answer_question(img, prompt):
 
92
  with gr.Blocks() as demo:
93
  gr.Markdown(
94
  """
95
+ # 🌔 contemplative moondream
96
+ a demo of [moondream](http://moondream.ai) steered to discuss the meaning of life using [activation vectors](https://github.com/vikhyat/moondream/blob/main/notebooks/RepEng.ipynb)
97
  """
98
  )
99
  with gr.Row():