wakeupmh commited on
Commit
067323d
·
1 Parent(s): 75a9848

fix: models

Browse files
Files changed (1) hide show
  1. services/model_handler.py +44 -20
services/model_handler.py CHANGED
@@ -101,6 +101,17 @@ class ModelHandler:
101
  add_references=True,
102
  )
103
 
 
 
 
 
 
 
 
 
 
 
 
104
  @staticmethod
105
  @st.cache_resource
106
  def _load_model():
@@ -120,7 +131,12 @@ class ModelHandler:
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"""
@@ -130,25 +146,33 @@ class ModelHandler:
130
 
131
  def generate_answer(self, query: str) -> str:
132
  try:
133
- translator = self.translator.run(query, stream=False)
134
- logging.info(f"Translated query")
135
- research = self.researcher.run(translator.content, stream=False)
136
- logging.info(f"Generated research")
137
- summary = self.summarizer.run(research.content, stream=False)
138
- logging.info(f"Generated summary")
139
- presentation = self.presenter.run(summary.content, stream=False)
140
- logging.info(f"Generated presentation")
141
 
142
- if not presentation.content:
143
- return self._get_fallback_response()
144
- return presentation.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  except Exception as e:
146
  logging.error(f"Error generating answer: {str(e)}")
147
- return self._get_fallback_response()
148
-
149
- @staticmethod
150
- def _get_fallback_response() -> str:
151
- """Provide a friendly, helpful fallback response"""
152
- return """
153
- Peço descula, mas encontrei um erro ao gerar a resposta. Tente novamente ou refaça a sua pergunta.
154
- """
 
101
  add_references=True,
102
  )
103
 
104
+ def _format_prompt(self, role, instructions, query):
105
+ """Format the prompt for the model"""
106
+ return f"""Task: {role}
107
+
108
+ Instructions:
109
+ {instructions}
110
+
111
+ Input: {query}
112
+
113
+ Output:"""
114
+
115
  @staticmethod
116
  @st.cache_resource
117
  def _load_model():
 
131
  if not api_key:
132
  raise ValueError("HuggingFace API key not found in environment variables")
133
 
134
+ return HuggingFace(
135
+ id=MODEL_PATH,
136
+ api_key=api_key,
137
+ task="text2text-generation",
138
+ max_length=512
139
+ )
140
 
141
  def _initialize_local_model(self):
142
  """Initialize local model as fallback"""
 
146
 
147
  def generate_answer(self, query: str) -> str:
148
  try:
149
+ # Format translation prompt
150
+ translation_prompt = self._format_prompt(
151
+ role="Translate the following text to English",
152
+ instructions="Provide a direct English translation of the input text.",
153
+ query=query
154
+ )
 
 
155
 
156
+ # Get English translation
157
+ translation = self.translator.run(translation_prompt, stream=False)
158
+ if not translation:
159
+ logging.error("Translation failed")
160
+ return "Error: Unable to translate the query"
161
+
162
+ # Format research prompt
163
+ research_prompt = self._format_prompt(
164
+ role="Research Assistant",
165
+ instructions="Provide a clear and concise answer based on scientific sources.",
166
+ query=translation
167
+ )
168
+
169
+ # Get research results
170
+ research_results = self.researcher.run(research_prompt, stream=False)
171
+ if not research_results:
172
+ logging.error("Research failed")
173
+ return "Error: Unable to perform research"
174
+
175
+ return research_results
176
  except Exception as e:
177
  logging.error(f"Error generating answer: {str(e)}")
178
+ return f"Error: {str(e)}"