Dawoodthouseef commited on
Commit
a865a75
1 Parent(s): c461628

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ from ctransformers import AutoModelForCausalLM
4
+
5
+ # Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
6
+ model = AutoModelForCausalLM.from_pretrained("TheBloke/Mistral-7B-Instruct-v0.1-GGUF", model_file="mistral-7b-instruct-v0.1.Q5_K_S.gguf", model_type="mistral", gpu_layers=0)
7
+ ins = '''[INST] <<SYS>>
8
+ You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
9
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
10
+ <</SYS>>
11
+ {} [/INST]
12
+ '''
13
+
14
+ theme = gr.themes.Monochrome(
15
+ primary_hue="indigo",
16
+ secondary_hue="blue",
17
+ neutral_hue="slate",
18
+ radius_size=gr.themes.sizes.radius_sm,
19
+ font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
20
+ )
21
+ def response(question):
22
+ start_time = time.time()
23
+ res = model(ins.format(question))
24
+ end_time = time.time()
25
+ yield {'inference_time': end_time - start_time,'response':res}
26
+
27
+
28
+ examples = [
29
+ "Instead of making a peanut butter and jelly sandwich, what else could I combine peanut butter with in a sandwich? Give five ideas",
30
+ "How do I make a campfire?",
31
+ "Explain to me the difference between nuclear fission and fusion.",
32
+ "I'm selling my Nikon D-750, write a short blurb for my ad."
33
+ ]
34
+
35
+ def process_example(args):
36
+ for x in generate(args):
37
+ pass
38
+ return x
39
+
40
+ css = ".generating {visibility: hidden}"
41
+
42
+ # Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
43
+ class SeafoamCustom(Base):
44
+ def __init__(
45
+ self,
46
+ *,
47
+ primary_hue: colors.Color | str = colors.emerald,
48
+ secondary_hue: colors.Color | str = colors.blue,
49
+ neutral_hue: colors.Color | str = colors.blue,
50
+ spacing_size: sizes.Size | str = sizes.spacing_md,
51
+ radius_size: sizes.Size | str = sizes.radius_md,
52
+ font: fonts.Font
53
+ | str
54
+ | Iterable[fonts.Font | str] = (
55
+ fonts.GoogleFont("Quicksand"),
56
+ "ui-sans-serif",
57
+ "sans-serif",
58
+ ),
59
+ font_mono: fonts.Font
60
+ | str
61
+ | Iterable[fonts.Font | str] = (
62
+ fonts.GoogleFont("IBM Plex Mono"),
63
+ "ui-monospace",
64
+ "monospace",
65
+ ),
66
+ ):
67
+ super().__init__(
68
+ primary_hue=primary_hue,
69
+ secondary_hue=secondary_hue,
70
+ neutral_hue=neutral_hue,
71
+ spacing_size=spacing_size,
72
+ radius_size=radius_size,
73
+ font=font,
74
+ font_mono=font_mono,
75
+ )
76
+ super().set(
77
+ button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
78
+ button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
79
+ button_primary_text_color="white",
80
+ button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
81
+ block_shadow="*shadow_drop_lg",
82
+ button_shadow="*shadow_drop_lg",
83
+ input_background_fill="zinc",
84
+ input_border_color="*secondary_300",
85
+ input_shadow="*shadow_drop",
86
+ input_shadow_focus="*shadow_drop_lg",
87
+ )
88
+
89
+
90
+ seafoam = SeafoamCustom()
91
+
92
+
93
+ with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
94
+ with gr.Column():
95
+ gr.Markdown(
96
+ """ ## Mistral-7b
97
+
98
+ Type in the box below and click the button to generate answers to your most pressing questions!
99
+
100
+ """
101
+ )
102
+
103
+ with gr.Row():
104
+ with gr.Column(scale=3):
105
+ instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
106
+
107
+ with gr.Box():
108
+ gr.Markdown("**Answer**")
109
+ output = gr.Markdown(elem_id="q-output")
110
+ submit = gr.Button("Generate", variant="primary")
111
+ gr.Examples(
112
+ examples=examples,
113
+ inputs=[instruction],
114
+ cache_examples=True,
115
+ fn=process_example,
116
+ outputs=[output],
117
+ )
118
+
119
+
120
+
121
+ submit.click(generate, inputs=[instruction], outputs=[output])
122
+ instruction.submit(generate, inputs=[instruction], outputs=[output])
123
+
124
+ demo.queue(concurrency_count=1).launch(debug=False)