lifeofcoding commited on
Commit
3ecbde7
·
1 Parent(s): 78d4605

initial commit

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -0
  2. app.py +143 -0
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ huggingface-hub
2
+ llama-cpp-python
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ from typing import Iterable
3
+ import gradio as gr
4
+ from gradio.themes.base import Base
5
+ from gradio.themes.utils import colors, fonts, sizes
6
+
7
+ from llama_cpp import Llama
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ hf_hub_download(repo_id="lifeofcoding/alpaca-lora-movie-review-sentiment", filename="adapter_model.bin", local_dir=".")
11
+ llm = Llama(model_path="./adapter_model.bin")
12
+
13
+
14
+ ins = '''Below is an instruction that describes a task. Write a response that appropriately completes the request.
15
+ ### Instruction:
16
+ {}
17
+ ### Response:
18
+ '''
19
+
20
+ theme = gr.themes.Monochrome(
21
+ primary_hue="indigo",
22
+ secondary_hue="blue",
23
+ neutral_hue="slate",
24
+ radius_size=gr.themes.sizes.radius_sm,
25
+ font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
26
+ )
27
+
28
+
29
+
30
+
31
+ # def generate(instruction):
32
+ # response = llm(ins.format(instruction))
33
+ # response = response['choices'][0]['text']
34
+ # result = ""
35
+ # for word in response.split(" "):
36
+ # result += word + " "
37
+ # yield result
38
+
39
+ def generate(instruction):
40
+ result = ""
41
+ for x in llm(ins.format(instruction), stop=['### Instruction:', '### End'], stream=True):
42
+ result += x['choices'][0]['text']
43
+ yield result
44
+
45
+
46
+ examples = [
47
+ "Instead of making a peanut butter and jelly sandwich, what else could I combine peanut butter with in a sandwich? Give five ideas",
48
+ "How do I make a campfire?",
49
+ "Explain to me the difference between nuclear fission and fusion.",
50
+ "Write an ad for sale Nikon D750."
51
+ ]
52
+
53
+ def process_example(args):
54
+ for x in generate(args):
55
+ pass
56
+ return x
57
+
58
+ css = ".generating {visibility: hidden}"
59
+
60
+ # Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
61
+ class SeafoamCustom(Base):
62
+ def __init__(
63
+ self,
64
+ *,
65
+ primary_hue: colors.Color | str = colors.emerald,
66
+ secondary_hue: colors.Color | str = colors.blue,
67
+ neutral_hue: colors.Color | str = colors.blue,
68
+ spacing_size: sizes.Size | str = sizes.spacing_md,
69
+ radius_size: sizes.Size | str = sizes.radius_md,
70
+ font: fonts.Font
71
+ | str
72
+ | Iterable[fonts.Font | str] = (
73
+ fonts.GoogleFont("Quicksand"),
74
+ "ui-sans-serif",
75
+ "sans-serif",
76
+ ),
77
+ font_mono: fonts.Font
78
+ | str
79
+ | Iterable[fonts.Font | str] = (
80
+ fonts.GoogleFont("IBM Plex Mono"),
81
+ "ui-monospace",
82
+ "monospace",
83
+ ),
84
+ ):
85
+ super().__init__(
86
+ primary_hue=primary_hue,
87
+ secondary_hue=secondary_hue,
88
+ neutral_hue=neutral_hue,
89
+ spacing_size=spacing_size,
90
+ radius_size=radius_size,
91
+ font=font,
92
+ font_mono=font_mono,
93
+ )
94
+ super().set(
95
+ button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
96
+ button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
97
+ button_primary_text_color="white",
98
+ button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
99
+ block_shadow="*shadow_drop_lg",
100
+ button_shadow="*shadow_drop_lg",
101
+ input_background_fill="zinc",
102
+ input_border_color="*secondary_300",
103
+ input_shadow="*shadow_drop",
104
+ input_shadow_focus="*shadow_drop_lg",
105
+ )
106
+
107
+
108
+ seafoam = SeafoamCustom()
109
+
110
+
111
+ with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
112
+ with gr.Column():
113
+ gr.Markdown(
114
+ """ ## Alpaca-LoRa
115
+ 7b quantized 4bit (q4_1)
116
+
117
+ Type in the box below and click the button to generate answers to your most pressing questions!
118
+
119
+ """
120
+ )
121
+
122
+ with gr.Row():
123
+ with gr.Column(scale=3):
124
+ instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
125
+
126
+ with gr.Box():
127
+ gr.Markdown("**Answer**")
128
+ output = gr.Markdown(elem_id="q-output")
129
+ submit = gr.Button("Generate", variant="primary")
130
+ gr.Examples(
131
+ examples=examples,
132
+ inputs=[instruction],
133
+ cache_examples=False,
134
+ fn=process_example,
135
+ outputs=[output],
136
+ )
137
+
138
+
139
+
140
+ submit.click(generate, inputs=[instruction], outputs=[output])
141
+ instruction.submit(generate, inputs=[instruction], outputs=[output])
142
+
143
+ demo.queue(concurrency_count=1).launch(debug=True)