Spaces:
Sleeping
Sleeping
fix gpu decorator outside gradio issue
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ import os
|
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
|
| 13 |
-
@spaces.GPU
|
| 14 |
def launch_app():
|
| 15 |
|
| 16 |
@spaces.GPU
|
|
@@ -81,42 +80,42 @@ def launch_app():
|
|
| 81 |
def forward(self, base, source=None, subspaces=None):
|
| 82 |
return torch.relu(self.proj(base))
|
| 83 |
|
| 84 |
-
# Load tokenizer and model
|
| 85 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
|
| 86 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto', token=hf_token).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 87 |
-
|
| 88 |
-
# Load fast model inference pipeline
|
| 89 |
-
pipe = pipeline(
|
| 90 |
-
task="text-generation",
|
| 91 |
-
model=model_name,
|
| 92 |
-
use_fast=True,
|
| 93 |
-
token=hf_token
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
path_to_params = hf_hub_download(
|
| 97 |
-
repo_id=interpreter_name,
|
| 98 |
-
filename=interpreter_path,
|
| 99 |
-
force_download=False,
|
| 100 |
-
)
|
| 101 |
-
params = torch.load(path_to_params, map_location="cuda" if torch.cuda.is_available() else "cpu")
|
| 102 |
-
encoder = Encoder(embed_dim=params.shape[0], latent_dim=params.shape[1]).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 103 |
-
encoder.proj.weight.data = params.float()
|
| 104 |
-
pv_model = pv.IntervenableModel({
|
| 105 |
-
"component": interpreter_component,
|
| 106 |
-
"intervention": encoder}, model=model).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 107 |
-
|
| 108 |
-
# Load dictionary
|
| 109 |
-
all_concepts = get_concepts_dictionary(dictionary_url)
|
| 110 |
-
|
| 111 |
-
description_text = """
|
| 112 |
-
## Does an LLM Think Like You?
|
| 113 |
-
Input a prompt and a concept that you think is most relevant for your prompt. See how much (if at all) the LLM uses that concept when processing your prompt.
|
| 114 |
-
Examples:
|
| 115 |
-
- **Prompt**: What is 2+2? **Concept**: math
|
| 116 |
-
- **Prompt**: I really like anchovies on pizza but I know a lot of people don't. **Concept**: food
|
| 117 |
-
"""
|
| 118 |
-
|
| 119 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
gr.Markdown(description_text)
|
| 121 |
with gr.Row():
|
| 122 |
prompt_input = gr.Textbox(label="Enter a prompt", value="I really like anchovies on pizza but I know a lot of people don't.")
|
|
|
|
| 10 |
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
|
| 12 |
|
|
|
|
| 13 |
def launch_app():
|
| 14 |
|
| 15 |
@spaces.GPU
|
|
|
|
| 80 |
def forward(self, base, source=None, subspaces=None):
|
| 81 |
return torch.relu(self.proj(base))
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
+
# Load tokenizer and model
|
| 85 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
|
| 86 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto', token=hf_token).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 87 |
+
|
| 88 |
+
# Load fast model inference pipeline
|
| 89 |
+
pipe = pipeline(
|
| 90 |
+
task="text-generation",
|
| 91 |
+
model=model_name,
|
| 92 |
+
use_fast=True,
|
| 93 |
+
token=hf_token
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
path_to_params = hf_hub_download(
|
| 97 |
+
repo_id=interpreter_name,
|
| 98 |
+
filename=interpreter_path,
|
| 99 |
+
force_download=False,
|
| 100 |
+
)
|
| 101 |
+
params = torch.load(path_to_params, map_location="cuda" if torch.cuda.is_available() else "cpu")
|
| 102 |
+
encoder = Encoder(embed_dim=params.shape[0], latent_dim=params.shape[1]).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 103 |
+
encoder.proj.weight.data = params.float()
|
| 104 |
+
pv_model = pv.IntervenableModel({
|
| 105 |
+
"component": interpreter_component,
|
| 106 |
+
"intervention": encoder}, model=model).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 107 |
+
|
| 108 |
+
# Load dictionary
|
| 109 |
+
all_concepts = get_concepts_dictionary(dictionary_url)
|
| 110 |
+
|
| 111 |
+
description_text = """
|
| 112 |
+
## Does an LLM Think Like You?
|
| 113 |
+
Input a prompt and a concept that you think is most relevant for your prompt. See how much (if at all) the LLM uses that concept when processing your prompt.
|
| 114 |
+
Examples:
|
| 115 |
+
- **Prompt**: What is 2+2? **Concept**: math
|
| 116 |
+
- **Prompt**: I really like anchovies on pizza but I know a lot of people don't. **Concept**: food
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
gr.Markdown(description_text)
|
| 120 |
with gr.Row():
|
| 121 |
prompt_input = gr.Textbox(label="Enter a prompt", value="I really like anchovies on pizza but I know a lot of people don't.")
|