ceiteach commited on
Commit
0d3ef1c
1 Parent(s): 2d45ec0

Upload 2 files

Browse files
Files changed (2) hide show
  1. handler.py +6 -25
  2. requirements.txt +3 -6
handler.py CHANGED
@@ -1,43 +1,32 @@
1
- from unsloth import FastLanguageModel
2
  import torch
3
  import json
4
 
5
  class EndpointHandler():
6
  def __init__(self, path="ceiteach/chart-no-pretrain-llama31-unsloth"):
7
- max_seq_length = 4096
8
- dtype = None
9
- load_in_4bit = True
10
- model_name = "ceiteach/chart-no-pretrain-llama31-unsloth"
11
- model, tokenizer = FastLanguageModel.from_pretrained(
12
- model_name = model_name,
13
- max_seq_length = max_seq_length,
14
- dtype = dtype,
15
- load_in_4bit = load_in_4bit,
16
- )
17
  self.model = model
18
  self.tokenizer = tokenizer
19
 
20
  def __call__(self, data):
21
  model = self.model
22
  tokenizer = self.tokenizer
23
- FastLanguageModel.for_inference(model)
24
 
25
- date = "2024-08-18"
26
  previous_metric = "CPFORD"
27
  previous_chart_type = "line"
28
  instruction = """
29
  You are responding to an athlete who wants to view their data in a chart.
30
  Today's date is 2024-08-18 (YYYY-MM-DD).
31
-
32
  You have access to the following functions. Your response must use at least one of these functions.
33
-
34
  Functions: "
35
  - {"name":"draw","description":"Use this tool to add or update a dataset in the chart.","parameters":{"type":"OBJECT","description":"The parameters for the draw function.","properties":{"chartType":{"type":"STRING","description":"The type of chart to draw. It can be either 'line' or 'bar'. If unspecified use the previous chart type which will be provided in the prompt."},"metric":{"type":"STRING","description":"The metric to be charted. If unspecified use the previous metric which will be provided in the prompt."}},"required":["chartType","metric"]}}
36
  - {"name":"updateDateRange","description":"Use this tool to update the date range of the chart.","parameters":{"type":"OBJECT","description":"The parameters for the updateDateRange function.","properties":{"startDate":{"type":"STRING","description":"The start date of the chart. If unspecified use the previous start date which will be provided in the prompt."}},"required":["startDate"]}}
37
  - {"name":"removeDataset","description":"Use this tool to remove a dataset from the chart.","parameters":{"type":"OBJECT","description":"The parameters for the removeDataset function.","properties":{"metric":{"type":"STRING","description":"The metric to be removed. If unspecified use the previous metric which will be provided in the prompt."}},"required":["metric"]}}
38
  - {"name":"clearChart","description":"Use this tool to clear the chart.","parameters":{"type":"OBJECT","description":"The parameters for the clearChart function.","properties":{}}}
39
  "
40
-
41
  "Metrics" is a map of metric names to metric ids. The format is '<metric name>': '<metric id>'.
42
  Metrics: "
43
  'Accelerations': accelerationCount,
@@ -280,7 +269,6 @@ Metrics: "
280
  'Omega 3 Fatty Acids': O3FA,
281
  'Omega 3 Index': O3INDEX
282
  "
283
-
284
  Rules to follow: "
285
  - Use the "draw" tool to add or update a dataset in the chart.
286
  - Use the "updateDateRange" tool to update the date range of the chart.
@@ -296,7 +284,6 @@ Rules to follow: "
296
  - Only use the "removeDataset", "removeHighlight" or "clearChart" tools if the user has explicitly asked for it.
297
  - "removeDataset" will remove a particular dataset. 'clearChart' will completely clear the chart. If the user mentions an athlete or metric, you should assume they want to remove a dataset and not clear the chart.
298
  "
299
-
300
  If a new chart type or metric is not provided, use the previous values as we can assume the user wants to continue with the same options.
301
  - The previous chart type is line.
302
  - The previous metric is CPFORD.
@@ -304,16 +291,14 @@ If a new chart type or metric is not provided, use the previous values as we can
304
 
305
  user_input = data.pop("inputs", data)
306
  alpaca_prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
307
-
308
  ### Instruction:
309
  {instruction}
310
-
311
  ### Input:
312
  {user_input}
313
-
314
  ### Response:
315
  """
316
  prompt_tokenized = tokenizer(alpaca_prompt, return_tensors="pt").to("cuda")
 
317
  with torch.no_grad():
318
  response = tokenizer.decode(model.generate(**prompt_tokenized, max_new_tokens=128, temperature=0.01)[0], skip_special_tokens=True)
319
  response_content = response.split("### Response:")[1].strip()
@@ -323,7 +308,3 @@ If a new chart type or metric is not provided, use the previous values as we can
323
  except json.JSONDecodeError as e:
324
  print("Failed to parse function calls from response")
325
  return { "response": "Unable to generate response"}
326
-
327
-
328
-
329
-
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import torch
3
  import json
4
 
5
  class EndpointHandler():
6
  def __init__(self, path="ceiteach/chart-no-pretrain-llama31-unsloth"):
7
+ model_name = path
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype = torch.float16, device_map="auto")
 
 
 
 
 
 
 
10
  self.model = model
11
  self.tokenizer = tokenizer
12
 
13
  def __call__(self, data):
14
  model = self.model
15
  tokenizer = self.tokenizer
 
16
 
17
+ date = "2024-08-19"
18
  previous_metric = "CPFORD"
19
  previous_chart_type = "line"
20
  instruction = """
21
  You are responding to an athlete who wants to view their data in a chart.
22
  Today's date is 2024-08-18 (YYYY-MM-DD).
 
23
  You have access to the following functions. Your response must use at least one of these functions.
 
24
  Functions: "
25
  - {"name":"draw","description":"Use this tool to add or update a dataset in the chart.","parameters":{"type":"OBJECT","description":"The parameters for the draw function.","properties":{"chartType":{"type":"STRING","description":"The type of chart to draw. It can be either 'line' or 'bar'. If unspecified use the previous chart type which will be provided in the prompt."},"metric":{"type":"STRING","description":"The metric to be charted. If unspecified use the previous metric which will be provided in the prompt."}},"required":["chartType","metric"]}}
26
  - {"name":"updateDateRange","description":"Use this tool to update the date range of the chart.","parameters":{"type":"OBJECT","description":"The parameters for the updateDateRange function.","properties":{"startDate":{"type":"STRING","description":"The start date of the chart. If unspecified use the previous start date which will be provided in the prompt."}},"required":["startDate"]}}
27
  - {"name":"removeDataset","description":"Use this tool to remove a dataset from the chart.","parameters":{"type":"OBJECT","description":"The parameters for the removeDataset function.","properties":{"metric":{"type":"STRING","description":"The metric to be removed. If unspecified use the previous metric which will be provided in the prompt."}},"required":["metric"]}}
28
  - {"name":"clearChart","description":"Use this tool to clear the chart.","parameters":{"type":"OBJECT","description":"The parameters for the clearChart function.","properties":{}}}
29
  "
 
30
  "Metrics" is a map of metric names to metric ids. The format is '<metric name>': '<metric id>'.
31
  Metrics: "
32
  'Accelerations': accelerationCount,
 
269
  'Omega 3 Fatty Acids': O3FA,
270
  'Omega 3 Index': O3INDEX
271
  "
 
272
  Rules to follow: "
273
  - Use the "draw" tool to add or update a dataset in the chart.
274
  - Use the "updateDateRange" tool to update the date range of the chart.
 
284
  - Only use the "removeDataset", "removeHighlight" or "clearChart" tools if the user has explicitly asked for it.
285
  - "removeDataset" will remove a particular dataset. 'clearChart' will completely clear the chart. If the user mentions an athlete or metric, you should assume they want to remove a dataset and not clear the chart.
286
  "
 
287
  If a new chart type or metric is not provided, use the previous values as we can assume the user wants to continue with the same options.
288
  - The previous chart type is line.
289
  - The previous metric is CPFORD.
 
291
 
292
  user_input = data.pop("inputs", data)
293
  alpaca_prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
 
294
  ### Instruction:
295
  {instruction}
 
296
  ### Input:
297
  {user_input}
 
298
  ### Response:
299
  """
300
  prompt_tokenized = tokenizer(alpaca_prompt, return_tensors="pt").to("cuda")
301
+ model.eval()
302
  with torch.no_grad():
303
  response = tokenizer.decode(model.generate(**prompt_tokenized, max_new_tokens=128, temperature=0.01)[0], skip_special_tokens=True)
304
  response_content = response.split("### Response:")[1].strip()
 
308
  except json.JSONDecodeError as e:
309
  print("Failed to parse function calls from response")
310
  return { "response": "Unable to generate response"}
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,3 @@
1
- unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git
2
- xformers==0.0.26
3
- trl==0.8.6
4
- peft==0.12.0
5
- accelerate==0.32.1
6
- bitsandbytes==0.43.3
 
1
+ transformers==4.43.1
2
+ bitsandbytes
3
+ peft