Hjgugugjhuhjggg commited on
Commit
eb492f1
·
verified ·
1 Parent(s): 9409dc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -134,13 +134,9 @@ class ModelManager:
134
  print(f"Error loading model {model_config['name']}: {e}")
135
 
136
  def load_all_models(self):
137
- with ThreadPoolExecutor() as executor:
138
- futures = [
139
- executor.submit(self.load_model, config)
140
- for config in model_configs
141
- ]
142
- for future in as_completed(futures):
143
- pass
144
  return self.models
145
 
146
  model_manager = ModelManager()
@@ -210,22 +206,16 @@ def get_best_response(responses):
210
 
211
  async def generate_model_response(model, inputs):
212
  try:
213
- response = model(inputs)
214
  return remove_duplicates(response['choices'][0]['text'])
215
  except Exception as e:
216
  return ""
217
 
218
  async def process_message(message):
219
  inputs = normalize_input(message)
220
- with ThreadPoolExecutor() as executor:
221
- futures = [
222
- executor.submit(generate_model_response, model, inputs)
223
- for model in global_data['models'].values()
224
- ]
225
- responses = [
226
- future.result()
227
- for future in as_completed(futures)
228
- ]
229
  best_response = get_best_response(responses)
230
  return best_response
231
 
 
134
  print(f"Error loading model {model_config['name']}: {e}")
135
 
136
  def load_all_models(self):
137
+ loop = asyncio.get_event_loop()
138
+ tasks = [loop.run_in_executor(None, self.load_model, config) for config in model_configs]
139
+ loop.run_until_complete(asyncio.gather(*tasks))
 
 
 
 
140
  return self.models
141
 
142
  model_manager = ModelManager()
 
206
 
207
  async def generate_model_response(model, inputs):
208
  try:
209
+ response = await model(inputs)
210
  return remove_duplicates(response['choices'][0]['text'])
211
  except Exception as e:
212
  return ""
213
 
214
  async def process_message(message):
215
  inputs = normalize_input(message)
216
+ # Use asyncio.gather to handle multiple coroutines concurrently
217
+ tasks = [generate_model_response(model, inputs) for model in global_data['models'].values()]
218
+ responses = await asyncio.gather(*tasks)
 
 
 
 
 
 
219
  best_response = get_best_response(responses)
220
  return best_response
221