Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_id = "LiquidAI/LFM2-2.6B"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
device_map="auto",
|
| 10 |
+
torch_dtype="bfloat16",
|
| 11 |
+
)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 13 |
+
|
| 14 |
+
def chat_with_model(message, history):
|
| 15 |
+
# Format conversation history
|
| 16 |
+
conversation = []
|
| 17 |
+
for user_msg, assistant_msg in history:
|
| 18 |
+
conversation.append({"role": "user", "content": user_msg})
|
| 19 |
+
conversation.append({"role": "assistant", "content": assistant_msg})
|
| 20 |
+
conversation.append({"role": "user", "content": message})
|
| 21 |
+
|
| 22 |
+
# Apply chat template
|
| 23 |
+
input_ids = tokenizer.apply_chat_template(
|
| 24 |
+
conversation,
|
| 25 |
+
add_generation_prompt=True,
|
| 26 |
+
return_tensors="pt",
|
| 27 |
+
tokenize=True,
|
| 28 |
+
).to(model.device)
|
| 29 |
+
|
| 30 |
+
# Generate response
|
| 31 |
+
output = model.generate(
|
| 32 |
+
input_ids,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
temperature=0.3,
|
| 35 |
+
min_p=0.15,
|
| 36 |
+
repetition_penalty=1.05,
|
| 37 |
+
max_new_tokens=512,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Decode only the newly generated tokens, skipping the prompt
|
| 42 |
+
response = tokenizer.decode(output[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
# Create Gradio interface
|
| 47 |
+
iface = gr.ChatInterface(
|
| 48 |
+
fn=chat_with_model,
|
| 49 |
+
title="LFM2-2.6B Chatbot",
|
| 50 |
+
description="A chatbot powered by LiquidAI/LFM2-2.6B. Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder).",
|
| 51 |
+
theme="soft",
|
| 52 |
+
examples=[
|
| 53 |
+
["What is C. elegans?"],
|
| 54 |
+
["Write a short story about a robot who discovers music."],
|
| 55 |
+
["Explain the importance of the transformer architecture in NLP."],
|
| 56 |
+
],
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
iface.launch()
|