Colby commited on
Commit
03c5aaf
·
verified ·
1 Parent(s): 54033c4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -70
app.py CHANGED
@@ -29,74 +29,13 @@ wiki_wiki = wikipediaapi.Wikipedia('Organika ([email protected])', 'en')
29
 
30
  # Use a pipeline as a high-level helper
31
  from transformers import pipeline
32
- topic_model = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-9")
33
- #model = pipeline("text-generation", model="Colby/StarCoder-3B-WoW-JSON", device=0)
34
-
35
- import requests
36
-
37
- # function for Huggingface API calls
38
- def query(payload, model_path, headers):
39
- API_URL = "https://api-inference.huggingface.co/models/" + model_path
40
- for retry in range(3):
41
- response = requests.post(API_URL, headers=headers, json=payload)
42
- if response.status_code == requests.codes.ok:
43
- try:
44
- results = response.json()
45
- return results
46
- except:
47
- print('Invalid response received from server')
48
- print(response)
49
- return None
50
- else:
51
- # Not connected to internet maybe?
52
- if response.status_code==404:
53
- print('Are you connected to the internet?')
54
- print('URL attempted = '+API_URL)
55
- break
56
- if response.status_code==503:
57
- print(response.json())
58
- continue
59
- if response.status_code==504:
60
- print('504 Gateway Timeout')
61
- else:
62
- print('Unsuccessful request, status code '+ str(response.status_code))
63
- # print(response.json()) #debug only
64
- print(payload)
65
-
66
- def generate_text(prompt, model_path, text_generation_parameters, headers):
67
- start_time = time.time()
68
- options = {'use_cache': False, 'wait_for_model': True}
69
- payload = {"inputs": prompt, "parameters": text_generation_parameters, "options": options}
70
- output_list = query(payload, model_path, headers)
71
- if not output_list:
72
- print('Generation failed')
73
- end_time = time.time()
74
- duration = round(end_time - start_time, 1)
75
- stringlist = []
76
- if output_list and 'generated_text' in output_list[0].keys():
77
- print(f'{len(output_list)} sample(s) of text generated in {duration} seconds.')
78
- for gendict in output_list:
79
- stringlist.append(gendict['generated_text'])
80
- else:
81
- print(output_list)
82
- return(stringlist)
83
-
84
- model_path = "Colby/StarCoder-1B-WoW-JSON"
85
- parameters = {
86
- "max_new_tokens": 250,
87
- "return_full_text": False,
88
- "do_sample": True,
89
- "temperature": 0.8,
90
- "top_p": 0.9,
91
- "top_k": 50,
92
- "repetition_penalty": 1.1
93
- }
94
- headers = {"Authorization": "Bearer " + os.environ['HF_TOKEN']}
95
 
96
  def merlin_chat(message, history):
97
  chat_text = ""
98
  chat_list = []
99
- for turn in history:
100
  chat_text += f"{turn[0]}\n\n{turn[1]}\n\n"
101
  chat_list.append({"role": "user", "content": turn[0]})
102
  chat_list.append({"role": "assistant", "content": turn[1]})
@@ -111,9 +50,12 @@ def merlin_chat(message, history):
111
  continue
112
  if ent.text in ents_found:
113
  continue
114
- ents_found.append(ent.text.title())
115
  r.extract_keywords_from_text(chat_text)
116
- ents_found = ents_found + r.get_ranked_phrases()[:3]
 
 
 
117
  context = ""
118
  scores = topic_model(chat_text, ents_found, multi_label=True)['scores']
119
  if ents_found:
@@ -135,6 +77,8 @@ def merlin_chat(message, history):
135
  continue
136
  else:
137
  context += entsum + '\n\n'
 
 
138
  system_msg = {
139
  'role': 'system', 'content': context
140
  }
@@ -142,10 +86,12 @@ def merlin_chat(message, history):
142
  user_msg = {'role': 'user', 'content': message}
143
  chat_list.append(user_msg)
144
  prompt = json.dumps(chat_list)[:-1] + ",{\"role\": \"assistant\", \"content\": \""
 
145
  for attempt in range(3):
146
- result = generate_text(prompt, model_path, parameters, headers)
147
- response = result[0]
148
- print(response) # so we can see it in logs
 
149
  start = 0
150
  end = 0
151
  cleanStr = response.lstrip()
@@ -161,7 +107,10 @@ def merlin_chat(message, history):
161
  message = messages[-1]
162
  if message['role'] != 'assistant':
163
  continue
 
 
 
164
  return message['content']
165
  return "🤔"
166
 
167
- gr.ChatInterface(merlin_chat).launch()
 
29
 
30
  # Use a pipeline as a high-level helper
31
  from transformers import pipeline
32
+ topic_model = pipeline("zero-shot-classification", model="valhalla/distilbart-mnli-12-9", device=0)
33
+ model = pipeline("text-generation", model="Colby/StarCoder-1B-WoW-JSON", device=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def merlin_chat(message, history):
36
  chat_text = ""
37
  chat_list = []
38
+ for turn in history[-3:]:
39
  chat_text += f"{turn[0]}\n\n{turn[1]}\n\n"
40
  chat_list.append({"role": "user", "content": turn[0]})
41
  chat_list.append({"role": "assistant", "content": turn[1]})
 
50
  continue
51
  if ent.text in ents_found:
52
  continue
53
+ ents_found.append(ent.text.title().lower())
54
  r.extract_keywords_from_text(chat_text)
55
+ for phrase in r.get_ranked_phrases()[:3]:
56
+ phrase = phrase.lower()
57
+ if phrase not in ents_found:
58
+ ents_found.append(phrase)
59
  context = ""
60
  scores = topic_model(chat_text, ents_found, multi_label=True)['scores']
61
  if ents_found:
 
77
  continue
78
  else:
79
  context += entsum + '\n\n'
80
+ else:
81
+ print("not found.")
82
  system_msg = {
83
  'role': 'system', 'content': context
84
  }
 
86
  user_msg = {'role': 'user', 'content': message}
87
  chat_list.append(user_msg)
88
  prompt = json.dumps(chat_list)[:-1] + ",{\"role\": \"assistant\", \"content\": \""
89
+ print(f"PROMPT: {prompt}")
90
  for attempt in range(3):
91
+ #result = generate_text(prompt, model_path, parameters, headers)
92
+ result = model(prompt,return_full_text=False, max_new_tokens=256, temperature=0.8, repetition_penalty=1.1)
93
+ response = result[0]['generated_text']
94
+ print(f"COMPLETION: {response}") # so we can see it in logs
95
  start = 0
96
  end = 0
97
  cleanStr = response.lstrip()
 
107
  message = messages[-1]
108
  if message['role'] != 'assistant':
109
  continue
110
+ msg_text = message['content']
111
+ if chat_text.find(msg_text) >= 0:
112
+ continue
113
  return message['content']
114
  return "🤔"
115
 
116
+ gr.ChatInterface(merlin_chat).launch(share=True)