Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "Smilyai-labs/Sam-reason-S2.1" # or your local path
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Chat function
|
12 |
+
def chat_with_model(user_input, history):
|
13 |
+
if history is None:
|
14 |
+
history = []
|
15 |
+
|
16 |
+
# Build conversation string for context
|
17 |
+
conversation = ""
|
18 |
+
for msg in history:
|
19 |
+
conversation += f"User: {msg['content']}\nSam:"
|
20 |
+
if msg['role'] == "assistant":
|
21 |
+
conversation += f" {msg['content']}\n"
|
22 |
+
|
23 |
+
conversation += f"User: {user_input}\nSam:"
|
24 |
+
|
25 |
+
# Encode and generate
|
26 |
+
inputs = tokenizer(conversation, return_tensors="pt", truncation=True, max_length=1024)
|
27 |
+
with torch.no_grad():
|
28 |
+
outputs = model.generate(
|
29 |
+
inputs.input_ids,
|
30 |
+
max_new_tokens=150,
|
31 |
+
do_sample=True,
|
32 |
+
top_k=50,
|
33 |
+
top_p=0.95,
|
34 |
+
temperature=0.7,
|
35 |
+
pad_token_id=tokenizer.eos_token_id
|
36 |
+
)
|
37 |
+
|
38 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
response_text = decoded.split("Sam:")[-1].strip()
|
40 |
+
|
41 |
+
# Add new message to chat history
|
42 |
+
history.append({"role": "user", "content": user_input})
|
43 |
+
history.append({"role": "assistant", "content": response_text})
|
44 |
+
|
45 |
+
return "", history
|
46 |
+
|
47 |
+
# Gradio UI
|
48 |
+
def create_chatbot_interface():
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# 💬 Chat with **Sam** (SmilyAI's Reasoning LLM 2.1 generation)")
|
51 |
+
chatbot = gr.Chatbot(label="Chat", type="messages")
|
52 |
+
user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
|
53 |
+
send_btn = gr.Button("Send")
|
54 |
+
|
55 |
+
send_btn.click(
|
56 |
+
chat_with_model,
|
57 |
+
inputs=[user_input, chatbot],
|
58 |
+
outputs=[user_input, chatbot]
|
59 |
+
)
|
60 |
+
|
61 |
+
return demo
|
62 |
+
|
63 |
+
# Launch
|
64 |
+
demo = create_chatbot_interface()
|
65 |
+
demo.launch()
|
66 |
+
|