Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
19a1870
1
Parent(s):
3a44bb8
changed space to use LiquidAI/LFM2-1.2
Browse files- README.md +5 -5
- app.py +58 -60
- requirements.txt +1 -5
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: true
|
10 |
license: apache-2.0
|
|
|
1 |
---
|
2 |
+
title: LiquidAI - LFM2-1.2B Chat
|
3 |
+
emoji: π
|
4 |
+
colorFrom: orange
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.7.3
|
8 |
app_file: app.py
|
9 |
pinned: true
|
10 |
license: apache-2.0
|
app.py
CHANGED
@@ -1,82 +1,80 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
-
from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
6 |
from threading import Thread
|
7 |
|
8 |
-
# Lazy loading the model to meet huggingface stateless GPU requirements
|
9 |
-
|
10 |
-
# Defining a custom stopping criteria class for the model's text generation.
|
11 |
-
class StopOnTokens(StoppingCriteria):
|
12 |
-
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
13 |
-
stop_ids = [50256, 50295] # IDs of tokens where the generation should stop.
|
14 |
-
for stop_id in stop_ids:
|
15 |
-
if input_ids[0][-1] == stop_id: # Checking if the last generated token is a stop token.
|
16 |
-
return True
|
17 |
-
return False
|
18 |
-
|
19 |
-
|
20 |
-
# Function to generate model predictions.
|
21 |
@spaces.GPU
|
22 |
def predict(message, history):
|
23 |
torch.set_default_device("cuda")
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
trust_remote_code=True
|
29 |
-
)
|
30 |
model = AutoModelForCausalLM.from_pretrained(
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
trust_remote_code=True
|
|
|
|
|
35 |
)
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
44 |
generate_kwargs = dict(
|
45 |
-
input_ids,
|
46 |
streamer=streamer,
|
47 |
max_new_tokens=256,
|
48 |
do_sample=True,
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
stopping_criteria=StoppingCriteriaList([stop])
|
54 |
)
|
|
|
|
|
55 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
56 |
-
t.start()
|
|
|
|
|
57 |
partial_message = ""
|
58 |
for new_token in streamer:
|
59 |
partial_message += new_token
|
60 |
-
if '<|im_end|>' in partial_message: # Breaking the loop if the stop token is generated.
|
61 |
-
break
|
62 |
yield partial_message
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
5 |
from threading import Thread
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
@spaces.GPU
|
8 |
def predict(message, history):
|
9 |
torch.set_default_device("cuda")
|
10 |
+
|
11 |
+
# Load model and tokenizer
|
12 |
+
model_id = "LiquidAI/LFM2-1.2B"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(
|
15 |
+
model_id,
|
16 |
+
device_map="auto",
|
17 |
+
torch_dtype=torch.bfloat16,
|
18 |
+
trust_remote_code=True,
|
19 |
+
load_in_4bit=True, # Keeping 4-bit quantization for efficiency
|
20 |
+
# attn_implementation="flash_attention_2" # Uncomment on compatible GPU
|
21 |
)
|
22 |
+
|
23 |
+
# Format conversation history for chat template
|
24 |
+
messages = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
25 |
+
for conv in history for i, msg in enumerate(conv) if msg]
|
26 |
+
messages.append({"role": "user", "content": message})
|
27 |
+
|
28 |
+
# Apply chat template
|
29 |
+
input_ids = tokenizer.apply_chat_template(
|
30 |
+
messages,
|
31 |
+
add_generation_prompt=True,
|
32 |
+
return_tensors="pt",
|
33 |
+
tokenize=True
|
34 |
+
).to('cuda')
|
35 |
+
|
36 |
+
# Setup streamer for real-time output
|
37 |
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
38 |
+
|
39 |
+
# Generation parameters
|
40 |
generate_kwargs = dict(
|
41 |
+
input_ids=input_ids,
|
42 |
streamer=streamer,
|
43 |
max_new_tokens=256,
|
44 |
do_sample=True,
|
45 |
+
temperature=0.3,
|
46 |
+
min_p=0.15,
|
47 |
+
repetition_penalty=1.05,
|
48 |
+
pad_token_id=tokenizer.eos_token_id
|
|
|
49 |
)
|
50 |
+
|
51 |
+
# Start generation in separate thread
|
52 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
53 |
+
t.start()
|
54 |
+
|
55 |
+
# Stream tokens
|
56 |
partial_message = ""
|
57 |
for new_token in streamer:
|
58 |
partial_message += new_token
|
|
|
|
|
59 |
yield partial_message
|
60 |
|
61 |
+
# Setup Gradio interface
|
62 |
+
gr.ChatInterface(
|
63 |
+
predict,
|
64 |
+
description="""
|
65 |
+
<center><h2>LiquidAI LFM2-1.2B Chat</h2></center>
|
66 |
+
|
67 |
+
Chat with [LiquidAI/LFM2-1.2B](https://huggingface.co/LiquidAI/LFM2-1.2B), a compact and efficient language model.
|
68 |
+
|
69 |
+
This model provides high-quality responses while maintaining a small footprint, making it ideal for fast inference.
|
70 |
+
""",
|
71 |
+
examples=[
|
72 |
+
'Can you solve the equation 2x + 3 = 11 for x?',
|
73 |
+
'What is C. elegans?',
|
74 |
+
'Explain quantum computing in simple terms',
|
75 |
+
'Write a Python function to find prime numbers',
|
76 |
+
'What are the key differences between RNA and DNA?',
|
77 |
+
'Can you write a haiku about artificial intelligence?'
|
78 |
+
],
|
79 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
80 |
+
).launch()
|
requirements.txt
CHANGED
@@ -1,12 +1,8 @@
|
|
1 |
-
git+https://github.com/huggingface/diffusers.git
|
2 |
git+https://github.com/huggingface/transformers.git
|
3 |
git+https://github.com/huggingface/peft.git
|
4 |
-
--extra-index-url https://download.pytorch.org/whl/
|
5 |
torch
|
6 |
pydantic
|
7 |
-
Pillow
|
8 |
accelerate
|
9 |
-
bitsandbytes
|
10 |
spaces
|
11 |
-
invisible_watermark
|
12 |
|
|
|
|
|
1 |
git+https://github.com/huggingface/transformers.git
|
2 |
git+https://github.com/huggingface/peft.git
|
3 |
+
--extra-index-url https://download.pytorch.org/whl/cu126
|
4 |
torch
|
5 |
pydantic
|
|
|
6 |
accelerate
|
|
|
7 |
spaces
|
|
|
8 |
|