Hackavist commited on
Commit
030eb32
1 Parent(s): 9d79da1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -25
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import re
2
  import streamlit as st
3
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
4
 
5
  # Initialize the chat history
6
  history = []
@@ -8,38 +8,30 @@ history = []
8
  def clean_text(text):
9
  return re.sub('[^a-zA-Z\s]', '', text).strip()
10
 
11
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
12
- model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/DialoGPT-small").half().cuda()
13
 
14
  def generate_response(user_input):
 
15
  history.append((user_input, ""))
16
 
17
  if not history:
18
  return ""
19
 
20
  last_user_message = history[-1][0]
21
- combined_messages = " ".join([msg for msg, _ in reversed(history[:-1])]) + " User: " + last_user_message
22
-
23
- tokens = tokenizer.encode(combined_messages, add_special_tokens=True, max_length=4096, truncation=True)
24
- tokens = tokens[:1024]
25
- segment_ids = [0]*len(tokens)
26
- input_ids = torch.tensor([tokens], dtype=torch.long).cuda()
27
-
28
- with torch.no_grad():
29
- outputs = model.generate(
30
- input_ids,
31
- max_length=1024,
32
- min_length=20,
33
- length_penalty=2.0,
34
- early_stopping=True,
35
- num_beams=4,
36
- bad_words_callback=[lambda x: True if 'User:' in str(x) else False]
37
- )
38
- output = output[0].tolist()[len(tokens)-1:]
39
- decoded_output = tokenizer.decode(output, skip_special_tokens=True)
40
-
41
- history[-1] = (last_user_message, decoded_output)
42
- return f"AI: {decoded_output}".capitalize()
43
 
44
  st.title("Simple Chat App using DistilBert Model (HuggingFace & Streamlit)")
45
 
 
1
  import re
2
  import streamlit as st
3
+ from transformers import pipeline
4
 
5
  # Initialize the chat history
6
  history = []
 
8
  def clean_text(text):
9
  return re.sub('[^a-zA-Z\s]', '', text).strip()
10
 
11
+ # Load DistilBert model
12
+ model = pipeline("question-answering", model="distilbert-base-cased")
13
 
14
  def generate_response(user_input):
15
+ # Add user input to history
16
  history.append((user_input, ""))
17
 
18
  if not history:
19
  return ""
20
 
21
  last_user_message = history[-1][0]
22
+ user_input = clean_text(last_user_message)
23
+
24
+ if len(user_input) > 0:
25
+ # Modify the context value below with appropriate content
26
+ #Replace this line with useful information for generating proper responses.
27
+ context = """The Phoenix pay system is a payroll processing system for Canadian federal government employees, provided by IBM in June 2011 using PeopleSoft software, and run by Public Services and Procurement Canada. The Public Service Pay Centre is located in Miramichi, New Brunswick. It was first introduced in 2009 as part of Prime Minister Stephen Harper's Transformation of Pay Administration Initiative, intended to replace Canada's 40-year old system with a new, cost-saving "automated, off-the-shelf commercial system."
28
+ By July 2018, Phoenix has caused pay problems to close to 80 percent of the federal government's 290,000 public servants through underpayments, over-payments, and non-payments.[1] The Standing Senate Committee on National Finance, chaired by Senator Percy Mockler, sought to examine the causes for the failure, holding "eight meetings with 28 witnesses, including the Auditor General of Canada, union representatives, departments and agencies, officials from IBM, the Minister of Public Services and Procurement and the Clerk of the Privy Council"[1] and paid a visit to the Miramichi pay system location during their investigation. Their report, "The Phoenix Pay Problem: Working Towards a Solution" on July 31, 2018, in which they called Phoenix a failure and an "international embarrassment".[1] Instead of saving $70 million a year as planned, the report said that the cost to taxpayers to fix Phoenix's problems could reach a total of $2.2 billion by 2023. The Office of the Auditor General of Canada also performed an independent audit, and published a report in 2018 that concluded that the Phoenix project “was a incomprehensible failure of project management and oversight”,[2] and that Phoenix Executives did not heed warnings from the Miramichi Pay Centre, costing the federal government hundreds of millions of dollars, and had a negative financial impact on tens of thousands of its employees. """
29
+
30
+ result = model(question=user_input, context=context)
31
+ answer = result['answer']
32
+ history[-1] = (last_user_message, answer)
33
+
34
+ return f"AI: {answer}"
 
 
 
 
 
 
 
 
 
35
 
36
  st.title("Simple Chat App using DistilBert Model (HuggingFace & Streamlit)")
37