Monster commited on
Commit
c832e20
1 Parent(s): 36ed091

Create app.py

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