Update app.py
Browse files
app.py
CHANGED
|
@@ -1,197 +1,78 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
from transformers import AutoTokenizer
|
| 7 |
-
import gc
|
| 8 |
|
| 9 |
-
|
| 10 |
-
st.set_page_config(
|
| 11 |
-
page_title="๐ Automotive SLM Assistant",
|
| 12 |
-
page_icon="๐",
|
| 13 |
-
layout="wide"
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
logging.getLogger('streamlit').setLevel(logging.ERROR)
|
| 19 |
-
logging.getLogger('transformers').setLevel(logging.ERROR)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
device = torch.device('cpu')
|
| 28 |
-
|
| 29 |
-
# Load tokenizer first
|
| 30 |
-
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 31 |
-
if tokenizer.pad_token is None:
|
| 32 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 33 |
-
|
| 34 |
-
# Simplified model loading for HF Spaces
|
| 35 |
-
# You would upload your model files to the HF Spaces repo
|
| 36 |
-
if os.path.exists("model.pt"):
|
| 37 |
-
checkpoint = torch.load("model.pt", map_location=device)
|
| 38 |
-
|
| 39 |
-
# Create simple config if not in checkpoint
|
| 40 |
-
config = {
|
| 41 |
-
'd_model': 256,
|
| 42 |
-
'n_layer': 4,
|
| 43 |
-
'n_head': 4,
|
| 44 |
-
'vocab_size': 50257,
|
| 45 |
-
'n_positions': 256,
|
| 46 |
-
'use_moe': True,
|
| 47 |
-
'n_experts': 4
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
# Use simplified model class for HF Spaces
|
| 51 |
-
model = SimpleAutomotiveModel(config)
|
| 52 |
-
|
| 53 |
-
if 'model_state_dict' in checkpoint:
|
| 54 |
-
model.load_state_dict(checkpoint['model_state_dict'])
|
| 55 |
-
|
| 56 |
-
model.eval()
|
| 57 |
-
|
| 58 |
-
return model, tokenizer, config
|
| 59 |
-
else:
|
| 60 |
-
st.error("Model file not found. Please upload your model.pt to the repository.")
|
| 61 |
-
return None, None, None
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
st.error(f"Error loading model: {e}")
|
| 65 |
-
return None, None, None
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
self.layers = torch.nn.ModuleList([
|
| 75 |
-
torch.nn.TransformerEncoderLayer(
|
| 76 |
-
d_model=config['d_model'],
|
| 77 |
-
nhead=config['n_head'],
|
| 78 |
-
batch_first=True
|
| 79 |
-
) for _ in range(config['n_layer'])
|
| 80 |
-
])
|
| 81 |
-
self.ln_f = torch.nn.LayerNorm(config['d_model'])
|
| 82 |
-
self.lm_head = torch.nn.Linear(config['d_model'], config['vocab_size'], bias=False)
|
| 83 |
-
|
| 84 |
-
def forward(self, input_ids):
|
| 85 |
-
x = self.embeddings(input_ids)
|
| 86 |
-
for layer in self.layers:
|
| 87 |
-
x = layer(x)
|
| 88 |
-
x = self.ln_f(x)
|
| 89 |
-
return {"logits": self.lm_head(x)}
|
| 90 |
-
|
| 91 |
-
def generate(self, input_ids, max_new_tokens=50, temperature=0.8, **kwargs):
|
| 92 |
-
"""Simple generation for HF Spaces"""
|
| 93 |
-
device = input_ids.device
|
| 94 |
-
generated = input_ids.clone()
|
| 95 |
-
|
| 96 |
-
for _ in range(max_new_tokens):
|
| 97 |
-
with torch.no_grad():
|
| 98 |
-
outputs = self.forward(generated)
|
| 99 |
-
logits = outputs["logits"][:, -1, :] / temperature
|
| 100 |
-
probs = torch.softmax(logits, dim=-1)
|
| 101 |
-
next_token = torch.multinomial(probs, 1)
|
| 102 |
-
generated = torch.cat([generated, next_token], dim=1)
|
| 103 |
-
|
| 104 |
-
# Simple stopping condition
|
| 105 |
-
if next_token.item() == 50256: # EOS token
|
| 106 |
-
break
|
| 107 |
-
|
| 108 |
-
return generated
|
| 109 |
|
| 110 |
-
def
|
| 111 |
-
"""Generate response optimized for HF Spaces"""
|
| 112 |
try:
|
| 113 |
-
|
| 114 |
-
inputs = tokenizer(prompt, return_tensors="pt", max_length=200, truncation=True)
|
| 115 |
-
|
| 116 |
-
# Generate
|
| 117 |
-
with torch.no_grad():
|
| 118 |
-
outputs = model.generate(
|
| 119 |
-
inputs['input_ids'],
|
| 120 |
-
max_new_tokens=max_tokens,
|
| 121 |
-
temperature=temperature,
|
| 122 |
-
pad_token_id=tokenizer.pad_token_id
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Decode
|
| 126 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 127 |
-
|
| 128 |
-
# Remove original prompt
|
| 129 |
-
if response.startswith(prompt):
|
| 130 |
-
response = response[len(prompt):].strip()
|
| 131 |
-
|
| 132 |
-
return response if response else "I apologize, but I couldn't generate a proper response. Please try rephrasing your question."
|
| 133 |
-
|
| 134 |
except Exception as e:
|
| 135 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# Display chat history
|
| 171 |
-
for message in st.session_state.messages:
|
| 172 |
-
with st.chat_message(message["role"]):
|
| 173 |
-
st.markdown(message["content"])
|
| 174 |
-
|
| 175 |
-
# Chat input
|
| 176 |
-
if prompt := st.chat_input("Ask me about automotive topics..."):
|
| 177 |
-
# Add user message
|
| 178 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 179 |
-
with st.chat_message("user"):
|
| 180 |
-
st.markdown(prompt)
|
| 181 |
-
|
| 182 |
-
# Generate and display response
|
| 183 |
-
with st.chat_message("assistant"):
|
| 184 |
-
with st.spinner("๐ค Thinking..."):
|
| 185 |
-
response = generate_response(model, tokenizer, prompt, max_tokens, temperature)
|
| 186 |
-
st.markdown(response)
|
| 187 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 188 |
-
|
| 189 |
-
# Cleanup for HF Spaces memory management
|
| 190 |
-
if len(st.session_state.messages) > 20: # Keep last 20 messages
|
| 191 |
-
st.session_state.messages = st.session_state.messages[-20:]
|
| 192 |
-
|
| 193 |
-
# Force garbage collection
|
| 194 |
-
gc.collect()
|
| 195 |
|
| 196 |
if __name__ == "__main__":
|
| 197 |
-
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from src.model_manager import ModelManager
|
| 4 |
+
from src.inference_engine import InferenceEngine
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
ASSETS_DIR = "assets"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize once
|
| 9 |
+
manager = ModelManager(os.path.join(ASSETS_DIR, "models"))
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def list_models():
|
| 12 |
+
models = manager.get_available_models()
|
| 13 |
+
return models
|
| 14 |
+
|
| 15 |
+
# Cache loaded engines by model name
|
| 16 |
+
_engines = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def load_engine(model_name):
|
| 19 |
+
if model_name in _engines:
|
| 20 |
+
return _engines[model_name]
|
| 21 |
+
model, tokenizer, config = manager.load_model(model_name)
|
| 22 |
+
engine = InferenceEngine(model, tokenizer, config)
|
| 23 |
+
_engines[model_name] = engine
|
| 24 |
+
return engine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def chat_fn(message, history, model_name, max_tokens, temperature, top_p, top_k):
|
|
|
|
| 27 |
try:
|
| 28 |
+
engine = load_engine(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
+
return history + [[message, f"Error loading model: {e}"]]
|
| 31 |
+
reply = engine.generate_response(
|
| 32 |
+
message,
|
| 33 |
+
max_tokens=max_tokens,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
top_k=top_k
|
| 37 |
+
)
|
| 38 |
+
history = history + [[message, reply]]
|
| 39 |
+
return history
|
| 40 |
+
|
| 41 |
+
def clear_chat():
|
| 42 |
+
return []
|
| 43 |
|
| 44 |
+
with gr.Blocks(title="Automotive SLM Chatbot") as demo:
|
| 45 |
+
gr.Markdown("# ๐ Automotive SLM Chatbot (Gradio)")
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column(scale=3):
|
| 48 |
+
chatbot = gr.Chatbot(height=450, label="Chat")
|
| 49 |
+
msg = gr.Textbox(placeholder="Ask about automotive topics...", label="Your message")
|
| 50 |
+
with gr.Row():
|
| 51 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 52 |
+
clear_btn = gr.Button("Clear")
|
| 53 |
+
with gr.Column(scale=2):
|
| 54 |
+
gr.Markdown("### Model settings")
|
| 55 |
+
available = list_models()
|
| 56 |
+
model_dropdown = gr.Dropdown(
|
| 57 |
+
choices=available, value=available[0] if available else None, label="Model"
|
| 58 |
+
)
|
| 59 |
+
max_tokens = gr.Slider(10, 256, value=64, step=1, label="Max tokens")
|
| 60 |
+
temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Temperature")
|
| 61 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 62 |
+
top_k = gr.Slider(1, 100, value=50, step=1, label="Top-k")
|
| 63 |
+
gr.Markdown("Tip: lower temperature for more deterministic answers.")
|
| 64 |
+
# Events
|
| 65 |
+
send_evt = send_btn.click(
|
| 66 |
+
fn=chat_fn,
|
| 67 |
+
inputs=[msg, chatbot, model_dropdown, max_tokens, temperature, top_p, top_k],
|
| 68 |
+
outputs=[chatbot]
|
| 69 |
+
)
|
| 70 |
+
msg.submit(
|
| 71 |
+
fn=chat_fn,
|
| 72 |
+
inputs=[msg, chatbot, model_dropdown, max_tokens, temperature, top_p, top_k],
|
| 73 |
+
outputs=[chatbot]
|
| 74 |
+
)
|
| 75 |
+
clear_btn.click(clear_chat, inputs=None, outputs=[chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
+
demo.launch()
|