Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import subprocess
|
7 |
+
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
from llama_cpp import Llama
|
10 |
+
from llama_cpp import LlamaRAMCache
|
11 |
+
|
12 |
+
hf_hub_download(repo_id="TinyPixel/Llama-2-7B-bf16-sharded", filename="llama-2-7b-bf16-sharded.tar", local_dir=".")
|
13 |
+
|
14 |
+
|
15 |
+
llm = Llama(model_path="llama-2-7b-bf16-sharded.tar", rms_norm_eps=1e-5)
|
16 |
+
|
17 |
+
cache = LlamaRAMCache(capacity_bytes=2 << 30)
|
18 |
+
|
19 |
+
llm.set_cache(cache)
|
20 |
+
|
21 |
+
|
22 |
+
ins = '''[INST] <<SYS>>
|
23 |
+
You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct.
|
24 |
+
If you don't know the answer to a question, please don't share false information.
|
25 |
+
If given a creative task, generate a unique response where your answers are based on factual truths.
|
26 |
+
<</SYS>>
|
27 |
+
{} [/INST]
|
28 |
+
'''
|
29 |
+
|
30 |
+
theme = gr.themes.Monochrome(
|
31 |
+
primary_hue="indigo",
|
32 |
+
secondary_hue="blue",
|
33 |
+
neutral_hue="slate",
|
34 |
+
radius_size=gr.themes.sizes.radius_sm,
|
35 |
+
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
36 |
+
)
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
def generate(instruction):
|
42 |
+
result = ""
|
43 |
+
for x in llm(ins.format(instruction), stop=['USER:'], stream=True, max_tokens=512):
|
44 |
+
result += x['choices'][0]['text']
|
45 |
+
yield result
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
examples = [
|
50 |
+
"Instead of making a peanut butter and jelly sandwich, what else could I combine peanut butter with in a sandwich? Give five ideas",
|
51 |
+
"How do I make a campfire?",
|
52 |
+
"Explain to me the difference between nuclear fission and fusion.",
|
53 |
+
"I'm selling my Nikon D-750, write a short blurb for my ad."
|
54 |
+
]
|
55 |
+
|
56 |
+
def process_example(args):
|
57 |
+
for x in generate(args):
|
58 |
+
pass
|
59 |
+
return x
|
60 |
+
|
61 |
+
css = ".generating {visibility: hidden}"
|
62 |
+
|
63 |
+
# Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
|
64 |
+
class SeafoamCustom(Base):
|
65 |
+
def __init__(
|
66 |
+
self,
|
67 |
+
*,
|
68 |
+
primary_hue: colors.Color | str = colors.emerald,
|
69 |
+
secondary_hue: colors.Color | str = colors.blue,
|
70 |
+
neutral_hue: colors.Color | str = colors.blue,
|
71 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
72 |
+
radius_size: sizes.Size | str = sizes.radius_md,
|
73 |
+
font: fonts.Font
|
74 |
+
| str
|
75 |
+
| Iterable[fonts.Font | str] = (
|
76 |
+
fonts.GoogleFont("Quicksand"),
|
77 |
+
"ui-sans-serif",
|
78 |
+
"sans-serif",
|
79 |
+
),
|
80 |
+
font_mono: fonts.Font
|
81 |
+
| str
|
82 |
+
| Iterable[fonts.Font | str] = (
|
83 |
+
fonts.GoogleFont("IBM Plex Mono"),
|
84 |
+
"ui-monospace",
|
85 |
+
"monospace",
|
86 |
+
),
|
87 |
+
):
|
88 |
+
super().__init__(
|
89 |
+
primary_hue=primary_hue,
|
90 |
+
secondary_hue=secondary_hue,
|
91 |
+
neutral_hue=neutral_hue,
|
92 |
+
spacing_size=spacing_size,
|
93 |
+
radius_size=radius_size,
|
94 |
+
font=font,
|
95 |
+
font_mono=font_mono,
|
96 |
+
)
|
97 |
+
super().set(
|
98 |
+
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
99 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
100 |
+
button_primary_text_color="white",
|
101 |
+
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
102 |
+
block_shadow="*shadow_drop_lg",
|
103 |
+
button_shadow="*shadow_drop_lg",
|
104 |
+
input_background_fill="zinc",
|
105 |
+
input_border_color="*secondary_300",
|
106 |
+
input_shadow="*shadow_drop",
|
107 |
+
input_shadow_focus="*shadow_drop_lg",
|
108 |
+
)
|
109 |
+
|
110 |
+
|
111 |
+
seafoam = SeafoamCustom()
|
112 |
+
|
113 |
+
|
114 |
+
with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
|
115 |
+
with gr.Column():
|
116 |
+
gr.Markdown(
|
117 |
+
""" ## Llama-O 🦙💬
|
118 |
+
|
119 |
+
4bit (q4_K_M)
|
120 |
+
|
121 |
+
Type in the box below and click the button to generate answers to your most pressing questions!
|
122 |
+
|
123 |
+
"""
|
124 |
+
)
|
125 |
+
|
126 |
+
with gr.Row():
|
127 |
+
with gr.Column(scale=3):
|
128 |
+
instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
|
129 |
+
|
130 |
+
with gr.Box():
|
131 |
+
gr.Markdown("**Answer**")
|
132 |
+
output = gr.Markdown(elem_id="q-output")
|
133 |
+
submit = gr.Button("Generate", variant="primary")
|
134 |
+
gr.Examples(
|
135 |
+
examples=examples,
|
136 |
+
inputs=[instruction],
|
137 |
+
cache_examples=True,
|
138 |
+
fn=process_example,
|
139 |
+
outputs=[output],
|
140 |
+
)
|
141 |
+
|
142 |
+
|
143 |
+
|
144 |
+
submit.click(generate, inputs=[instruction], outputs=[output])
|
145 |
+
instruction.submit(generate, inputs=[instruction], outputs=[output])
|
146 |
+
|
147 |
+
demo.queue(concurrency_count=1).launch(debug=False)
|