tmnam20 commited on
Commit
55ae524
1 Parent(s): 3b62d42
Files changed (2) hide show
  1. app.py +11 -61
  2. st_utils.py +11 -7
app.py CHANGED
@@ -12,15 +12,15 @@ import torch
12
  # list_files(os.getcwd())
13
 
14
  # Set the title and description of the app
15
- st.title("Text Summarization App")
16
  st.write(
17
  """
18
  This app uses the Hugging Face transformers library to generate summaries of input text.
19
- Simply select one of the sample Python functions from the dropdown menu below, and click the 'Summarize' button to generate a summary.
20
  """
21
  )
22
 
23
- st.write(f"Has CUDA: {torch.cuda.is_available()}")
24
 
25
  # Download the model from the Hugging Face Hub if it doesn't exist
26
  download_model()
@@ -36,14 +36,13 @@ values = [
36
  "def search(data, target):\n for i in range(len(data)):\n if data[i] == target:\n return i\n return -1",
37
  ]
38
 
39
- st.subheader("Select a sample Python function:")
40
- selected_value = st.selectbox("", values)
41
 
42
  # Create a text input area for the user to enter their text
43
  text_input = st.text_area(
44
- "Or enter your Python function here:",
45
- height=300,
46
- value=values[0],
47
  )
48
 
49
 
@@ -56,63 +55,14 @@ def generate_summary(text):
56
  # When the user clicks the 'Summarize' button, generate a summary
57
  if st.button("Summarize") and (len(selected_value) > 0 or len(text_input) > 0):
58
  with st.spinner("Generating summary..."):
59
- if len(selected_value) > 0:
60
- summaries = generate_summary(selected_value)
61
  st.subheader("Docstrings:")
62
  for i, summary in enumerate(summaries):
63
  st.write(f"{i + 1}. " + summary)
 
64
  else:
65
- summaries = generate_summary(text_input)
66
  st.subheader("Docstrings:")
67
  for i, summary in enumerate(summaries):
68
  st.write(f"{i + 1}. " + summary)
69
-
70
-
71
- # import streamlit as st
72
- # from st_utils import load_tokenizer_and_model, generate_docstring, download_model
73
-
74
- # # Download the model from the Hugging Face Hub if it doesn't exist
75
-
76
-
77
- # # Set the title and description of the app
78
- # st.title("Text Summarization App")
79
- # st.write(
80
- # """
81
- # This app uses the Hugging Face transformers library to generate summaries of input text.
82
- # Simply enter your text in the input area below, and click the 'Summarize' button to generate a summary.
83
- # """
84
- # )
85
-
86
- # tokenizer, model, device = load_tokenizer_and_model("./models/pytorch_model.bin")
87
-
88
- # # Create a text input area for the user to enter their text
89
- # values = [
90
- # "def multiply(a, b):\n return a * b",
91
- # "def get_data():\n data = []\n for i in range(10):\n data.append(i)\n return data",
92
- # "def search(data, target):\n for i in range(len(data)):\n if data[i] == target:\n return i\n return -1",
93
- # ]
94
-
95
- # st.subheader("Enter your Python function here:")
96
- # text_input = st.text_area(
97
- # "Input text here...",
98
- # height=300,
99
- # value=values[2],
100
- # )
101
-
102
-
103
- # # Define a function to generate a summary
104
- # def generate_summary(text):
105
- # summary = generate_docstring(model, tokenizer, device, text, max_length=30)
106
- # return summary
107
-
108
-
109
- # # When the user clicks the 'Summarize' button, generate a summary
110
- # if st.button("Summarize") and len(text_input) > 0:
111
- # with st.spinner("Generating summary..."):
112
- # # summary = generate_summary(text_input)
113
- # # st.write("Summary:")
114
- # # st.code(summary, language="text")
115
- # summaries = generate_summary(text_input)
116
- # st.subheader("Summary:")
117
- # for i, summary in enumerate(summaries):
118
- # st.write(f"{i + 1}. " + summary)
 
12
  # list_files(os.getcwd())
13
 
14
  # Set the title and description of the app
15
+ st.title("Code Function Summarization App")
16
  st.write(
17
  """
18
  This app uses the Hugging Face transformers library to generate summaries of input text.
19
+ Simply select one of the sample Python functions from the dropdown menu below, and click the 'Summarize' button to generate a summary for the corresponding function.
20
  """
21
  )
22
 
23
+ # st.write(f"Has CUDA: {torch.cuda.is_available()}")
24
 
25
  # Download the model from the Hugging Face Hub if it doesn't exist
26
  download_model()
 
36
  "def search(data, target):\n for i in range(len(data)):\n if data[i] == target:\n return i\n return -1",
37
  ]
38
 
39
+ selected_value = st.selectbox("Select a sample Python function:", values)
 
40
 
41
  # Create a text input area for the user to enter their text
42
  text_input = st.text_area(
43
+ "Or enter your Python function here (prioritize this over the dropdown menu):",
44
+ height=256,
45
+ value=selected_value,
46
  )
47
 
48
 
 
55
  # When the user clicks the 'Summarize' button, generate a summary
56
  if st.button("Summarize") and (len(selected_value) > 0 or len(text_input) > 0):
57
  with st.spinner("Generating summary..."):
58
+ if len(text_input) > 0:
59
+ summaries = generate_summary(text_input)
60
  st.subheader("Docstrings:")
61
  for i, summary in enumerate(summaries):
62
  st.write(f"{i + 1}. " + summary)
63
+ # if len(selected_value) > 0:
64
  else:
65
+ summaries = generate_summary(selected_value)
66
  st.subheader("Docstrings:")
67
  for i, summary in enumerate(summaries):
68
  st.write(f"{i + 1}. " + summary)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
st_utils.py CHANGED
@@ -93,7 +93,7 @@ class CONFIG:
93
 
94
 
95
  # download model with streamlit cache decorator
96
- @st.cache(persist=False, show_spinner=True, allow_output_mutation=True)
97
  def download_model():
98
  if not os.path.exists(r"models/pytorch_model.bin"):
99
  os.makedirs("./models", exist_ok=True)
@@ -108,7 +108,8 @@ def download_model():
108
 
109
 
110
  # load with streamlit cache decorator
111
- @st.cache(persist=False, show_spinner=True, allow_output_mutation=True)
 
112
  def load_tokenizer_and_model(pretrained_path):
113
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
114
 
@@ -122,7 +123,7 @@ def load_tokenizer_and_model(pretrained_path):
122
  CONFIG.config_name if CONFIG.config_name else CONFIG.model_name_or_path,
123
  cache_dir=CONFIG.cache_dir,
124
  )
125
- model_config.save_pretrained("config")
126
 
127
  # load tokenizer
128
  tokenizer = tokenizer_class.from_pretrained(
@@ -159,28 +160,31 @@ def load_tokenizer_and_model(pretrained_path):
159
  map_location=device,
160
  )
161
  except RuntimeError as e:
 
162
  try:
163
  state_dict = torch.load(
164
  os.path.join(os.getcwd(), "models", "pytorch_model.bin"),
165
  map_location="cpu",
166
  )
167
  except RuntimeError as e:
 
168
  state_dict = torch.load(
169
  os.path.join(os.getcwd(), "models", "pytorch_model_cpu.bin"),
170
  map_location="cpu",
171
  )
 
 
172
  model.load_state_dict(state_dict)
173
 
174
- model = model.to("cpu")
175
- torch.save(
176
- model.state_dict(), os.path.join(os.getcwd(), "models", "pytorch_model_cpu.bin")
177
- )
178
 
179
  model = model.to(device)
180
 
181
  return tokenizer, model, device
182
 
183
 
 
184
  def preprocessing(code_segment):
185
  # remove newlines
186
  code_segment = re.sub(r"\n", " ", code_segment)
 
93
 
94
 
95
  # download model with streamlit cache decorator
96
+ @st.cache_resource
97
  def download_model():
98
  if not os.path.exists(r"models/pytorch_model.bin"):
99
  os.makedirs("./models", exist_ok=True)
 
108
 
109
 
110
  # load with streamlit cache decorator
111
+ # @st.cache(persist=False, show_spinner=True, allow_output_mutation=True)
112
+ @st.cache_resource
113
  def load_tokenizer_and_model(pretrained_path):
114
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
115
 
 
123
  CONFIG.config_name if CONFIG.config_name else CONFIG.model_name_or_path,
124
  cache_dir=CONFIG.cache_dir,
125
  )
126
+ # model_config.save_pretrained("config")
127
 
128
  # load tokenizer
129
  tokenizer = tokenizer_class.from_pretrained(
 
160
  map_location=device,
161
  )
162
  except RuntimeError as e:
163
+ print(e)
164
  try:
165
  state_dict = torch.load(
166
  os.path.join(os.getcwd(), "models", "pytorch_model.bin"),
167
  map_location="cpu",
168
  )
169
  except RuntimeError as e:
170
+ print(e)
171
  state_dict = torch.load(
172
  os.path.join(os.getcwd(), "models", "pytorch_model_cpu.bin"),
173
  map_location="cpu",
174
  )
175
+
176
+ del state_dict["encoder.embeddings.position_ids"]
177
  model.load_state_dict(state_dict)
178
 
179
+ # model = model.to("cpu")
180
+ # torch.save(model.state_dict(), os.path.join(os.getcwd(), "models", "pytorch_model_cpu.bin"))
 
 
181
 
182
  model = model.to(device)
183
 
184
  return tokenizer, model, device
185
 
186
 
187
+ @st.cache_data
188
  def preprocessing(code_segment):
189
  # remove newlines
190
  code_segment = re.sub(r"\n", " ", code_segment)