wakeupmh commited on
Commit
91d27ff
·
1 Parent(s): fc4ef0e

fix: tenacity

Browse files
Files changed (2) hide show
  1. requirements.txt +2 -1
  2. services/model_handler.py +26 -2
requirements.txt CHANGED
@@ -8,4 +8,5 @@ python-dotenv>=1.0.0
8
  agno==1.1.0
9
  pypdf>=3.11.1
10
  watchdog>=2.3.1
11
- sentencepiece>=0.1.99
 
 
8
  agno==1.1.0
9
  pypdf>=3.11.1
10
  watchdog>=2.3.1
11
+ sentencepiece>=0.1.99
12
+ tenacity>=8.2.2
services/model_handler.py CHANGED
@@ -6,8 +6,11 @@ from agno.tools.arxiv import ArxivTools
6
  from agno.tools.pubmed import PubmedTools
7
  from agno.models.huggingface import HuggingFace
8
  import os
 
 
9
 
10
  MODEL_PATH = "google/flan-t5-small"
 
11
  class ModelHandler:
12
  def __init__(self):
13
  """Initialize the model handler"""
@@ -22,7 +25,13 @@ class ModelHandler:
22
  def _initialize_model(self):
23
  """Initialize model and tokenizer"""
24
  self.model, self.tokenizer = self._load_model()
25
- base_model = HuggingFace(id=MODEL_PATH, api_key=os.getenv("hfToken"))
 
 
 
 
 
 
26
 
27
  self.translator = Agent(
28
  name="Translator",
@@ -42,7 +51,7 @@ class ModelHandler:
42
  instructions=[
43
  "You need to understand the context of the question to provide the best answer based on your tools."
44
  "Be precise and provide just enough information to be useful",
45
- "You must cite the sources used in your answer."
46
  "You must create an accessible summary.",
47
  "The content must be for people without autism knowledge.",
48
  "Focus in the main findings of the paper taking in consideration the question.",
@@ -104,6 +113,21 @@ class ModelHandler:
104
  logging.error(f"Error loading model: {str(e)}")
105
  return None, None
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  def generate_answer(self, query: str) -> str:
108
  try:
109
  translator = self.translator.run(query, stream=False)
 
6
  from agno.tools.pubmed import PubmedTools
7
  from agno.models.huggingface import HuggingFace
8
  import os
9
+ import time
10
+ from tenacity import retry, stop_after_attempt, wait_exponential
11
 
12
  MODEL_PATH = "google/flan-t5-small"
13
+
14
  class ModelHandler:
15
  def __init__(self):
16
  """Initialize the model handler"""
 
25
  def _initialize_model(self):
26
  """Initialize model and tokenizer"""
27
  self.model, self.tokenizer = self._load_model()
28
+
29
+ try:
30
+ base_model = self._initialize_hf_model_with_retry()
31
+ except Exception as e:
32
+ logging.warning(f"Failed to initialize HuggingFace API model, falling back to local model: {str(e)}")
33
+ # Fallback to local model
34
+ base_model = self._initialize_local_model()
35
 
36
  self.translator = Agent(
37
  name="Translator",
 
51
  instructions=[
52
  "You need to understand the context of the question to provide the best answer based on your tools."
53
  "Be precise and provide just enough information to be useful",
54
+ "You must cite the sources used in your answer.",
55
  "You must create an accessible summary.",
56
  "The content must be for people without autism knowledge.",
57
  "Focus in the main findings of the paper taking in consideration the question.",
 
113
  logging.error(f"Error loading model: {str(e)}")
114
  return None, None
115
 
116
+ @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
117
+ def _initialize_hf_model_with_retry(self):
118
+ """Initialize HuggingFace model with retry logic"""
119
+ api_key = os.getenv("hfToken")
120
+ if not api_key:
121
+ raise ValueError("HuggingFace API key not found in environment variables")
122
+
123
+ return HuggingFace(id=MODEL_PATH, api_key=api_key)
124
+
125
+ def _initialize_local_model(self):
126
+ """Initialize local model as fallback"""
127
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH)
128
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
129
+ return {"model": model, "tokenizer": tokenizer}
130
+
131
  def generate_answer(self, query: str) -> str:
132
  try:
133
  translator = self.translator.run(query, stream=False)