Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,27 @@ def install(package):
|
|
| 6 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 7 |
|
| 8 |
# Install dependencies
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
from transformers import LlamaTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 18 |
import gradio as gr
|
|
@@ -25,26 +39,40 @@ import torch
|
|
| 25 |
load_dotenv()
|
| 26 |
|
| 27 |
# Log in to Hugging Face
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Model configuration
|
| 31 |
model_name = "mistralai/Mistral-7B-v0.3"
|
| 32 |
|
| 33 |
-
# Load tokenizer
|
| 34 |
tokenizer = LlamaTokenizer.from_pretrained(model_name)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# 4-bit quantization for better performance
|
| 37 |
bnb_config = BitsAndBytesConfig(
|
| 38 |
-
load_in_4bit=True,
|
| 39 |
bnb_4bit_quant_type="nf4",
|
| 40 |
-
bnb_4bit_compute_dtype=
|
| 41 |
)
|
| 42 |
|
| 43 |
# Load model with optimized settings
|
| 44 |
model = AutoModelForCausalLM.from_pretrained(
|
| 45 |
model_name,
|
| 46 |
quantization_config=bnb_config,
|
| 47 |
-
device_map=
|
| 48 |
torch_dtype=torch.float16
|
| 49 |
)
|
| 50 |
|
|
@@ -70,4 +98,4 @@ gr.ChatInterface(
|
|
| 70 |
title="Shërbimi i Konsumatorit",
|
| 71 |
examples=["Si mund të rivendos fjalëkalimin?", "A e keni në dispozicion këtë produkt?"],
|
| 72 |
cache_examples=True
|
| 73 |
-
).launch(server_port=7860, share=True)
|
|
|
|
| 6 |
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 7 |
|
| 8 |
# Install dependencies
|
| 9 |
+
packages = [
|
| 10 |
+
"transformers==4.34.0",
|
| 11 |
+
"torch==2.0.1+cu118",
|
| 12 |
+
"gradio==3.39.0",
|
| 13 |
+
"accelerate==0.23.0",
|
| 14 |
+
"bitsandbytes==0.41.1",
|
| 15 |
+
"sentencepiece==0.1.99",
|
| 16 |
+
"python-dotenv==1.0.0"
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
for package in packages:
|
| 20 |
+
try:
|
| 21 |
+
install(package)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Failed to install {package}: {e}")
|
| 24 |
+
|
| 25 |
+
# Install bitsandbytes from Test PyPI if needed
|
| 26 |
+
try:
|
| 27 |
+
import bitsandbytes
|
| 28 |
+
except ImportError:
|
| 29 |
+
install("bitsandbytes -i https://test.pypi.org/simple/")
|
| 30 |
|
| 31 |
from transformers import LlamaTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 32 |
import gradio as gr
|
|
|
|
| 39 |
load_dotenv()
|
| 40 |
|
| 41 |
# Log in to Hugging Face
|
| 42 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 43 |
+
if huggingface_token:
|
| 44 |
+
login(token=huggingface_token)
|
| 45 |
+
else:
|
| 46 |
+
raise ValueError("HUGGINGFACE_TOKEN is missing in .env file!")
|
| 47 |
|
| 48 |
# Model configuration
|
| 49 |
model_name = "mistralai/Mistral-7B-v0.3"
|
| 50 |
|
| 51 |
+
# Load tokenizer
|
| 52 |
tokenizer = LlamaTokenizer.from_pretrained(model_name)
|
| 53 |
|
| 54 |
+
# Check if torch is correctly installed
|
| 55 |
+
try:
|
| 56 |
+
assert torch.cuda.is_available(), "CUDA is not available. Install CUDA or use CPU mode."
|
| 57 |
+
except AssertionError as e:
|
| 58 |
+
print(e)
|
| 59 |
+
print("Falling back to CPU mode.")
|
| 60 |
+
device_map = "cpu"
|
| 61 |
+
else:
|
| 62 |
+
device_map = "auto"
|
| 63 |
+
|
| 64 |
# 4-bit quantization for better performance
|
| 65 |
bnb_config = BitsAndBytesConfig(
|
| 66 |
+
load_in_4bit=True, # Change to load_in_8bit=True if needed
|
| 67 |
bnb_4bit_quant_type="nf4",
|
| 68 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 69 |
)
|
| 70 |
|
| 71 |
# Load model with optimized settings
|
| 72 |
model = AutoModelForCausalLM.from_pretrained(
|
| 73 |
model_name,
|
| 74 |
quantization_config=bnb_config,
|
| 75 |
+
device_map=device_map,
|
| 76 |
torch_dtype=torch.float16
|
| 77 |
)
|
| 78 |
|
|
|
|
| 98 |
title="Shërbimi i Konsumatorit",
|
| 99 |
examples=["Si mund të rivendos fjalëkalimin?", "A e keni në dispozicion këtë produkt?"],
|
| 100 |
cache_examples=True
|
| 101 |
+
).launch(server_port=7860, share=True)
|