Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from instruct_pipeline import InstructionTextGenerationPipeline
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
theme = gr.themes.Monochrome(
|
| 12 |
+
primary_hue="indigo",
|
| 13 |
+
secondary_hue="blue",
|
| 14 |
+
neutral_hue="slate",
|
| 15 |
+
radius_size=gr.themes.sizes.radius_sm,
|
| 16 |
+
font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b", padding_side="left")
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b", device_map="auto", load_in_8bit=True)
|
| 21 |
+
|
| 22 |
+
generate_text = InstructionTextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 23 |
+
|
| 24 |
+
#generate_text = pipeline(model="databricks/dolly-v2-12b", torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")
|
| 25 |
+
|
| 26 |
+
def generate(instruction):
|
| 27 |
+
response = generate_text(instruction)
|
| 28 |
+
result = ""
|
| 29 |
+
for word in response.split(" "):
|
| 30 |
+
result += word + " "
|
| 31 |
+
yield result
|
| 32 |
+
|
| 33 |
+
examples = [
|
| 34 |
+
"Instead of making a peanut butter and jelly sandwich, what else could I combine peanut butter with in a sandwich? Give five ideas",
|
| 35 |
+
"How do I make a campfire?",
|
| 36 |
+
"Write me a tweet about the release of Dolly 2.0, a new LLM",
|
| 37 |
+
"Explain to me the difference between nuclear fission and fusion.",
|
| 38 |
+
"I'm selling my Nikon D-750, write a short blurb for my ad."
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
def process_example(args):
|
| 42 |
+
for x in generate(args):
|
| 43 |
+
pass
|
| 44 |
+
return x
|
| 45 |
+
|
| 46 |
+
css = ".generating {visibility: hidden}"
|
| 47 |
+
|
| 48 |
+
# Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
|
| 49 |
+
class SeafoamCustom(Base):
|
| 50 |
+
def __init__(
|
| 51 |
+
self,
|
| 52 |
+
*,
|
| 53 |
+
primary_hue: colors.Color | str = colors.emerald,
|
| 54 |
+
secondary_hue: colors.Color | str = colors.blue,
|
| 55 |
+
neutral_hue: colors.Color | str = colors.blue,
|
| 56 |
+
spacing_size: sizes.Size | str = sizes.spacing_md,
|
| 57 |
+
radius_size: sizes.Size | str = sizes.radius_md,
|
| 58 |
+
font: fonts.Font
|
| 59 |
+
| str
|
| 60 |
+
| Iterable[fonts.Font | str] = (
|
| 61 |
+
fonts.GoogleFont("Quicksand"),
|
| 62 |
+
"ui-sans-serif",
|
| 63 |
+
"sans-serif",
|
| 64 |
+
),
|
| 65 |
+
font_mono: fonts.Font
|
| 66 |
+
| str
|
| 67 |
+
| Iterable[fonts.Font | str] = (
|
| 68 |
+
fonts.GoogleFont("IBM Plex Mono"),
|
| 69 |
+
"ui-monospace",
|
| 70 |
+
"monospace",
|
| 71 |
+
),
|
| 72 |
+
):
|
| 73 |
+
super().__init__(
|
| 74 |
+
primary_hue=primary_hue,
|
| 75 |
+
secondary_hue=secondary_hue,
|
| 76 |
+
neutral_hue=neutral_hue,
|
| 77 |
+
spacing_size=spacing_size,
|
| 78 |
+
radius_size=radius_size,
|
| 79 |
+
font=font,
|
| 80 |
+
font_mono=font_mono,
|
| 81 |
+
)
|
| 82 |
+
super().set(
|
| 83 |
+
button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
|
| 84 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
|
| 85 |
+
button_primary_text_color="white",
|
| 86 |
+
button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
|
| 87 |
+
block_shadow="*shadow_drop_lg",
|
| 88 |
+
button_shadow="*shadow_drop_lg",
|
| 89 |
+
input_background_fill="zinc",
|
| 90 |
+
input_border_color="*secondary_300",
|
| 91 |
+
input_shadow="*shadow_drop",
|
| 92 |
+
input_shadow_focus="*shadow_drop_lg",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
seafoam = SeafoamCustom()
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
|
| 100 |
+
with gr.Column():
|
| 101 |
+
gr.Markdown(
|
| 102 |
+
""" ## Dolly 2.0
|
| 103 |
+
|
| 104 |
+
Dolly 2.0 is a 12B parameter language model based on the EleutherAI pythia model family and fine-tuned exclusively on a new, high-quality human generated instruction following dataset, crowdsourced among Databricks employees. For more details, please refer to the [model card](https://huggingface.co/databricks/dolly-v2-12b)
|
| 105 |
+
|
| 106 |
+
Type in the box below and click the button to generate answers to your most pressing questions!
|
| 107 |
+
|
| 108 |
+
"""
|
| 109 |
+
)
|
| 110 |
+
gr.HTML("<p>You can duplicate this Space to run it privately without a queue for shorter queue times : <a style='display:inline-block' href='https://huggingface.co/spaces/RamAnanth1/Dolly-v2?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a> </p>")
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column(scale=3):
|
| 114 |
+
instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
|
| 115 |
+
|
| 116 |
+
with gr.Box():
|
| 117 |
+
gr.Markdown("**Answer**")
|
| 118 |
+
output = gr.Markdown(elem_id="q-output")
|
| 119 |
+
submit = gr.Button("Generate", variant="primary")
|
| 120 |
+
gr.Examples(
|
| 121 |
+
examples=examples,
|
| 122 |
+
inputs=[instruction],
|
| 123 |
+
cache_examples=False,
|
| 124 |
+
fn=process_example,
|
| 125 |
+
outputs=[output],
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
submit.click(generate, inputs=[instruction], outputs=[output])
|
| 131 |
+
instruction.submit(generate, inputs=[instruction], outputs=[output])
|