tangzhy commited on
Commit
a3f1767
1 Parent(s): bda33ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -20,6 +20,15 @@ DESCRIPTION = """\
20
  Hello! I'm ORLM-LLaMA-3-8B, here to automate your optimization modeling tasks! Check our [repo](https://github.com/Cardinal-Operations/ORLM) and [paper](https://arxiv.org/abs/2405.17743)!
21
  """
22
 
 
 
 
 
 
 
 
 
 
23
  MAX_MAX_NEW_TOKENS = 4096
24
  DEFAULT_MAX_NEW_TOKENS = 4096
25
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
@@ -29,21 +38,21 @@ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
29
  # bnb_4bit_compute_dtype=torch.bfloat16,
30
  # bnb_4bit_use_double_quant=True,
31
  # bnb_4bit_quant_type= "nf4")
32
- quantization_config = BitsAndBytesConfig(load_in_8bit=True)
33
 
34
  model_id = "CardinalOperations/ORLM-LLaMA-3-8B"
35
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
36
  model = AutoModelForCausalLM.from_pretrained(
37
  model_id,
38
  device_map="auto",
39
- # torch_dtype=torch.bfloat16,
40
- # attn_implementation="flash_attention_2",
41
- quantization_config=quantization_config,
42
  )
43
  model.eval()
44
 
45
 
46
- @spaces.GPU(duration=120)
47
  def generate(
48
  message: str,
49
  chat_history: list[tuple[str, str]],
@@ -56,11 +65,12 @@ def generate(
56
  if chat_history != []:
57
  return "Sorry, I am an instruction-tuned model and currently do not support chatting. Please try clearing the chat history or refreshing the page to ask a new question."
58
 
59
- tokenized_example = tokenizer(message, return_tensors='pt', max_length=MAX_INPUT_TOKEN_LENGTH, truncation=True)
 
60
  input_ids = tokenized_example.input_ids
61
  input_ids = input_ids.to(model.device)
62
 
63
- streamer = TextIteratorStreamer(tokenizer, timeout=50.0, skip_prompt=True, skip_special_tokens=True)
64
  generate_kwargs = dict(
65
  {"input_ids": input_ids},
66
  streamer=streamer,
@@ -80,7 +90,7 @@ def generate(
80
  for text in streamer:
81
  outputs.append(text)
82
  yield "".join(outputs)
83
-
84
  # outputs.append("\n\nI have now attempted to solve the optimization modeling task! Please try executing the code in your environment, making sure it is equipped with `coptpy`.")
85
  # yield "".join(outputs)
86
 
@@ -126,9 +136,9 @@ chat_interface = gr.ChatInterface(
126
  ],
127
  stop_btn=None,
128
  examples=[
129
- ["Below is an operations research question. Build a mathematical model and corresponding python code using `coptpy` that appropriately addresses the question.\n\n# Question:\nA lab has 1000 units of medicinal ingredients to make two pills, a large pill and a small pill. A large pill requires 3 units of medicinal ingredients and 2 units of filler. A small pill requires 2 units of medicinal ingredients and 1 unit of filler. The lab has to make at least 100 large pills. However, since small pills are more popular at least 60% of the total number of pills must be small. How many of each should be made to minimize the total number of filler material needed?\n\n# Response:"],
130
- ["Below is an operations research question. Build a mathematical model and corresponding python code using `coptpy` that appropriately addresses the question.\n\n# Question:\nLet's say you're on a mission to create the perfect meal plan for yourself. You're focused on getting the right balance of nutrients without digging too deep into your pockets. You have six different food options to consider: Chicken, Oats, Tofu, Rice, Beef, and Salmon. Each of these foods provides a certain amount of protein, carbohydrates, and calories, and they all come with their own distinct prices.\n\nHere's a detailed breakdown of the nutritional content and cost of each food item:\n\n- Chicken: Delivers 14 grams of protein, a single gram of carbohydrates, and 164 calories at a cost of $6.\n- Oats: Gives you 11 grams of protein, 6 grams of carbohydrates, and 210 calories for just $2.\n- Tofu: Offers 8 grams of protein, 12 grams of carbohydrates, and 98 calories at a cost of $9.\n- Rice: Provides 1 gram of protein, a generous 17 grams of carbohydrates, and 92 calories for $3.\n- Beef: Comes packed with 16 grams of protein, 11 grams of carbohydrates, and 211 calories, priced at $7.\n- Salmon: Brings a hefty 19 grams of protein, 13 grams of carbohydrates, and 211 calories but costs $9.\n\nYou want to make sure your meal plan meets the following nutritional targets: at least 70 grams of protein, 117 grams of carbohydrates, and 1837 calories. Considering these six foods, what is the least amount of money you need to spend to meet these dietary needs?\nRemember, your response should only contain the optimal value of the cost to meet the requirements.\n\n# Response:"],
131
- ["Below is an operations research question. Build a mathematical model and corresponding python code using `coptpy` that appropriately addresses the question.\n\n# Question:\nHaus Toys can manufacture and sell toy trucks, toy planes, toy boats, and toy trains. The profit from selling one truck is $5, from one plane is $10, from one boat is $8, and from one train is $7. How many types of toys should Haus Toys manufacture to maximize profit?\n\nThere are 890 units of wood available. Manufacturing one truck requires 12 units of wood, one plane requires 20 units of wood, one boat requires 15 units of wood, and one train requires 10 units of wood.\n\nThere are 500 units of steel available. Manufacturing one plane requires 3 units of steel, one boat requires 5 units of steel, one train requires 4 units of steel, and one truck requires 6 units of steel.\n\nIf Haus Toys manufactures trucks, then they will not manufacture trains.\n\nHowever, if they manufacture boats, they will also manufacture planes.\n\nThe number of toy boats manufactured cannot exceed the number of toy trains manufactured.\n\n# Response:"],
132
  ],
133
  )
134
 
 
20
  Hello! I'm ORLM-LLaMA-3-8B, here to automate your optimization modeling tasks! Check our [repo](https://github.com/Cardinal-Operations/ORLM) and [paper](https://arxiv.org/abs/2405.17743)!
21
  """
22
 
23
+ PROMPT_TEMPLATE = """
24
+ Below is an operations research question. Build a mathematical model and corresponding python code using `coptpy` that appropriately addresses the question.
25
+
26
+ # Question:
27
+ {Question}
28
+
29
+ # Response:
30
+ """
31
+
32
  MAX_MAX_NEW_TOKENS = 4096
33
  DEFAULT_MAX_NEW_TOKENS = 4096
34
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
 
38
  # bnb_4bit_compute_dtype=torch.bfloat16,
39
  # bnb_4bit_use_double_quant=True,
40
  # bnb_4bit_quant_type= "nf4")
41
+ # quantization_config = BitsAndBytesConfig(load_in_8bit=True)
42
 
43
  model_id = "CardinalOperations/ORLM-LLaMA-3-8B"
44
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
45
  model = AutoModelForCausalLM.from_pretrained(
46
  model_id,
47
  device_map="auto",
48
+ torch_dtype=torch.bfloat16,
49
+ attn_implementation="flash_attention_2",
50
+ # quantization_config=quantization_config,
51
  )
52
  model.eval()
53
 
54
 
55
+ @spaces.GPU(duration=60)
56
  def generate(
57
  message: str,
58
  chat_history: list[tuple[str, str]],
 
65
  if chat_history != []:
66
  return "Sorry, I am an instruction-tuned model and currently do not support chatting. Please try clearing the chat history or refreshing the page to ask a new question."
67
 
68
+ prompt = PROMPT_TEMPLATE.replace("{Question}", message).strip()
69
+ tokenized_example = tokenizer(prompt, return_tensors='pt', max_length=MAX_INPUT_TOKEN_LENGTH, truncation=True)
70
  input_ids = tokenized_example.input_ids
71
  input_ids = input_ids.to(model.device)
72
 
73
+ streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
74
  generate_kwargs = dict(
75
  {"input_ids": input_ids},
76
  streamer=streamer,
 
90
  for text in streamer:
91
  outputs.append(text)
92
  yield "".join(outputs)
93
+
94
  # outputs.append("\n\nI have now attempted to solve the optimization modeling task! Please try executing the code in your environment, making sure it is equipped with `coptpy`.")
95
  # yield "".join(outputs)
96
 
 
136
  ],
137
  stop_btn=None,
138
  examples=[
139
+ ["A lab has 1000 units of medicinal ingredients to make two pills, a large pill and a small pill. A large pill requires 3 units of medicinal ingredients and 2 units of filler. A small pill requires 2 units of medicinal ingredients and 1 unit of filler. The lab has to make at least 100 large pills. However, since small pills are more popular at least 60% of the total number of pills must be small. How many of each should be made to minimize the total number of filler material needed?"],
140
+ ["Let's say you're on a mission to create the perfect meal plan for yourself. You're focused on getting the right balance of nutrients without digging too deep into your pockets. You have six different food options to consider: Chicken, Oats, Tofu, Rice, Beef, and Salmon. Each of these foods provides a certain amount of protein, carbohydrates, and calories, and they all come with their own distinct prices.\n\nHere's a detailed breakdown of the nutritional content and cost of each food item:\n\n- Chicken: Delivers 14 grams of protein, a single gram of carbohydrates, and 164 calories at a cost of $6.\n- Oats: Gives you 11 grams of protein, 6 grams of carbohydrates, and 210 calories for just $2.\n- Tofu: Offers 8 grams of protein, 12 grams of carbohydrates, and 98 calories at a cost of $9.\n- Rice: Provides 1 gram of protein, a generous 17 grams of carbohydrates, and 92 calories for $3.\n- Beef: Comes packed with 16 grams of protein, 11 grams of carbohydrates, and 211 calories, priced at $7.\n- Salmon: Brings a hefty 19 grams of protein, 13 grams of carbohydrates, and 211 calories but costs $9.\n\nYou want to make sure your meal plan meets the following nutritional targets: at least 70 grams of protein, 117 grams of carbohydrates, and 1837 calories. Considering these six foods, what is the least amount of money you need to spend to meet these dietary needs?\nRemember, your response should only contain the optimal value of the cost to meet the requirements."],
141
+ ["Haus Toys can manufacture and sell toy trucks, toy planes, toy boats, and toy trains. The profit from selling one truck is $5, from one plane is $10, from one boat is $8, and from one train is $7. How many types of toys should Haus Toys manufacture to maximize profit?\n\nThere are 890 units of wood available. Manufacturing one truck requires 12 units of wood, one plane requires 20 units of wood, one boat requires 15 units of wood, and one train requires 10 units of wood.\n\nThere are 500 units of steel available. Manufacturing one plane requires 3 units of steel, one boat requires 5 units of steel, one train requires 4 units of steel, and one truck requires 6 units of steel.\n\nIf Haus Toys manufactures trucks, then they will not manufacture trains.\n\nHowever, if they manufacture boats, they will also manufacture planes.\n\nThe number of toy boats manufactured cannot exceed the number of toy trains manufactured."],
142
  ],
143
  )
144