File size: 1,851 Bytes
c41b93e
 
 
3f5e597
c41b93e
 
 
 
 
249ae02
c41b93e
 
 
3f5e597
c41b93e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import spaces

# Load model and tokenizer
model_id = "LiquidAI/LFM2-2.6B"
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",  # Ensure proper device mapping for zero-gpu
)
tokenizer = AutoTokenizer.from_pretrained(model_id)

@spaces.GPU(duration=120)
def chat_with_model(message, history):
    # Format conversation history
    conversation = []
    for user_msg, assistant_msg in history:
        conversation.append({"role": "user", "content": user_msg})
        conversation.append({"role": "assistant", "content": assistant_msg})
    conversation.append({"role": "user", "content": message})
    
    # Apply chat template
    input_ids = tokenizer.apply_chat_template(
        conversation,
        add_generation_prompt=True,
        return_tensors="pt",
        tokenize=True,
    ).to(model.device)
    
    # Generate response
    output = model.generate(
        input_ids,
        do_sample=True,
        temperature=0.3,
        min_p=0.15,
        repetition_penalty=1.05,
        max_new_tokens=512,
        pad_token_id=tokenizer.eos_token_id,
    )
    
    # Decode only the newly generated tokens, skipping the prompt
    response = tokenizer.decode(output[0][input_ids.shape[-1]:], skip_special_tokens=True)
    
    return response

# Create Gradio interface
iface = gr.ChatInterface(
    fn=chat_with_model,
    title="LFM2-2.6B Chatbot",
    description="A chatbot powered by LiquidAI/LFM2-2.6B. Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder).",
    theme="soft",
    examples=[
        ["What is C. elegans?"],
        ["Write a short story about a robot who discovers music."],
        ["Explain the importance of the transformer architecture in NLP."],
    ],
)

iface.launch()