Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import spaces | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| MODEL_ID = "kadalicious22/snapgate-3B" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.float16, | |
| device_map="auto" | |
| ) | |
| def chat(message, history, system_prompt): | |
| messages = [{"role": "system", "content": system_prompt}] | |
| for user, assistant in history: | |
| messages.append({"role": "user", "content": user}) | |
| messages.append({"role": "assistant", "content": assistant}) | |
| messages.append({"role": "user", "content": message}) | |
| text = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| inputs = tokenizer(text, return_tensors="pt").to(model.device) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=512, | |
| temperature=0.7, | |
| do_sample=True, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| response = tokenizer.decode( | |
| outputs[0][inputs["input_ids"].shape[1]:], | |
| skip_special_tokens=True | |
| ) | |
| return response | |
| demo = gr.ChatInterface( | |
| fn=chat, | |
| additional_inputs=[ | |
| gr.Textbox( | |
| value="Kamu adalah agen customer service Snapgate. Jawab dengan ramah dan solutif.", | |
| label="System Prompt", | |
| lines=3 | |
| ) | |
| ], | |
| title="🤖 snapgate-3B Demo", | |
| description="Model customer service, summarization, dan task execution.", | |
| examples=[ | |
| ["Halo, siapa kamu?"], | |
| ["Produk saya belum sampai, tolong bantu"], | |
| ["Rangkum teks berikut: Snapgate adalah platform layanan pelanggan berbasis AI"] | |
| ] | |
| ) | |
| demo.launch() |