tangzhy commited on
Commit
5ed473c
1 Parent(s): b7bc525

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -13,22 +13,18 @@ from transformers import (
13
  )
14
 
15
  DESCRIPTION = """\
16
- # Gemma 2 9B IT
17
 
18
- Gemma 2 is Google's latest iteration of open LLMs.
19
- This is a demo of [`google/gemma-2-9b-it`](https://huggingface.co/google/gemma-2-9b-it), fine-tuned for instruction following.
20
- For more details, please check [our post](https://huggingface.co/blog/gemma2).
21
-
22
- 👉 Looking for a larger and more powerful version? Try the 27B version in [HuggingChat](https://huggingface.co/chat/models/google/gemma-2-27b-it).
23
  """
24
 
25
- MAX_MAX_NEW_TOKENS = 2048
26
- DEFAULT_MAX_NEW_TOKENS = 1024
27
- MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
28
 
29
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
30
 
31
- model_id = "google/gemma-2-9b-it"
32
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
33
  model = AutoModelForCausalLM.from_pretrained(
34
  model_id,
@@ -38,6 +34,14 @@ model = AutoModelForCausalLM.from_pretrained(
38
  model.config.sliding_window = 4096
39
  model.eval()
40
 
 
 
 
 
 
 
 
 
41
 
42
  @spaces.GPU(duration=90)
43
  def generate(
@@ -49,20 +53,25 @@ def generate(
49
  top_k: int = 50,
50
  repetition_penalty: float = 1.2,
51
  ) -> Iterator[str]:
52
- conversation = []
53
- for user, assistant in chat_history:
54
- conversation.extend(
55
- [
56
- {"role": "user", "content": user},
57
- {"role": "assistant", "content": assistant},
58
- ]
59
- )
60
- conversation.append({"role": "user", "content": message})
61
-
62
- input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
63
- if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
64
- input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
65
- gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
 
 
 
 
 
66
  input_ids = input_ids.to(model.device)
67
 
68
  streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
@@ -127,17 +136,15 @@ chat_interface = gr.ChatInterface(
127
  ],
128
  stop_btn=None,
129
  examples=[
130
- ["Hello there! How are you doing?"],
131
- ["Can you explain briefly to me what is the Python programming language?"],
132
- ["Explain the plot of Cinderella in a sentence."],
133
- ["How many hours does it take a man to eat a Helicopter?"],
134
- ["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
135
  ],
136
  )
137
 
138
  with gr.Blocks(css="style.css", fill_height=True) as demo:
139
  gr.Markdown(DESCRIPTION)
140
- gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
141
  chat_interface.render()
142
 
143
  if __name__ == "__main__":
 
13
  )
14
 
15
  DESCRIPTION = """\
16
+ # ORLM LLaMA-3-8B
17
 
18
+ 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)!
 
 
 
 
19
  """
20
 
21
+ MAX_MAX_NEW_TOKENS = 4096
22
+ DEFAULT_MAX_NEW_TOKENS = 2048
23
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "1024"))
24
 
25
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
26
 
27
+ model_id = "CardinalOperations/ORLM-LLaMA-3-8B"
28
  tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)
29
  model = AutoModelForCausalLM.from_pretrained(
30
  model_id,
 
34
  model.config.sliding_window = 4096
35
  model.eval()
36
 
37
+ PROMPT_TEMPLATE = """
38
+ Below is an operations research question. Build a mathematical model and corresponding python code using `coptpy` that appropriately addresses the question.
39
+
40
+ # Question:
41
+ {Question}
42
+
43
+ # Response:
44
+ """
45
 
46
  @spaces.GPU(duration=90)
47
  def generate(
 
53
  top_k: int = 50,
54
  repetition_penalty: float = 1.2,
55
  ) -> Iterator[str]:
56
+ # conversation = []
57
+ # for user, assistant in chat_history:
58
+ # conversation.extend(
59
+ # [
60
+ # {"role": "user", "content": user},
61
+ # {"role": "assistant", "content": assistant},
62
+ # ]
63
+ # )
64
+ # conversation.append({"role": "user", "content": message})
65
+
66
+ # input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
67
+ # if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
68
+ # input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
69
+ # gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
70
+ # input_ids = input_ids.to(model.device)
71
+
72
+ prompt = PROMPT_TEMPLATE.replace("{Question}", message).strip()
73
+ tokenized_example = tokenizer(prompt, return_tensors='pt', max_length=MAX_INPUT_TOKEN_LENGTH, truncation=True)
74
+ input_ids = tokenized_example.input_ids
75
  input_ids = input_ids.to(model.device)
76
 
77
  streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
 
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
 
145
  with gr.Blocks(css="style.css", fill_height=True) as demo:
146
  gr.Markdown(DESCRIPTION)
147
+ # gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
148
  chat_interface.render()
149
 
150
  if __name__ == "__main__":