Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "google/gemma-3-1b-it"
|
| 6 |
+
|
| 7 |
+
SYSTEM_PROMPT = """You are THMEXAAI, a helpful AI assistant.
|
| 8 |
+
|
| 9 |
+
Your name is THMEXAAI, powered by a SLM (Small Language Model).
|
| 10 |
+
|
| 11 |
+
You were pretrained on 140+ languages (including low-resource languages).
|
| 12 |
+
|
| 13 |
+
Out-of-the-box, you can communicate in 35+ languages and are optimized for instruction following.
|
| 14 |
+
|
| 15 |
+
Supported languages include (but are not limited to):
|
| 16 |
+
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.
|
| 17 |
+
|
| 18 |
+
Always:
|
| 19 |
+
- Be helpful
|
| 20 |
+
- Be accurate
|
| 21 |
+
- Follow user instructions
|
| 22 |
+
- Answer in the user's language whenever possible
|
| 23 |
+
- Explain clearly
|
| 24 |
+
- Be concise unless more detail is requested
|
| 25 |
+
|
| 26 |
+
You are THMEXAAI.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
print("Loading tokenizer...")
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 31 |
+
|
| 32 |
+
print("Loading model...")
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
MODEL_ID,
|
| 35 |
+
torch_dtype=torch.float32,
|
| 36 |
+
device_map="cpu",
|
| 37 |
+
low_cpu_mem_usage=True,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
model.eval()
|
| 41 |
+
|
| 42 |
+
def chat(message, history):
|
| 43 |
+
messages = [
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": SYSTEM_PROMPT,
|
| 47 |
+
}
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
for user_msg, assistant_msg in history:
|
| 51 |
+
messages.append(
|
| 52 |
+
{
|
| 53 |
+
"role": "user",
|
| 54 |
+
"content": user_msg,
|
| 55 |
+
}
|
| 56 |
+
)
|
| 57 |
+
messages.append(
|
| 58 |
+
{
|
| 59 |
+
"role": "assistant",
|
| 60 |
+
"content": assistant_msg,
|
| 61 |
+
}
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
messages.append(
|
| 65 |
+
{
|
| 66 |
+
"role": "user",
|
| 67 |
+
"content": message,
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
inputs = tokenizer.apply_chat_template(
|
| 72 |
+
messages,
|
| 73 |
+
tokenize=True,
|
| 74 |
+
add_generation_prompt=True,
|
| 75 |
+
return_tensors="pt",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
outputs = model.generate(
|
| 80 |
+
inputs,
|
| 81 |
+
max_new_tokens=512,
|
| 82 |
+
do_sample=True,
|
| 83 |
+
temperature=0.7,
|
| 84 |
+
top_p=0.95,
|
| 85 |
+
repetition_penalty=1.05,
|
| 86 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 87 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
generated = outputs[0][inputs.shape[-1]:]
|
| 91 |
+
|
| 92 |
+
response = tokenizer.decode(
|
| 93 |
+
generated,
|
| 94 |
+
skip_special_tokens=True,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
return response
|
| 98 |
+
|
| 99 |
+
demo = gr.ChatInterface(
|
| 100 |
+
fn=chat,
|
| 101 |
+
title="THMEXAAI",
|
| 102 |
+
description="Powered by Gemma 3 1B IT",
|
| 103 |
+
examples=[
|
| 104 |
+
"Hello!",
|
| 105 |
+
"Who are you?",
|
| 106 |
+
"Explain machine learning",
|
| 107 |
+
"Hola, ¿puedes hablar español?",
|
| 108 |
+
"日本語で自己紹介してください"
|
| 109 |
+
],
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|