Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,30 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
|
| 5 |
-
import time
|
| 6 |
-
import torch.quantization
|
| 7 |
-
|
| 8 |
-
# Directly assign your Hugging Face token here
|
| 9 |
-
hf_token = "your_hugging_face_api_token"
|
| 10 |
-
|
| 11 |
-
# Log in to Hugging Face
|
| 12 |
-
login(token=hf_token)
|
| 13 |
-
|
| 14 |
-
# Load the Mixtral-8x7B-Instruct model and tokenizer with authorization header
|
| 15 |
-
model_name = 'mistralai/Mistral-7B-Instruct-v0.3'
|
| 16 |
-
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 17 |
-
|
| 18 |
-
# Ensure sentencepiece is installed
|
| 19 |
-
try:
|
| 20 |
-
import sentencepiece
|
| 21 |
-
except ImportError:
|
| 22 |
-
raise ImportError("The sentencepiece library is required for this tokenizer. Please install it with `pip install sentencepiece`.")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
|
| 27 |
-
# Load tokenizer
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 29 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
quantized_model =
|
| 33 |
|
| 34 |
-
# Check if a GPU is available and
|
| 35 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
quantized_model.to(device)
|
| 37 |
|
| 38 |
-
# Measure time for loading tokenizer, model, and quantization
|
| 39 |
-
loading_time = time.time() - start_time
|
| 40 |
-
print(f"Time taken to load tokenizer, model, and quantize: {loading_time:.2f} seconds")
|
| 41 |
-
|
| 42 |
# Example text input
|
| 43 |
text_input = "How did Tesla perform in Q1 2024?"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
inference_start_time = time.time()
|
| 47 |
-
|
| 48 |
-
# Tokenize the input text
|
| 49 |
inputs = tokenizer(text_input, return_tensors="pt").to(device)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
tokenization_time = time.time() - inference_start_time
|
| 53 |
-
|
| 54 |
-
# Generate a response
|
| 55 |
outputs = quantized_model.generate(**inputs, max_length=150, do_sample=False)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
inference_time = time.time() - inference_start_time
|
| 59 |
-
print(f"Time taken for inference with quantized model: {inference_time:.2f} seconds")
|
| 60 |
-
|
| 61 |
-
# Decode the generated tokens to a readable string
|
| 62 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
|
| 64 |
-
# Print
|
| 65 |
-
print(f"Generated response: {response}")
|
| 66 |
-
|
| 67 |
-
# Total execution time
|
| 68 |
-
total_time = time.time() - start_time
|
| 69 |
-
print(f"Total execution time with quantized model: {total_time:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Path to the locally saved quantized model directory
|
| 5 |
+
model_path = '/path/to/your/quantized_model_directory'
|
| 6 |
|
| 7 |
+
# Load tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 9 |
|
| 10 |
+
# Load quantized model
|
| 11 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 12 |
|
| 13 |
+
# Check if a GPU is available and move model to GPU if available
|
| 14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
quantized_model.to(device)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Example text input
|
| 18 |
text_input = "How did Tesla perform in Q1 2024?"
|
| 19 |
|
| 20 |
+
# Tokenize input
|
|
|
|
|
|
|
|
|
|
| 21 |
inputs = tokenizer(text_input, return_tensors="pt").to(device)
|
| 22 |
|
| 23 |
+
# Generate response
|
|
|
|
|
|
|
|
|
|
| 24 |
outputs = quantized_model.generate(**inputs, max_length=150, do_sample=False)
|
| 25 |
|
| 26 |
+
# Decode generated tokens to readable string
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
|
| 29 |
+
# Print generated response
|
| 30 |
+
print(f"Generated response: {response}")
|
|
|
|
|
|
|
|
|
|
|
|