Spaces:
Paused
Paused
Init
Browse files
README.md
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title: MamayLM
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
-
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: INSAIT-Institute/MamayLM-Gemma-3-12B-IT-v1.0
|
| 3 |
+
emoji: 🇺🇦
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
app_file: app.py
|
| 9 |
+
short_description: Chat with INSAIT-Institute/MamayLM-Gemma-3-12B-IT-v1.0
|
| 10 |
+
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from threading import Thread
|
| 3 |
+
from typing import Iterator
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
+
import subprocess
|
| 10 |
+
|
| 11 |
+
subprocess.run(
|
| 12 |
+
"pip install flash-attn --no-build-isolation",
|
| 13 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
| 14 |
+
shell=True,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
DESCRIPTION = """\
|
| 19 |
+
# INSAIT-Institute/MamayLM-Gemma-3-12B-IT-v1.0
|
| 20 |
+
|
| 21 |
+
[🪪 **Model card**](https://huggingface.co/INSAIT-Institute/MamayLM-Gemma-3-12B-IT-v1.0)
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 25 |
+
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 26 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 27 |
+
|
| 28 |
+
model_id = "INSAIT-Institute/MamayLM-Gemma-3-12B-IT-v1.0"
|
| 29 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 30 |
+
|
| 31 |
+
attn_impl = "flash_attention_2" if torch.cuda.is_available() else "eager"
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
model_id,
|
| 34 |
+
device_map="auto",
|
| 35 |
+
torch_dtype=torch.bfloat16,
|
| 36 |
+
attn_implementation=attn_impl,
|
| 37 |
+
)
|
| 38 |
+
# model.config.sliding_window = 4096
|
| 39 |
+
model.eval()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@spaces.GPU(duration=90)
|
| 43 |
+
def generate(
|
| 44 |
+
message: str,
|
| 45 |
+
chat_history: list[tuple[str, str]],
|
| 46 |
+
system_message: str = "",
|
| 47 |
+
max_new_tokens: int = 1024,
|
| 48 |
+
temperature: float = 0.001,
|
| 49 |
+
top_p: float = 1.0,
|
| 50 |
+
top_k: int = 50,
|
| 51 |
+
repetition_penalty: float = 1.0,
|
| 52 |
+
) -> Iterator[str]:
|
| 53 |
+
conversation = [{"role": "system", "content": system_message}]
|
| 54 |
+
for user, assistant in chat_history:
|
| 55 |
+
conversation.extend(
|
| 56 |
+
[
|
| 57 |
+
{"role": "user", "content": user},
|
| 58 |
+
{"role": "assistant", "content": assistant},
|
| 59 |
+
]
|
| 60 |
+
)
|
| 61 |
+
conversation.append({"role": "user", "content": message})
|
| 62 |
+
|
| 63 |
+
input_ids = tokenizer.apply_chat_template(
|
| 64 |
+
conversation, add_generation_prompt=True, return_tensors="pt"
|
| 65 |
+
)
|
| 66 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 67 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 68 |
+
gr.Warning(
|
| 69 |
+
f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens."
|
| 70 |
+
)
|
| 71 |
+
input_ids = input_ids.to(model.device)
|
| 72 |
+
|
| 73 |
+
streamer = TextIteratorStreamer(
|
| 74 |
+
tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True
|
| 75 |
+
)
|
| 76 |
+
generate_kwargs = dict(
|
| 77 |
+
{"input_ids": input_ids},
|
| 78 |
+
streamer=streamer,
|
| 79 |
+
max_new_tokens=max_new_tokens,
|
| 80 |
+
do_sample=True,
|
| 81 |
+
eos_token_id=[1, 107],
|
| 82 |
+
top_p=top_p,
|
| 83 |
+
top_k=top_k,
|
| 84 |
+
temperature=temperature,
|
| 85 |
+
num_beams=1,
|
| 86 |
+
repetition_penalty=repetition_penalty,
|
| 87 |
+
disable_compile=True, # https://ai.google.dev/gemma/docs/core/huggingface_text_full_finetune#test_model_inference
|
| 88 |
+
)
|
| 89 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 90 |
+
t.start()
|
| 91 |
+
|
| 92 |
+
outputs = []
|
| 93 |
+
for text in streamer:
|
| 94 |
+
outputs.append(text)
|
| 95 |
+
yield "".join(outputs)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
chat_interface = gr.ChatInterface(
|
| 99 |
+
fn=generate,
|
| 100 |
+
additional_inputs=[
|
| 101 |
+
gr.Textbox(
|
| 102 |
+
value="",
|
| 103 |
+
label="System message",
|
| 104 |
+
render=False,
|
| 105 |
+
),
|
| 106 |
+
gr.Slider(
|
| 107 |
+
label="Max new tokens",
|
| 108 |
+
minimum=1,
|
| 109 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
| 110 |
+
step=1,
|
| 111 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
| 112 |
+
),
|
| 113 |
+
gr.Slider(
|
| 114 |
+
label="Temperature",
|
| 115 |
+
minimum=0,
|
| 116 |
+
maximum=4.0,
|
| 117 |
+
step=0.1,
|
| 118 |
+
value=0.1, # default from https://huggingface.co/docs/transformers/en/main_classes/text_generation
|
| 119 |
+
),
|
| 120 |
+
gr.Slider(
|
| 121 |
+
label="Top-p (nucleus sampling)",
|
| 122 |
+
minimum=0.05,
|
| 123 |
+
maximum=1.0,
|
| 124 |
+
step=0.05,
|
| 125 |
+
value=1, # from https://huggingface.co/google/gemma-3-270m-it/blob/main/generation_config.json
|
| 126 |
+
),
|
| 127 |
+
gr.Slider(
|
| 128 |
+
label="Top-k",
|
| 129 |
+
minimum=1,
|
| 130 |
+
maximum=1000,
|
| 131 |
+
step=1,
|
| 132 |
+
value=25, # from https://huggingface.co/google/gemma-3-270m-it/blob/main/generation_config.json
|
| 133 |
+
),
|
| 134 |
+
gr.Slider(
|
| 135 |
+
label="Repetition penalty",
|
| 136 |
+
minimum=1.0,
|
| 137 |
+
maximum=2.0,
|
| 138 |
+
step=0.05,
|
| 139 |
+
value=1.1, # default from https://huggingface.co/docs/transformers/en/main_classes/text_generation
|
| 140 |
+
),
|
| 141 |
+
],
|
| 142 |
+
stop_btn=None,
|
| 143 |
+
examples=[
|
| 144 |
+
["Привіт! Як справи?"],
|
| 145 |
+
[
|
| 146 |
+
"Плюси та мінуси довгострокових стосунків. Маркований список із максимум 3 перевагами та 3 недоліками, стисло."
|
| 147 |
+
],
|
| 148 |
+
["Скільки годин потрібно людині, щоб з'їсти гелікоптер?"],
|
| 149 |
+
["Як відкрити файл JSON у Python?"],
|
| 150 |
+
[
|
| 151 |
+
"Створіть маркований список переваг і недоліків життя в Сан-Франциско. Максимум 2 переваги та 2 недоліки."
|
| 152 |
+
],
|
| 153 |
+
["Придумай коротке оповідання з тваринами про цінність дружби."],
|
| 154 |
+
["Чи можеш ти коротко пояснити, що таке мова програмування Python?"],
|
| 155 |
+
[
|
| 156 |
+
"Напишіть статтю на 100 слів на тему 'Переваги відкритого коду в дослідженнях ШІ'."
|
| 157 |
+
],
|
| 158 |
+
],
|
| 159 |
+
cache_examples=False,
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
with gr.Blocks(css="style.css", fill_height=True, theme="soft") as demo:
|
| 163 |
+
gr.Markdown(DESCRIPTION)
|
| 164 |
+
gr.DuplicateButton(
|
| 165 |
+
value="Duplicate Space for private use", elem_id="duplicate-button"
|
| 166 |
+
)
|
| 167 |
+
chat_interface.render()
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
transformers>=4.55.2
|
style.css
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
h1 {
|
| 2 |
+
text-align: center;
|
| 3 |
+
display: block;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
#duplicate-button {
|
| 7 |
+
margin: auto;
|
| 8 |
+
color: #fff;
|
| 9 |
+
background: #1565c0;
|
| 10 |
+
border-radius: 100vh;
|
| 11 |
+
}
|