| import torch |
| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
| MODEL_ID = "google/gemma-3-1b-it" |
|
|
| SYSTEM_PROMPT = """You are THMEXAAI, a helpful AI assistant. |
| |
| Your name is THMEXAAI, powered by a SLM (Small Language Model). |
| |
| You were pretrained on 140+ languages (including low-resource languages). |
| |
| Out-of-the-box, you can communicate in 35+ languages and are optimized for instruction following. |
| |
| Supported languages include (but are not limited to): |
| English, Spanish, French, German, Italian, Portuguese, Dutch, Swedish, Norwegian, Danish, Finnish, Polish, Czech, Slovak, Hungarian, Romanian, Bulgarian, Greek, Turkish, Arabic, Hebrew, Russian, Ukrainian, Hindi, Bengali, Urdu, Tamil, Telugu, Indonesian, Malay, Vietnamese, Thai, Chinese, Japanese and Korean. |
| |
| Always: |
| - Be helpful |
| - Be accurate |
| - Follow user instructions |
| - Answer in the user's language whenever possible |
| - Explain clearly |
| - Be concise unless more detail is requested |
| |
| You are THMEXAAI. |
| """ |
|
|
| print("Loading tokenizer...") |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
|
|
| print("Loading model...") |
| model = AutoModelForCausalLM.from_pretrained( |
| MODEL_ID, |
| torch_dtype=torch.float32, |
| device_map="cpu", |
| low_cpu_mem_usage=True, |
| ) |
|
|
| model.eval() |
|
|
| def chat(message, history): |
| messages = [ |
| { |
| "role": "system", |
| "content": SYSTEM_PROMPT, |
| } |
| ] |
|
|
| for user_msg, assistant_msg in history: |
| messages.append( |
| { |
| "role": "user", |
| "content": user_msg, |
| } |
| ) |
| messages.append( |
| { |
| "role": "assistant", |
| "content": assistant_msg, |
| } |
| ) |
|
|
| messages.append( |
| { |
| "role": "user", |
| "content": message, |
| } |
| ) |
|
|
| inputs = tokenizer.apply_chat_template( |
| messages, |
| tokenize=True, |
| add_generation_prompt=True, |
| return_tensors="pt", |
| ) |
|
|
| with torch.no_grad(): |
| outputs = model.generate( |
| inputs, |
| max_new_tokens=512, |
| do_sample=True, |
| temperature=0.7, |
| top_p=0.95, |
| repetition_penalty=1.05, |
| pad_token_id=tokenizer.eos_token_id, |
| eos_token_id=tokenizer.eos_token_id, |
| ) |
|
|
| generated = outputs[0][inputs.shape[-1]:] |
|
|
| response = tokenizer.decode( |
| generated, |
| skip_special_tokens=True, |
| ) |
|
|
| return response |
|
|
| demo = gr.ChatInterface( |
| fn=chat, |
| title="THMEXAAI", |
| description="Powered by Gemma 3 1B IT", |
| examples=[ |
| "Hello!", |
| "Who are you?", |
| "Explain machine learning", |
| "Hola, ¿puedes hablar español?", |
| "日本語で自己紹介してください" |
| ], |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |