zahemen9900 commited on
Commit
cf4153c
·
1 Parent(s): a938c73

Refactor model loading in FinanceAdvisorBot for improved quantization handling and add asset setup function

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +66 -31
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .qodo
app.py CHANGED
@@ -37,32 +37,57 @@ class FinanceAdvisorBot:
37
  self.precision = torch.bfloat16 if torch.cuda.is_available() else torch.float32
38
  logger.info(f"Precision set to {self.precision}")
39
 
40
- # Load tokenizer and model with precision
41
  self.tokenizer = AutoTokenizer.from_pretrained(
42
  base_model,
43
  padding_side="left",
44
  trust_remote_code=True
45
  )
46
 
47
- # Configure 4-bit quantization
48
- bnb_config = BitsAndBytesConfig(
49
- load_in_4bit=True,
50
- bnb_4bit_use_double_quant=True,
51
- bnb_4bit_quant_type="nf4",
52
- bnb_4bit_compute_dtype=torch.bfloat16
53
- )
54
 
55
- # Load base model with quantization
56
- base = AutoModelForCausalLM.from_pretrained(
57
- base_model,
58
- quantization_config=bnb_config,
59
- device_map="auto",
60
- trust_remote_code=True
61
- )
 
 
 
 
 
 
 
62
 
63
- # Load adapter weights from hub
64
- self.model = PeftModel.from_pretrained(base, adapter_path)
65
- logger.info("Model loaded successfully from HuggingFace Hub")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  self.model.eval()
68
  self.should_analyze_question = should_analyze_question
@@ -75,19 +100,8 @@ class FinanceAdvisorBot:
75
  self.system_prompt = {
76
  "role": "system",
77
  "content": (
78
- "You are FinSight, a professional financial advisor chatbot. "
79
- "Follow these guidelines strictly:\n"
80
- "1. Provide clear, concise, and accurate financial guidance\n"
81
- "2. Focus on factual, practical advice without speculation\n"
82
- "3. Use professional but accessible language\n"
83
- "4. Break down complex concepts into understandable terms\n"
84
- # "5. Maintain objectivity and avoid personal opinions\n"
85
- "6. Always consider risk management in advice\n"
86
- "7. Be transparent about limitations of AI advice\n"
87
- "8. Cite reliable sources when appropriate\n"
88
- "9. Encourage due diligence and research\n"
89
- "10. Give bullet points and numbered lists when necessary\n"
90
- "Remember: You are an AI assistant focused on financial education and guidance."
91
  )
92
  }
93
  self.conversation_history = []
@@ -464,6 +478,27 @@ def create_demo():
464
 
465
  return demo
466
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467
  if __name__ == "__main__":
468
  demo = create_demo()
469
  demo.queue().launch()
 
37
  self.precision = torch.bfloat16 if torch.cuda.is_available() else torch.float32
38
  logger.info(f"Precision set to {self.precision}")
39
 
40
+ # Load tokenizer
41
  self.tokenizer = AutoTokenizer.from_pretrained(
42
  base_model,
43
  padding_side="left",
44
  trust_remote_code=True
45
  )
46
 
47
+ # Configure model loading based on device
48
+ model_kwargs = {
49
+ "trust_remote_code": True,
50
+ "device_map": "auto"
51
+ }
 
 
52
 
53
+ if self.device == "cuda":
54
+ # Use 4-bit quantization for GPU
55
+ bnb_config = BitsAndBytesConfig(
56
+ load_in_4bit=True,
57
+ bnb_4bit_use_double_quant=True,
58
+ bnb_4bit_quant_type="nf4",
59
+ bnb_4bit_compute_dtype=torch.bfloat16
60
+ )
61
+ model_kwargs["quantization_config"] = bnb_config
62
+ model_kwargs["torch_dtype"] = self.precision
63
+ else:
64
+ # Use 8-bit quantization for CPU
65
+ model_kwargs["load_in_8bit"] = True
66
+ model_kwargs["torch_dtype"] = torch.float32
67
 
68
+ try:
69
+ # Load base model with appropriate configuration
70
+ base = AutoModelForCausalLM.from_pretrained(
71
+ base_model,
72
+ **model_kwargs
73
+ )
74
+
75
+ # Load adapter weights
76
+ self.model = PeftModel.from_pretrained(base, adapter_path)
77
+ logger.info("Model loaded successfully with adapter")
78
+
79
+ except Exception as e:
80
+ logger.warning(f"Failed to load model with quantization: {e}")
81
+ logger.info("Falling back to standard model loading")
82
+
83
+ # Fallback to basic loading without quantization
84
+ base = AutoModelForCausalLM.from_pretrained(
85
+ base_model,
86
+ torch_dtype=torch.float32,
87
+ trust_remote_code=True,
88
+ device_map="auto"
89
+ )
90
+ self.model = PeftModel.from_pretrained(base, adapter_path)
91
 
92
  self.model.eval()
93
  self.should_analyze_question = should_analyze_question
 
100
  self.system_prompt = {
101
  "role": "system",
102
  "content": (
103
+ "You are FinSight, a professional financial advisor chatbot.\n"
104
+ "Provide clear, concise, and accurate financial guidance to the user."
 
 
 
 
 
 
 
 
 
 
 
105
  )
106
  }
107
  self.conversation_history = []
 
478
 
479
  return demo
480
 
481
+ def setup_assets():
482
+ """Set up assets directory and files"""
483
+ current_dir = Path(__file__).parent
484
+ assets_dir = current_dir / "static" / "assets"
485
+ favicon_path = assets_dir / "favicon.ico"
486
+
487
+ # Create assets directory
488
+ # If favicon doesn't exist, create a default one or copy from another location
489
+ assets_dir.mkdir(parents=True, exist_ok=True)
490
+
491
+ # Copy default favicon if it doesn't exist
492
+ if not favicon_path.exists():
493
+ default_favicon = current_dir.parent.parent / "assets" / "favicon.ico"
494
+ if default_favicon.exists():
495
+ shutil.copy(default_favicon, favicon_path)
496
+ else:
497
+ # Create empty favicon if no default exists
498
+ favicon_path.touch()
499
+
500
+ return favicon_path
501
+
502
  if __name__ == "__main__":
503
  demo = create_demo()
504
  demo.queue().launch()