Lihuchen commited on
Commit
e8fd69f
1 Parent(s): a2901a2

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. confidence.py +74 -0
  3. cpu_llama_generate.py +5 -2
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from cpu_llama_generate import run
3
 
4
 
5
  def greet(query):
 
1
  import gradio as gr
2
+ from confidence import run
3
 
4
 
5
  def greet(query):
confidence.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from nltk.tokenize import sent_tokenize
2
+ #from tiny_llama import generate_answer
3
+ #from llama_generate import generate_answer
4
+ from cpu_llama_generate import generate_answer
5
+
6
+ def get_yes_or_no(result):
7
+ if 'yes' in str.lower(result)[:5]:return 'Yes'
8
+ if 'no' in str.lower(result)[:5]:return 'No'
9
+ return 'N/A'
10
+
11
+
12
+ def check_score(context, sentences):
13
+ score_mapping = {'Yes':1.0, 'No':0.0}
14
+ template = """
15
+ Context: {a}
16
+ Sentence: {b}
17
+ is the sentence supported by the context above?
18
+ Answer "Yes" or "No"
19
+ """
20
+ scores, results = list(), list()
21
+ for sentence in sentences:
22
+ content = template.format(a=context.strip().replace('/n', ''), b=sentence.strip().replace('/n', ''))
23
+ result = generate_answer(content, sample_num=1)[0]
24
+ print(result)
25
+ results.append(result)
26
+
27
+ results = [get_yes_or_no(r) for r in results]
28
+ scores = [score_mapping.get(result, 0.5) for result in results]
29
+
30
+ # for sent, score in zip(sentences, scores):
31
+ # print(sent.strip(), score)
32
+ #result_string += sent + ' ({a})'.format(a=score)
33
+
34
+ return scores
35
+
36
+
37
+ def run(query, sample_size=5):
38
+ sampled = generate_answer(query, sample_size+1)
39
+ answer = sampled[0]
40
+ proofs = sampled[1:]
41
+ sentences = sent_tokenize(answer)
42
+
43
+ all_scores = list()
44
+ for proof in proofs:
45
+ scores = check_score(proof, sentences)
46
+ all_scores.append(scores)
47
+
48
+ final_content = ''
49
+ avg_confidence = list()
50
+ for index, scores in enumerate(zip(*all_scores)):
51
+ sentence_confidence = sum(scores) / len(scores)
52
+ avg_confidence.append(sentence_confidence)
53
+ final_content += sentences[index].strip() + ' ({a}) '.format(a=sentence_confidence)
54
+ avg_confidence = sum(avg_confidence) / len(avg_confidence)
55
+ final_content += '\nThe confidence score of this answer is {a}'.format(a=avg_confidence)
56
+ return final_content
57
+
58
+
59
+ if __name__ == '__main__':
60
+ # result = generate_answer(query="Who is Lihu Chen?", sample_num=3)
61
+ # print(result)
62
+
63
+ # context = """
64
+ # Lihu Chen is an American writer and artist who works in comics. They received their degree in psychology from California State University, Fullerton and have worked on titles such as "The Gathering Storm" and "Heartthrob".
65
+ # """
66
+ # sentences = sent_tokenize("""
67
+ # Lihu Chen is an American writer and artist who works in comics. They received their degree in psychology from California State University, Fullerton and have worked on titles such as "The Gathering Storm" and "Heartthrob".
68
+ # """)
69
+ # result = check_score(context, sentences)
70
+ # print(result)
71
+ # result = """
72
+
73
+ answer = run(query='Tell me something about Gaël Varoquaux, e.g., birth date and place and short bio ', sample_size=5)
74
+ print(answer)
cpu_llama_generate.py CHANGED
@@ -8,6 +8,9 @@ device = 'cpu'
8
  llm = AutoModelForCausalLM.from_pretrained("TheBloke/Llama-2-7b-Chat-GGUF", model_file="llama-2-7b-chat.Q4_K_M.gguf", model_type="llama", gpu_layers=0)
9
 
10
 
11
- def run(query):
12
- return llm(query)
 
 
 
13
 
 
8
  llm = AutoModelForCausalLM.from_pretrained("TheBloke/Llama-2-7b-Chat-GGUF", model_file="llama-2-7b-chat.Q4_K_M.gguf", model_type="llama", gpu_layers=0)
9
 
10
 
11
+ def generate_answer(query, sample_num):
12
+ results = list()
13
+ for _ in range(sample_num):
14
+ results.append(llm(query))
15
+ return results
16