Spaces:
Runtime error

markshep68 commited on
Commit
fb83057
·
verified ·
1 Parent(s): 473dfc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -1,26 +1,21 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # Load the Mistral-7B model
6
- model_name = "mistralai/Mistral-7B-v0.1"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name, token=True)
8
-
9
- # Check if GPU is available, otherwise use CPU
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
- # Load model on the correct device
13
- model = AutoModelForCausalLM.from_pretrained(
14
- model_name,
15
- device_map="auto",
16
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
17
- token=True
18
- ).to(device)
19
 
20
  # Define chatbot function
21
  def chat_with_mistral(user_input):
22
  try:
23
- inputs = tokenizer(user_input, return_tensors="pt").to(device)
24
  outputs = model.generate(
25
  **inputs,
26
  max_new_tokens=100,
@@ -45,4 +40,3 @@ chatbot = gr.Interface(
45
 
46
  # Launch app
47
  chatbot.launch()
48
-
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3
  import torch
4
 
5
+ # Load a smaller version of the Mistral model (4-bit quantized)
6
+ model_name = "mistralai/Mistral-7B-Instruct-v0.1"
7
+ quantization_config = BitsAndBytesConfig(
8
+ load_in_4bit=True,
9
+ bnb_4bit_compute_dtype=torch.float16
10
+ )
11
 
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=True)
13
+ model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config, token=True)
 
 
 
 
 
14
 
15
  # Define chatbot function
16
  def chat_with_mistral(user_input):
17
  try:
18
+ inputs = tokenizer(user_input, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
19
  outputs = model.generate(
20
  **inputs,
21
  max_new_tokens=100,
 
40
 
41
  # Launch app
42
  chatbot.launch()