Spaces:
Sleeping
Sleeping
File size: 6,159 Bytes
1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d 1eabe40 1ebe45d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
import os
from gradio.components import clear_button
import torch
import gradio as gr
import requests
from v1.usta_model import UstaModel
from v1.usta_tokenizer import UstaTokenizer
model, tokenizer, model_status = None, None, "Not Loaded"
def load_model(custom_model_path=None):
try:
u_tokenizer = UstaTokenizer("v1/tokenize.json")
print(f"Tokenizer loaded successfully, vocab size: {len(u_tokenizer.vocab)}")
context_length = 32
vocab_size = len(u_tokenizer.vocab)
embedding_dim = 12
num_heads = 4
num_layers = 8
model = UstaModel(
vocab_size=vocab_size,
embedding_dim=embedding_dim,
num_heads=num_heads,
context_length=context_length,
num_layers=num_layers)
if custom_model_path and os.path.exists(custom_model_path):
model.load_state_dict(torch.load(custom_model_path))
else:
model.load_state_dict(torch.load("v1/u1_model.pth"))
model.eval()
print(f"Model loaded successfully, model parameters: {len(u_tokenizer.vocab)}")
return model, u_tokenizer, "Model Loaded Successfully"
except Exception as e:
return None, None, f"Error Loading Model: {e}"
try:
model, tokenizer, model_status = load_model()
except Exception as e:
print(f"Error loading model: {e}")
model, tokenizer, model_status = None, None, "Error Loading Model"
print(f"Model status: {model_status}")
if model is not None:
print("Model loaded successfully")
def chat_with_model(message, chat_history, max_new_tokens = 20):
try:
tokens = tokenizer.encode(message)
if len(tokens) > 25:
tokens = tokens[-25:]
with torch.no_grad():
actual_max_tokens = min(max_new_tokens,32 - len(tokens))
generated_tokens = model.generate(tokens, max_new_tokens=actual_max_tokens)
response = tokenizer.decode(generated_tokens)
original_message = tokenizer.decode(tokens.tolist())
if response.startswith(original_message):
response = response[len(original_message):]
response = response.replace("<pad>","").replace("<unk>","").strip()
print(f"uzunluk {len(response)}")
if len(response) <= 0:
response = "I am sorry i dont know the answer to that question"
chat_history.append((message, response))
return chat_history,""
except Exception as e:
print(f"Error generating response {e}")
return chat_history, "Error generating response"
def load_model_from_url(custom_model_url):
global model, tokenizer, model_status
try:
headers = {
"Accept":"application/octet-stream",
"User-Agent": "Mozilla5.0 (Windows NT 10.0; Win64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
}
response = requests.get(custom_model_url, headers=headers)
response.raise_for_status()
temp_file = "temp_model_pth"
with open(temp_file,"wb") as f:
f.write(response.content)
model, tokenizer, model_status = load_model(temp_file)
os.remove(temp_file)
return "Model loaded successfully on url"
except Exception as e:
print(f"Error loading model from url {e}")
return "Error loading model from url"
def load_model_from_file(model_file):
global model, tokenizer, model_status
try:
model, tokenizer, model_status=load_model(model_file.name)
return " Model loaded on file"
except Exception as e:
print(f"error loading model on file {e}")
return "Error loading model on file"
with gr.Blocks(title="Usta Model") as demo:
gr.Markdown("# Usta Model")
gr.Markdown(" Chat with the model")
chatbot = gr.Chatbot(height=300)
msg = gr.Textbox(placeholder="Enter your text here...", label="Message")
with gr.Row():
send_button = gr.Button("Send", variant="primary")
clear_button = gr.Button("Clear")
max_new_tokens = gr.Slider(
minimum=1,
maximum=30,
value=20,
step=1,
label="Max New Tokens",
info = "The maximum number of new tokens to generate"
)
gr.Markdown("## LOAD CUSTOM MODEL")
with gr.Row():
custom_model_url = gr.Textbox(
placeholder = "https://github.com/malibayram/llm-from-scratch/raw/refs/heads/main/u_model_4000.pth",
label = "Custom Model url",
scale = 4
)
load_url_button = gr.Button("Load Model", variant="primary",scale=1)
with gr.Row():
model_file = gr.File(
label = "Custom Model File",
file_types = [".pth", ".pt", ".bin"],
)
load_file_button = gr.Button("Load Model", variant="primary")
status = gr.Textbox(
label = "Model Status",
value = model_status,
interactive=False,
)
def send_message(message, chat_history, max_new_tokens):
if not message.strip():
return chat_history, ""
return chat_with_model(message, chat_history, max_new_tokens)
send_button.click(
send_message,
inputs = [msg,chatbot,max_new_tokens],
outputs=[chatbot,msg]
)
msg.submit(
send_message,
inputs=[msg,chatbot,max_new_tokens],
outputs=[chatbot,msg]
)
clear_button.click(lambda: None, None, chatbot, status)
load_url_button.click(
load_model_from_url,
inputs=[custom_model_url],
outputs=[status]
)
load_file_button.click(
load_model_from_file,
inputs=[model_file],
outputs=[status]
)
if __name__ == "__main__":
demo.launch(share=True) |