Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
3a7a5c6
1
Parent(s):
adc8fc7
update
Browse files- app.py +4 -0
- src/attribution/attntrace.py +8 -2
- src/models/Llama.py +0 -1
- src/prompts.py +1 -1
app.py
CHANGED
@@ -9,6 +9,7 @@ import numpy as np
|
|
9 |
import spaces
|
10 |
import nltk
|
11 |
import base64
|
|
|
12 |
from src.utils import split_into_sentences as split_into_sentences_utils
|
13 |
# --- AttnTrace imports (from app_full.py) ---
|
14 |
from src.models import create_model
|
@@ -114,6 +115,7 @@ def initialize_model_and_attr():
|
|
114 |
except Exception as e:
|
115 |
error_msg = f"Error initializing model/traceback: {str(e)}"
|
116 |
print(error_msg)
|
|
|
117 |
return None, None, error_msg
|
118 |
|
119 |
# Initialize with defaults
|
@@ -608,6 +610,7 @@ def basic_get_scores_and_sources_full_response(state: State):
|
|
608 |
state,
|
609 |
)
|
610 |
except Exception as e:
|
|
|
611 |
return (
|
612 |
gr.update(value=[("", None)], visible=False),
|
613 |
gr.update(selected=0),
|
@@ -822,6 +825,7 @@ def basic_get_scores_and_sources(
|
|
822 |
state,
|
823 |
)
|
824 |
except Exception as e:
|
|
|
825 |
return (
|
826 |
gr.update(value=[("", None)], visible=False),
|
827 |
gr.update(selected=0),
|
|
|
9 |
import spaces
|
10 |
import nltk
|
11 |
import base64
|
12 |
+
import traceback
|
13 |
from src.utils import split_into_sentences as split_into_sentences_utils
|
14 |
# --- AttnTrace imports (from app_full.py) ---
|
15 |
from src.models import create_model
|
|
|
115 |
except Exception as e:
|
116 |
error_msg = f"Error initializing model/traceback: {str(e)}"
|
117 |
print(error_msg)
|
118 |
+
traceback.print_exc()
|
119 |
return None, None, error_msg
|
120 |
|
121 |
# Initialize with defaults
|
|
|
610 |
state,
|
611 |
)
|
612 |
except Exception as e:
|
613 |
+
traceback.print_exc()
|
614 |
return (
|
615 |
gr.update(value=[("", None)], visible=False),
|
616 |
gr.update(selected=0),
|
|
|
825 |
state,
|
826 |
)
|
827 |
except Exception as e:
|
828 |
+
traceback.print_exc()
|
829 |
return (
|
830 |
gr.update(value=[("", None)], visible=False),
|
831 |
gr.update(selected=0),
|
src/attribution/attntrace.py
CHANGED
@@ -10,13 +10,14 @@ from .attention_utils import *
|
|
10 |
class AttnTraceAttribution(Attribution):
|
11 |
def __init__(self, llm,explanation_level = "segment",K=5, avg_k=5, q=0.4, B=30, verbose =1):
|
12 |
super().__init__(llm,explanation_level,K,verbose)
|
13 |
-
self.
|
|
|
14 |
self.model_type = llm.provider
|
15 |
self.tokenizer = llm.tokenizer
|
16 |
self.avg_k = avg_k
|
17 |
self.q = q
|
18 |
self.B = B
|
19 |
-
|
20 |
self.explanation_level = explanation_level
|
21 |
|
22 |
def loss_to_importance(self,losses, sentences_id_list):
|
@@ -37,6 +38,11 @@ class AttnTraceAttribution(Attribution):
|
|
37 |
return importances
|
38 |
def attribute(self, question: str, contexts: list, answer: str,explained_answer: str, customized_template: str = None):
|
39 |
start_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
40 |
model = self.model
|
41 |
tokenizer = self.tokenizer
|
42 |
model.eval() # Set model to evaluation mode
|
|
|
10 |
class AttnTraceAttribution(Attribution):
|
11 |
def __init__(self, llm,explanation_level = "segment",K=5, avg_k=5, q=0.4, B=30, verbose =1):
|
12 |
super().__init__(llm,explanation_level,K,verbose)
|
13 |
+
self.llm = llm # Use float16 for the model
|
14 |
+
self.model = None
|
15 |
self.model_type = llm.provider
|
16 |
self.tokenizer = llm.tokenizer
|
17 |
self.avg_k = avg_k
|
18 |
self.q = q
|
19 |
self.B = B
|
20 |
+
|
21 |
self.explanation_level = explanation_level
|
22 |
|
23 |
def loss_to_importance(self,losses, sentences_id_list):
|
|
|
38 |
return importances
|
39 |
def attribute(self, question: str, contexts: list, answer: str,explained_answer: str, customized_template: str = None):
|
40 |
start_time = time.time()
|
41 |
+
if self.llm.model!=None:
|
42 |
+
self.model = self.llm.model
|
43 |
+
else:
|
44 |
+
self.model = self.llm._load_model_if_needed()
|
45 |
+
self.layers = range(len(self.model.model.layers))
|
46 |
model = self.model
|
47 |
tokenizer = self.tokenizer
|
48 |
model.eval() # Set model to evaluation mode
|
src/models/Llama.py
CHANGED
@@ -42,7 +42,6 @@ class Llama(Model):
|
|
42 |
messages,
|
43 |
add_generation_prompt=True,
|
44 |
return_tensors="pt",
|
45 |
-
padding=True,
|
46 |
truncation=True
|
47 |
).to(model.device)
|
48 |
|
|
|
42 |
messages,
|
43 |
add_generation_prompt=True,
|
44 |
return_tensors="pt",
|
|
|
45 |
truncation=True
|
46 |
).to(model.device)
|
47 |
|
src/prompts.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
MULTIPLE_PROMPT_FORCE = 'You are a helpful assistant, below is a query from a user and some relevant contexts. \
|
2 |
-
Answer the
|
3 |
\n\nContexts: [context] \n\nQuery: [question] \n\nAnswer:'
|
4 |
|
5 |
SELF_CITATION_PROMPT = """You are a helpful assistant, below is a query from a user, some relevant contexts, and an answer to the query.
|
|
|
1 |
MULTIPLE_PROMPT_FORCE = 'You are a helpful assistant, below is a query from a user and some relevant contexts. \
|
2 |
+
Answer the query given the information in those contexts.\
|
3 |
\n\nContexts: [context] \n\nQuery: [question] \n\nAnswer:'
|
4 |
|
5 |
SELF_CITATION_PROMPT = """You are a helpful assistant, below is a query from a user, some relevant contexts, and an answer to the query.
|