GautamKishore commited on
Commit
8d9e35a
·
1 Parent(s): bce1948

feat: switch demo to lighter GGUF (llama.cpp) — 1.06GB vs 3.3GB fp16; add PyPI link

Browse files
Files changed (3) hide show
  1. Dockerfile +5 -5
  2. app.py +21 -38
  3. requirements.txt +1 -3
Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
  FROM python:3.12-slim
2
 
3
- # Fix fused model (v2) direct safetensors save, no corruption
4
  WORKDIR /app
5
 
6
  # System dependencies
@@ -8,11 +8,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
8
  git git-lfs ffmpeg libsm6 libxext6 cmake rsync libgl1 \
9
  && rm -rf /var/lib/apt/lists/* && git lfs install
10
 
11
- # Pre-download model at build time
12
- RUN pip install --no-cache-dir "huggingface-hub<1.22.0"
13
  RUN python3 -c "\
14
- from huggingface_hub import snapshot_download; \
15
- snapshot_download('eulogik/Bharat-Tiny-LLM-fused'); \
16
  "
17
 
18
  # Copy app files
 
1
  FROM python:3.12-slim
2
 
3
+ # Bharat-Tiny-LLM demo runs the GGUF build via llama.cpp (lighter than fp16)
4
  WORKDIR /app
5
 
6
  # System dependencies
 
8
  git git-lfs ffmpeg libsm6 libxext6 cmake rsync libgl1 \
9
  && rm -rf /var/lib/apt/lists/* && git lfs install
10
 
11
+ # Pre-download the GGUF model at build time so the Space starts fast
12
+ RUN pip install --no-cache-dir "huggingface-hub<1.22.0" "llama-cpp-python>=0.3.0"
13
  RUN python3 -c "\
14
+ from huggingface_hub import hf_hub_download; \
15
+ hf_hub_download(repo_id='eulogik/Bharat-Tiny-LLM-GGUF', filename='bharat-tiny-llm-q4_k_m.gguf'); \
16
  "
17
 
18
  # Copy app files
app.py CHANGED
@@ -16,10 +16,11 @@ def _json_schema_to_python_type_patched(schema, defs):
16
  _gcu._json_schema_to_python_type = _json_schema_to_python_type_patched
17
 
18
  import gradio as gr
19
- import torch
20
- from transformers import AutoModelForCausalLM, AutoTokenizer
21
 
22
- MODEL = "eulogik/Bharat-Tiny-LLM-fused"
 
23
  IM_END = 151645
24
 
25
  DESCRIPTION = """
@@ -32,6 +33,7 @@ DESCRIPTION = """
32
  Built by <a href="https://eulogik.com" target="_blank"><strong>eulogik</strong></a>
33
  · <a href="https://huggingface.co/eulogik/Bharat-Tiny-LLM" target="_blank">Download Model</a>
34
  · <a href="https://github.com/eulogik/Bharat-Tiny-LLM" target="_blank">GitHub</a>
 
35
  </p>
36
  </div>
37
  """
@@ -57,45 +59,25 @@ CUSTOM_CSS = """
57
  .examples-section { margin: 1rem 0; }
58
  """
59
 
60
- def load_pipeline():
61
- model = AutoModelForCausalLM.from_pretrained(MODEL, torch_dtype=torch.float32)
62
- tokenizer = AutoTokenizer.from_pretrained(MODEL)
63
- if tokenizer.pad_token_id is None:
64
- tokenizer.pad_token_id = tokenizer.eos_token_id
65
- model.eval()
66
- return model, tokenizer
67
-
68
- print("Loading model...")
69
- model, tokenizer = load_pipeline()
70
  print("Model loaded!")
71
 
 
72
  def generate(message, history, temperature, max_tokens):
73
  messages = [{"role": "user", "content": message}]
74
  if history:
75
  messages = [m for m in history] + messages
76
- prompt = ""
77
- for m in messages:
78
- prompt += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n"
79
- prompt += "<|im_start|>assistant\n"
80
- inputs = tokenizer(prompt, return_tensors="pt")
81
-
82
- with torch.no_grad():
83
- outputs = model.generate(
84
- **inputs,
85
- max_new_tokens=max_tokens,
86
- temperature=temperature,
87
- do_sample=True,
88
- top_p=0.85,
89
- repetition_penalty=1.25,
90
- no_repeat_ngram_size=3,
91
- pad_token_id=tokenizer.pad_token_id,
92
- eos_token_id=IM_END,
93
- )
94
-
95
- response = tokenizer.decode(
96
- outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True
97
  )
98
- return response
 
99
 
100
  def format_chat(message, history):
101
  history = history or []
@@ -105,6 +87,7 @@ def format_chat(message, history):
105
  history.append({"role": "assistant", "content": response})
106
  yield history, ""
107
 
 
108
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", neutral_hue="gray")) as demo:
109
  gr.HTML(DESCRIPTION)
110
 
@@ -128,8 +111,8 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", neutral_
128
 
129
  with gr.Accordion("Settings", open=False):
130
  with gr.Row():
131
- temperature_slider = gr.Slider(0.1, 1.0, value=0.3, step=0.05, label="Temperature")
132
- max_tokens_slider = gr.Slider(8, 256, value=128, step=8, label="Max Tokens")
133
 
134
  gr.Examples(
135
  examples=EXAMPLES,
@@ -149,4 +132,4 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", neutral_
149
  send.click(format_chat, [msg, chatbot], [chatbot, msg])
150
 
151
  if __name__ == "__main__":
152
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
16
  _gcu._json_schema_to_python_type = _json_schema_to_python_type_patched
17
 
18
  import gradio as gr
19
+ from llama_cpp import Llama
20
+ from huggingface_hub import hf_hub_download
21
 
22
+ MODEL_REPO = "eulogik/Bharat-Tiny-LLM-GGUF"
23
+ MODEL_FILE = "bharat-tiny-llm-q4_k_m.gguf"
24
  IM_END = 151645
25
 
26
  DESCRIPTION = """
 
33
  Built by <a href="https://eulogik.com" target="_blank"><strong>eulogik</strong></a>
34
  · <a href="https://huggingface.co/eulogik/Bharat-Tiny-LLM" target="_blank">Download Model</a>
35
  · <a href="https://github.com/eulogik/Bharat-Tiny-LLM" target="_blank">GitHub</a>
36
+ · <a href="https://pypi.org/project/bharat-tiny-llm/" target="_blank">PyPI</a>
37
  </p>
38
  </div>
39
  """
 
59
  .examples-section { margin: 1rem 0; }
60
  """
61
 
62
+ print("Loading GGUF model...")
63
+ gguf_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
64
+ llm = Llama(model_path=gguf_path, n_ctx=1024, n_gpu_layers=0, verbose=False)
 
 
 
 
 
 
 
65
  print("Model loaded!")
66
 
67
+
68
  def generate(message, history, temperature, max_tokens):
69
  messages = [{"role": "user", "content": message}]
70
  if history:
71
  messages = [m for m in history] + messages
72
+ out = llm.create_chat_completion(
73
+ messages=messages,
74
+ temperature=temperature,
75
+ top_p=0.85,
76
+ repeat_penalty=1.25,
77
+ max_tokens=max_tokens,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  )
79
+ return out["choices"][0]["message"]["content"]
80
+
81
 
82
  def format_chat(message, history):
83
  history = history or []
 
87
  history.append({"role": "assistant", "content": response})
88
  yield history, ""
89
 
90
+
91
  with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="blue", neutral_hue="gray")) as demo:
92
  gr.HTML(DESCRIPTION)
93
 
 
111
 
112
  with gr.Accordion("Settings", open=False):
113
  with gr.Row():
114
+ temperature_slider = gr.Slider(0.1, 1.0, value=0.3, step=0.05, label="Temperature")
115
+ max_tokens_slider = gr.Slider(8, 256, value=128, step=8, label="Max Tokens")
116
 
117
  gr.Examples(
118
  examples=EXAMPLES,
 
132
  send.click(format_chat, [msg, chatbot], [chatbot, msg])
133
 
134
  if __name__ == "__main__":
135
+ demo.launch()
requirements.txt CHANGED
@@ -2,6 +2,4 @@ gradio==4.44.1
2
  starlette==0.51.0
3
  requests>=2.31
4
  huggingface_hub<1.22.0
5
- transformers>=4.40
6
- torch>=2.1
7
- sentencepiece>=0.2
 
2
  starlette==0.51.0
3
  requests>=2.31
4
  huggingface_hub<1.22.0
5
+ llama-cpp-python>=0.3.0