| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| model_name = "lamapi/next-1b" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| torch_dtype=torch.float16, | |
| device_map="auto" | |
| ) | |
| model.eval() | |
| def chat(message, history): | |
| history = history or [] | |
| prompt = "".join([f"User: {u}\nBot: {b}\n" for u,b in history]) | |
| prompt += f"User: {message}\nBot:" | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| with torch.inference_mode(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=150, | |
| do_sample=True, | |
| temperature=0.8, | |
| top_p=0.9 | |
| ) | |
| text = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| reply = text.split("Bot:")[-1].strip() | |
| history.append((message, reply)) | |
| return reply, history | |
| iface = gr.ChatInterface( | |
| fn=chat, | |
| title="Next-1B Chatbot ⚡", | |
| description="Lamapi model ile hızlı, kuyruksuz chat!", | |
| ).launch(share=True, server_name="0.0.0.0", server_port=7860, concurrency_count=4, queue=False) | |