Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
| 3 |
from mistral_inference.transformer import Transformer
|
| 4 |
from mistral_inference.generate import generate
|
| 5 |
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 6 |
from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage, SystemMessage
|
| 7 |
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def setup_mistral():
|
| 10 |
"""Initialize Mistral model and tokenizer."""
|
| 11 |
-
mistral_models_path =
|
|
|
|
| 12 |
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
|
| 13 |
model = Transformer.from_folder(mistral_models_path)
|
| 14 |
return model, tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def check_custom_responses(message: str) -> str:
|
| 17 |
"""Check for specific patterns and return custom responses."""
|
|
@@ -162,24 +202,6 @@ def is_image_request(message: str) -> bool:
|
|
| 162 |
message_lower = message.lower()
|
| 163 |
return any(trigger in message_lower for trigger in image_triggers)
|
| 164 |
|
| 165 |
-
def generate_image(prompt: str) -> str:
|
| 166 |
-
"""Generate an image using DALLE-4K model."""
|
| 167 |
-
try:
|
| 168 |
-
response = image_client.text_to_image(
|
| 169 |
-
prompt,
|
| 170 |
-
parameters={
|
| 171 |
-
"negative_prompt": "(worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
|
| 172 |
-
"num_inference_steps": 30,
|
| 173 |
-
"guidance_scale": 7.5,
|
| 174 |
-
"sampling_steps": 15,
|
| 175 |
-
"upscaler": "4x-UltraSharp",
|
| 176 |
-
"denoising_strength": 0.5,
|
| 177 |
-
}
|
| 178 |
-
)
|
| 179 |
-
return response
|
| 180 |
-
except Exception as e:
|
| 181 |
-
print(f"Image generation error: {e}")
|
| 182 |
-
return None
|
| 183 |
def create_mistral_messages(history, system_message, current_message):
|
| 184 |
"""Convert chat history to Mistral message format."""
|
| 185 |
messages = []
|
|
@@ -199,7 +221,7 @@ def create_mistral_messages(history, system_message, current_message):
|
|
| 199 |
messages.append(UserMessage(content=current_message))
|
| 200 |
|
| 201 |
return messages
|
| 202 |
-
|
| 203 |
def respond(message, history, system_message, max_tokens=16343, temperature=0.7, top_p=0.95):
|
| 204 |
"""Main response function using Mistral model."""
|
| 205 |
# First check for custom responses
|
|
@@ -214,8 +236,8 @@ def respond(message, history, system_message, max_tokens=16343, temperature=0.7,
|
|
| 214 |
return
|
| 215 |
|
| 216 |
try:
|
| 217 |
-
#
|
| 218 |
-
|
| 219 |
|
| 220 |
# Prepare messages for Mistral
|
| 221 |
mistral_messages = create_mistral_messages(history, system_message, message)
|
|
@@ -224,20 +246,20 @@ def respond(message, history, system_message, max_tokens=16343, temperature=0.7,
|
|
| 224 |
completion_request = ChatCompletionRequest(messages=mistral_messages)
|
| 225 |
|
| 226 |
# Encode the request
|
| 227 |
-
tokens =
|
| 228 |
|
| 229 |
# Generate response
|
| 230 |
out_tokens, _ = generate(
|
| 231 |
[tokens],
|
| 232 |
-
|
| 233 |
max_tokens=max_tokens,
|
| 234 |
temperature=temperature,
|
| 235 |
top_p=top_p,
|
| 236 |
-
eos_id=
|
| 237 |
)
|
| 238 |
|
| 239 |
# Decode and yield response
|
| 240 |
-
response =
|
| 241 |
yield response
|
| 242 |
|
| 243 |
except Exception as e:
|
|
@@ -254,38 +276,48 @@ body, .gradio-container {
|
|
| 254 |
# System message
|
| 255 |
system_message = """Xylaria (v1.2.9) is an AI assistant developed by Sk Md Saad Amin, designed to provide efficient, practical support in various domains with adaptable communication."""
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pathlib import Path
|
| 3 |
+
import os
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
from mistral_inference.transformer import Transformer
|
| 6 |
from mistral_inference.generate import generate
|
| 7 |
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 8 |
from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage, SystemMessage
|
| 9 |
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
| 10 |
|
| 11 |
+
def download_mistral_model():
|
| 12 |
+
"""Download Mistral model if not already present."""
|
| 13 |
+
print("Checking for Mistral model...")
|
| 14 |
+
mistral_models_path = Path.home().joinpath('mistral_models', 'Nemo-Instruct')
|
| 15 |
+
|
| 16 |
+
# Check if model files already exist
|
| 17 |
+
required_files = ["params.json", "consolidated.safetensors", "tekken.json"]
|
| 18 |
+
files_exist = all(
|
| 19 |
+
mistral_models_path.joinpath(file).exists()
|
| 20 |
+
for file in required_files
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if not files_exist:
|
| 24 |
+
print("Downloading Mistral model (this may take a while)...")
|
| 25 |
+
mistral_models_path.mkdir(parents=True, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
snapshot_download(
|
| 28 |
+
repo_id="mistralai/Mistral-Nemo-Instruct-2407",
|
| 29 |
+
allow_patterns=required_files,
|
| 30 |
+
local_dir=mistral_models_path
|
| 31 |
+
)
|
| 32 |
+
print("Model downloaded successfully!")
|
| 33 |
+
else:
|
| 34 |
+
print("Mistral model already downloaded.")
|
| 35 |
+
|
| 36 |
+
return mistral_models_path
|
| 37 |
+
|
| 38 |
def setup_mistral():
|
| 39 |
"""Initialize Mistral model and tokenizer."""
|
| 40 |
+
mistral_models_path = download_mistral_model()
|
| 41 |
+
print("Initializing Mistral model and tokenizer...")
|
| 42 |
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
|
| 43 |
model = Transformer.from_folder(mistral_models_path)
|
| 44 |
return model, tokenizer
|
| 45 |
+
|
| 46 |
+
# Global variables for model and tokenizer
|
| 47 |
+
global_model = None
|
| 48 |
+
global_tokenizer = None
|
| 49 |
+
|
| 50 |
+
def initialize_globals():
|
| 51 |
+
"""Initialize global model and tokenizer if not already done."""
|
| 52 |
+
global global_model, global_tokenizer
|
| 53 |
+
if global_model is None or global_tokenizer is None:
|
| 54 |
+
global_model, global_tokenizer = setup_mistral()
|
| 55 |
|
| 56 |
def check_custom_responses(message: str) -> str:
|
| 57 |
"""Check for specific patterns and return custom responses."""
|
|
|
|
| 202 |
message_lower = message.lower()
|
| 203 |
return any(trigger in message_lower for trigger in image_triggers)
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
def create_mistral_messages(history, system_message, current_message):
|
| 206 |
"""Convert chat history to Mistral message format."""
|
| 207 |
messages = []
|
|
|
|
| 221 |
messages.append(UserMessage(content=current_message))
|
| 222 |
|
| 223 |
return messages
|
| 224 |
+
|
| 225 |
def respond(message, history, system_message, max_tokens=16343, temperature=0.7, top_p=0.95):
|
| 226 |
"""Main response function using Mistral model."""
|
| 227 |
# First check for custom responses
|
|
|
|
| 236 |
return
|
| 237 |
|
| 238 |
try:
|
| 239 |
+
# Initialize global model and tokenizer if needed
|
| 240 |
+
initialize_globals()
|
| 241 |
|
| 242 |
# Prepare messages for Mistral
|
| 243 |
mistral_messages = create_mistral_messages(history, system_message, message)
|
|
|
|
| 246 |
completion_request = ChatCompletionRequest(messages=mistral_messages)
|
| 247 |
|
| 248 |
# Encode the request
|
| 249 |
+
tokens = global_tokenizer.encode_chat_completion(completion_request).tokens
|
| 250 |
|
| 251 |
# Generate response
|
| 252 |
out_tokens, _ = generate(
|
| 253 |
[tokens],
|
| 254 |
+
global_model,
|
| 255 |
max_tokens=max_tokens,
|
| 256 |
temperature=temperature,
|
| 257 |
top_p=top_p,
|
| 258 |
+
eos_id=global_tokenizer.instruct_tokenizer.tokenizer.eos_id
|
| 259 |
)
|
| 260 |
|
| 261 |
# Decode and yield response
|
| 262 |
+
response = global_tokenizer.decode(out_tokens[0])
|
| 263 |
yield response
|
| 264 |
|
| 265 |
except Exception as e:
|
|
|
|
| 276 |
# System message
|
| 277 |
system_message = """Xylaria (v1.2.9) is an AI assistant developed by Sk Md Saad Amin, designed to provide efficient, practical support in various domains with adaptable communication."""
|
| 278 |
|
| 279 |
+
def main():
|
| 280 |
+
print("Starting Mistral Chat Interface...")
|
| 281 |
+
print("Initializing model (this may take a few minutes on first run)...")
|
| 282 |
+
|
| 283 |
+
# Initialize model and tokenizer at startup
|
| 284 |
+
initialize_globals()
|
| 285 |
+
|
| 286 |
+
# Create Gradio interface
|
| 287 |
+
demo = gr.ChatInterface(
|
| 288 |
+
respond,
|
| 289 |
+
additional_inputs=[
|
| 290 |
+
gr.Textbox(
|
| 291 |
+
value=system_message,
|
| 292 |
+
visible=False,
|
| 293 |
+
),
|
| 294 |
+
gr.Slider(
|
| 295 |
+
minimum=1,
|
| 296 |
+
maximum=16343,
|
| 297 |
+
value=16343,
|
| 298 |
+
step=1,
|
| 299 |
+
label="Max new tokens"
|
| 300 |
+
),
|
| 301 |
+
gr.Slider(
|
| 302 |
+
minimum=0.1,
|
| 303 |
+
maximum=4.0,
|
| 304 |
+
value=0.7,
|
| 305 |
+
step=0.1,
|
| 306 |
+
label="Temperature"
|
| 307 |
+
),
|
| 308 |
+
gr.Slider(
|
| 309 |
+
minimum=0.1,
|
| 310 |
+
maximum=1.0,
|
| 311 |
+
value=0.95,
|
| 312 |
+
step=0.05,
|
| 313 |
+
label="Top-p (nucleus sampling)"
|
| 314 |
+
),
|
| 315 |
+
],
|
| 316 |
+
css=custom_css
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
print("Launch successful! Interface is ready to use.")
|
| 320 |
+
demo.launch()
|
| 321 |
|
| 322 |
if __name__ == "__main__":
|
| 323 |
+
main()
|