ffreemt commited on
Commit
909aca2
1 Parent(s): 5080c22

Add loguru, pynvml

Browse files
Files changed (2) hide show
  1. app.py +44 -17
  2. requirements.txt +3 -1
app.py CHANGED
@@ -3,57 +3,83 @@ from __future__ import annotations
3
  from typing import Iterable
4
 
5
  import gradio as gr
6
- import torch
 
 
7
  from ctransformers import AutoModelForCausalLM
8
  from gradio.themes.base import Base
9
  from gradio.themes.utils import colors, fonts, sizes
10
  from huggingface_hub import hf_hub_download # snapshot_download,
 
11
 
12
- repo_id = 'TheBloke/openbuddy-mistral-7B-v13-GGUF'
13
- filename = 'openbuddy-mistral-7b-v13.Q4_K_S.gguf' # 4.17G
14
 
 
15
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, revision="main")
 
16
 
17
  # Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
18
  # model = AutoModelForCausalLM.from_pretrained("TheBloke/openbuddy-mistral-7B-v13-GGUF", model_file="openbuddy-mistral-7b-v13.Q4_K_S.gguf", model_type="mistral", gpu_layers=0)
19
 
20
- if torch.cuda.is_available():
 
 
 
 
 
 
 
 
 
21
  gpu_layers = 50 # set to what you like for GPU
22
  else:
23
  gpu_layers = 0
24
- model = AutoModelForCausalLM.from_pretrained(model_path, model_type="mistral", gpu_layers=gpu_layers)
25
 
26
- ins = '''[INST] <<FRIDAY>>
 
 
 
 
 
 
27
  Remember that your English name is "Shi-Ci" and your name in Chinese is "兮辞". 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.
28
  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.
29
  <</FRIDAY>>
30
  {} [/INST]
31
- '''
32
-
33
 
34
  theme = gr.themes.Monochrome(
35
  primary_hue="indigo",
36
  secondary_hue="blue",
37
  neutral_hue="slate",
38
  radius_size=gr.themes.sizes.radius_sm,
39
- font=[gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif"],
 
 
 
 
 
40
  )
 
 
41
  def response(question):
42
  res = model(ins.format(question))
43
  yield res
44
 
45
 
46
- examples = [
47
- "Hello!"
48
- ]
49
 
50
  def process_example(args):
51
  for x in response(args):
52
  pass
53
  return x
54
 
 
55
  css = ".generating {visibility: hidden}"
56
 
 
57
  # Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
58
  class SeafoamCustom(Base):
59
  def __init__(
@@ -117,9 +143,12 @@ with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
117
  )
118
 
119
  with gr.Row():
120
-
121
  with gr.Column(scale=3):
122
- instruction = gr.Textbox(placeholder="Enter your question here", label="Question", elem_id="q-input")
 
 
 
 
123
 
124
  with gr.Box():
125
  gr.Markdown("**Answer**")
@@ -133,9 +162,7 @@ with gr.Blocks(theme=seafoam, analytics_enabled=False, css=css) as demo:
133
  outputs=[output],
134
  )
135
 
136
-
137
-
138
  submit.click(response, inputs=[instruction], outputs=[output])
139
  instruction.submit(response, inputs=[instruction], outputs=[output])
140
 
141
- demo.queue(concurrency_count=1).launch(debug=False,share=True)
 
3
  from typing import Iterable
4
 
5
  import gradio as gr
6
+ import pynvml
7
+
8
+ # import torch
9
  from ctransformers import AutoModelForCausalLM
10
  from gradio.themes.base import Base
11
  from gradio.themes.utils import colors, fonts, sizes
12
  from huggingface_hub import hf_hub_download # snapshot_download,
13
+ from loguru import logger
14
 
15
+ repo_id = "TheBloke/openbuddy-mistral-7B-v13-GGUF"
16
+ filename = "openbuddy-mistral-7b-v13.Q4_K_S.gguf" # 4.17G
17
 
18
+ logger.debug("Start dl")
19
  model_path = hf_hub_download(repo_id=repo_id, filename=filename, revision="main")
20
+ logger.debug("Done dl")
21
 
22
  # Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
23
  # model = AutoModelForCausalLM.from_pretrained("TheBloke/openbuddy-mistral-7B-v13-GGUF", model_file="openbuddy-mistral-7b-v13.Q4_K_S.gguf", model_type="mistral", gpu_layers=0)
24
 
25
+ has_cuda = False
26
+ try:
27
+ pynvml.nvmlInit()
28
+ has_cuda = True
29
+ logger.debug("has cuda")
30
+ except pynvml.nvml.NVMLError_LibraryNotFound:
31
+ logger.debug("no cuda")
32
+
33
+ # if torch.cuda.is_available():
34
+ if has_cuda:
35
  gpu_layers = 50 # set to what you like for GPU
36
  else:
37
  gpu_layers = 0
 
38
 
39
+ logger.debug("Start loading the model")
40
+ model = AutoModelForCausalLM.from_pretrained(
41
+ model_path, model_type="mistral", gpu_layers=gpu_layers
42
+ )
43
+ logger.debug("Done loading the model")
44
+
45
+ ins = """[INST] <<FRIDAY>>
46
  Remember that your English name is "Shi-Ci" and your name in Chinese is "兮辞". 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.
47
  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.
48
  <</FRIDAY>>
49
  {} [/INST]
50
+ """
 
51
 
52
  theme = gr.themes.Monochrome(
53
  primary_hue="indigo",
54
  secondary_hue="blue",
55
  neutral_hue="slate",
56
  radius_size=gr.themes.sizes.radius_sm,
57
+ font=[
58
+ gr.themes.GoogleFont("Open Sans"),
59
+ "ui-sans-serif",
60
+ "system-ui",
61
+ "sans-serif",
62
+ ],
63
  )
64
+
65
+
66
  def response(question):
67
  res = model(ins.format(question))
68
  yield res
69
 
70
 
71
+ examples = ["Hello!"]
72
+
 
73
 
74
  def process_example(args):
75
  for x in response(args):
76
  pass
77
  return x
78
 
79
+
80
  css = ".generating {visibility: hidden}"
81
 
82
+
83
  # Based on the gradio theming guide and borrowed from https://huggingface.co/spaces/shivi/dolly-v2-demo
84
  class SeafoamCustom(Base):
85
  def __init__(
 
143
  )
144
 
145
  with gr.Row():
 
146
  with gr.Column(scale=3):
147
+ instruction = gr.Textbox(
148
+ placeholder="Enter your question here",
149
+ label="Question",
150
+ elem_id="q-input",
151
+ )
152
 
153
  with gr.Box():
154
  gr.Markdown("**Answer**")
 
162
  outputs=[output],
163
  )
164
 
 
 
165
  submit.click(response, inputs=[instruction], outputs=[output])
166
  instruction.submit(response, inputs=[instruction], outputs=[output])
167
 
168
+ demo.queue(concurrency_count=1).launch(debug=False, share=True)
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  ctransformers
2
  gradio
3
- huggingface-hub
 
 
 
1
  ctransformers
2
  gradio
3
+ huggingface-hub
4
+ loguru
5
+ pynvml