Lillian McAboy commited on
Commit
12bf2a0
·
2 Parent(s): ac6aabdb42f41a

updated from benny

Browse files
Files changed (1) hide show
  1. app.py +32 -43
app.py CHANGED
@@ -1,8 +1,14 @@
 
 
 
 
 
1
  import streamlit as st
2
- import torch, os
3
- from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
 
4
 
5
- model = 'google/gemma-1.1-7b-it'
6
 
7
  formal_options = [
8
  'More aggressive',
@@ -19,44 +25,29 @@ formal_options = [
19
  'Like Eric Cartman'
20
  ]
21
 
22
- formality_name = st.selectbox("Transform my text to be: ", formal_options)
23
-
24
- @st.cache(allow_output_mutation=True)
25
- def get_tokenizer(model_name):
26
- from transformers import AutoTokenizer
27
- return AutoTokenizer.from_pretrained(model_name)
28
 
29
- @st.cache(allow_output_mutation=True)
30
- def get_model(model_name):
31
- import torch
32
- from transformers import AutoModelForCausalLM
33
- mymodel = AutoModelForCausalLM.from_pretrained(model, device_map='auto', torch_dtype=torch.bfloat16)
34
- print(f"Loaded model, {mymodel.num_parameters():,d} parameters.")
35
- return mymodel
36
- original_text = st.text_area("Original Document", "This is my text that I want to change.")
37
- new_text = st.text_area("New Document", help="Leave this blank, this will be updated with your new document.")
38
 
39
  def get_spans_local(formality_name, original_text, new_text):
40
- #import torch
41
-
42
- tokenizer = get_tokenizer(model)
43
- mymodel = get_model(model)
44
-
45
  messages = [
46
  {
47
  "role": "user",
48
  "content": f"{formality_name}\n\n{original_text}",
49
  },
50
  ]
51
-
52
  tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")[0]
53
-
54
  new_text_ids = tokenizer(new_text, return_tensors='pt')['input_ids'][0]
55
  joined_ids = torch.cat([tokenized_chat, new_text_ids[1:]])
56
-
57
  with torch.no_grad():
58
  logits = model(joined_ids[None].to(model.device)).logits[0].cpu()
59
-
60
  spans = []
61
  length_so_far = 0
62
  for idx in range(len(tokenized_chat), len(joined_ids)):
@@ -77,25 +68,23 @@ def get_spans_local(formality_name, original_text, new_text):
77
  return spans
78
 
79
  def summarize_text(text):
80
- input_ids = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True)
81
  summary_ids = model.generate(input_ids, max_length=150, num_beams=2, early_stopping=True)
82
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
83
  return summary
84
 
85
 
86
- def get_highlights_api(prompt, doc, updated_doc):
87
- # Make a request to the API. prompt and doc are query parameters:
88
- # https://tools.kenarnold.org/api/highlights?prompt=Rewrite%20this%20document&doc=This%20is%20a%20document
89
- # The response is a JSON array
90
- import requests
91
- response = requests.get("https://tools.kenarnold.org/api/highlights", params=dict(prompt=prompt, doc=doc, updated_doc=updated_doc))
92
- return response.json()['highlights']
 
 
 
 
93
 
94
- if __name__ == "__main__":
95
- spans = get_spans_local(formality_name, original_text, new_text)
96
- st.write(spans)
97
-
98
- if st.button("Summarize My Document"):
99
- summary = summarize_text(original_text)
100
- summary_text = summary
101
- st.write(summary_text)
 
1
+
2
+ ​
3
+ Lillian McAboy
4
+ ​
5
+
6
  import streamlit as st
7
+ import torch
8
+ from transformers import AutoModelForCausalLM
9
+ from transformers import AutoTokenizer
10
 
11
+ model_id = 'google/gemma-2b'
12
 
13
  formal_options = [
14
  'More aggressive',
 
25
  'Like Eric Cartman'
26
  ]
27
 
28
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
29
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.bfloat16)
 
 
 
 
30
 
31
+ formality_name = st.selectbox("Transform my text to be: ", formal_options)
32
+ original_text = st.text_area("Input", "This is my text that I want to change.")
33
+ new_text = st.text_area("Output", key="output", help="Leave this blank, this will be updated with your new document.")
 
 
 
 
 
 
34
 
35
  def get_spans_local(formality_name, original_text, new_text):
 
 
 
 
 
36
  messages = [
37
  {
38
  "role": "user",
39
  "content": f"{formality_name}\n\n{original_text}",
40
  },
41
  ]
42
+
43
  tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")[0]
44
+
45
  new_text_ids = tokenizer(new_text, return_tensors='pt')['input_ids'][0]
46
  joined_ids = torch.cat([tokenized_chat, new_text_ids[1:]])
47
+
48
  with torch.no_grad():
49
  logits = model(joined_ids[None].to(model.device)).logits[0].cpu()
50
+
51
  spans = []
52
  length_so_far = 0
53
  for idx in range(len(tokenized_chat), len(joined_ids)):
 
68
  return spans
69
 
70
  def summarize_text(text):
71
+ input_ids = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True).to("cuda")
72
  summary_ids = model.generate(input_ids, max_length=150, num_beams=2, early_stopping=True)
73
  summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
74
  return summary
75
 
76
 
77
+ # def get_highlights_api(prompt, doc, updated_doc):
78
+ # # Make a request to the API. prompt and doc are query parameters:
79
+ # # https://tools.kenarnold.org/api/highlights?prompt=Rewrite%20this%20document&doc=This%20is%20a%20document
80
+ # # The response is a JSON array
81
+ # import requests
82
+ # response = requests.get("https://tools.kenarnold.org/api/highlights", params=dict(prompt=prompt, doc=doc, updated_doc=updated_doc))
83
+ # return response.json()['highlights']
84
+
85
+
86
+ # # spans = get_spans_local(formality_name, original_text, new_text)
87
+ # # st.write(spans)
88
 
89
+ if st.button("Summarize My Document"):
90
+ new_text = st.text_area("Output", value=summarize_text(original_text), key="output", help="Leave this blank, this will be updated with your new document.")