lifeofcoding commited on
Commit
2a98166
1 Parent(s): 5fd853d
Files changed (2) hide show
  1. app.py +50 -117
  2. requirements.txt +8 -0
app.py CHANGED
@@ -3,10 +3,6 @@ from peft import PeftModel
3
  import transformers
4
  import gradio as gr
5
 
6
- from typing import Iterable
7
- from gradio.themes.base import Base
8
- from gradio.themes.utils import colors, fonts, sizes
9
-
10
  assert (
11
  "LlamaTokenizer" in transformers._import_structure["models.llama"]
12
  ), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"
@@ -15,7 +11,7 @@ from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
15
  tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
16
 
17
  BASE_MODEL = "decapoda-research/llama-7b-hf"
18
- LORA_WEIGHTS = "lifeofcoding/alpaca-lora-movie-review-sentiment"
19
 
20
  if torch.cuda.is_available():
21
  device = "cuda"
@@ -64,15 +60,20 @@ else:
64
  def generate_prompt(instruction, input=None):
65
  if input:
66
  return 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.
 
67
  ### Instruction:
68
  {instruction}
 
69
  ### Input:
70
  {input}
 
71
  ### Response:"""
72
  else:
73
  return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
 
74
  ### Instruction:
75
  {instruction}
 
76
  ### Response:"""
77
 
78
  if device != "cpu":
@@ -115,118 +116,50 @@ def evaluate(
115
  return output.split("### Response:")[1].strip()
116
 
117
 
118
- ins = '''Below is an instruction that describes a task. Write a response that appropriately completes the request.
119
- ### Instruction:
120
- {}
121
- ### Response:
122
- '''
123
-
124
- theme = gr.themes.Monochrome(
125
- primary_hue="indigo",
126
- secondary_hue="blue",
127
- neutral_hue="slate",
128
- radius_size=gr.themes.sizes.radius_sm,
129
- font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
130
- )
131
-
132
-
133
-
134
-
135
- examples = [
136
- "Instead of making a peanut butter and jelly sandwich, what else could I combine peanut butter with in a sandwich? Give five ideas",
137
- "How do I make a campfire?",
138
- "Explain to me the difference between nuclear fission and fusion.",
139
- "Write an ad for sale Nikon D750."
140
- ]
141
-
142
- def process_example(args):
143
- for x in evaluate(args):
144
- pass
145
- return x
146
-
147
- css = ".generating {visibility: hidden}"
148
-
149
- # Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
150
- class SeafoamCustom(Base):
151
- def __init__(
152
- self,
153
- *,
154
- primary_hue: colors.Color | str = colors.emerald,
155
- secondary_hue: colors.Color | str = colors.blue,
156
- neutral_hue: colors.Color | str = colors.blue,
157
- spacing_size: sizes.Size | str = sizes.spacing_md,
158
- radius_size: sizes.Size | str = sizes.radius_md,
159
- font: fonts.Font
160
- | str
161
- | Iterable[fonts.Font | str] = (
162
- fonts.GoogleFont("Quicksand"),
163
- "ui-sans-serif",
164
- "sans-serif",
165
  ),
166
- font_mono: fonts.Font
167
- | str
168
- | Iterable[fonts.Font | str] = (
169
- fonts.GoogleFont("IBM Plex Mono"),
170
- "ui-monospace",
171
- "monospace",
 
172
  ),
173
- ):
174
- super().__init__(
175
- primary_hue=primary_hue,
176
- secondary_hue=secondary_hue,
177
- neutral_hue=neutral_hue,
178
- spacing_size=spacing_size,
179
- radius_size=radius_size,
180
- font=font,
181
- font_mono=font_mono,
182
  )
183
- super().set(
184
- button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
185
- button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
186
- button_primary_text_color="white",
187
- button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
188
- block_shadow="*shadow_drop_lg",
189
- button_shadow="*shadow_drop_lg",
190
- input_background_fill="zinc",
191
- input_border_color="*secondary_300",
192
- input_shadow="*shadow_drop",
193
- input_shadow_focus="*shadow_drop_lg",
194
- )
195
-
196
-
197
- seafoam = SeafoamCustom()
198
-
199
-
200
- with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
201
- with gr.Column():
202
- gr.Markdown(
203
- """ ## Alpaca-LoRa
204
- 7b quantized 4bit (q4_1)
205
-
206
- Type in the box below and click the button to generate answers to your most pressing questions!
207
-
208
- """
209
- )
210
-
211
- with gr.Row():
212
- with gr.Column(scale=3):
213
- instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
214
-
215
- with gr.Box():
216
- gr.Markdown("**Answer**")
217
- output = gr.Markdown(elem_id="q-output")
218
- submit = gr.Button("Generate", variant="primary")
219
- gr.Examples(
220
- examples=examples,
221
- inputs=[instruction],
222
- cache_examples=False,
223
- fn=process_example,
224
- outputs=[output],
225
- )
226
-
227
-
228
-
229
- submit.click(evaluate, inputs=[instruction], outputs=[output])
230
- instruction.submit(evaluate, inputs=[instruction], outputs=[output])
231
-
232
- demo.queue(concurrency_count=1).launch(debug=True)
 
3
  import transformers
4
  import gradio as gr
5
 
 
 
 
 
6
  assert (
7
  "LlamaTokenizer" in transformers._import_structure["models.llama"]
8
  ), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"
 
11
  tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
12
 
13
  BASE_MODEL = "decapoda-research/llama-7b-hf"
14
+ LORA_WEIGHTS = "tloen/alpaca-lora-7b"
15
 
16
  if torch.cuda.is_available():
17
  device = "cuda"
 
60
  def generate_prompt(instruction, input=None):
61
  if input:
62
  return 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.
63
+
64
  ### Instruction:
65
  {instruction}
66
+
67
  ### Input:
68
  {input}
69
+
70
  ### Response:"""
71
  else:
72
  return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
73
+
74
  ### Instruction:
75
  {instruction}
76
+
77
  ### Response:"""
78
 
79
  if device != "cpu":
 
116
  return output.split("### Response:")[1].strip()
117
 
118
 
119
+ g = gr.Interface(
120
+ fn=evaluate,
121
+ inputs=[
122
+ gr.components.Textbox(
123
+ lines=2, label="Instruction", placeholder="Tell me about alpacas."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  ),
125
+ gr.components.Textbox(lines=2, label="Input", placeholder="none"),
126
+ gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
127
+ gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
128
+ gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
129
+ gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
130
+ gr.components.Slider(
131
+ minimum=1, maximum=512, step=1, value=128, label="Max tokens"
132
  ),
133
+ ],
134
+ outputs=[
135
+ gr.inputs.Textbox(
136
+ lines=5,
137
+ label="Output",
 
 
 
 
138
  )
139
+ ],
140
+ title="🦙🌲 Alpaca-LoRA",
141
+ description="Alpaca-LoRA is a 7B-parameter LLaMA model finetuned to follow instructions. It is trained on the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset and makes use of the Huggingface LLaMA implementation. For more information, please visit [the project's website](https://github.com/tloen/alpaca-lora).",
142
+ )
143
+ g.queue(concurrency_count=1)
144
+ g.launch()
145
+
146
+ # Old testing code follows.
147
+
148
+ """
149
+ if __name__ == "__main__":
150
+ # testing code for readme
151
+ for instruction in [
152
+ "Tell me about alpacas.",
153
+ "Tell me about the president of Mexico in 2019.",
154
+ "Tell me about the king of France in 2019.",
155
+ "List all Canadian provinces in alphabetical order.",
156
+ "Write a Python program that prints the first 10 Fibonacci numbers.",
157
+ "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.",
158
+ "Tell me five words that rhyme with 'shock'.",
159
+ "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
160
+ "Count up from 1 to 500.",
161
+ ]:
162
+ print("Instruction:", instruction)
163
+ print("Response:", evaluate(instruction))
164
+ print()
165
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ datasets
2
+ loralib
3
+ sentencepiece
4
+ git+https://github.com/huggingface/transformers.git
5
+ accelerate
6
+ bitsandbytes
7
+ git+https://github.com/huggingface/peft.git
8
+ gradio