Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ from pydantic import BaseModel
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 5 |
import uvicorn
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
import re
|
| 8 |
import huggingface_hub
|
| 9 |
import spaces
|
|
|
|
|
|
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
|
@@ -56,9 +56,8 @@ class ModelManager:
|
|
| 56 |
def load_model(self, model_config):
|
| 57 |
try:
|
| 58 |
return {"model": Llama.from_pretrained(repo_id=model_config['repo_id'], filename=model_config['filename']), "name": model_config['name']}
|
| 59 |
-
except Exception
|
| 60 |
-
|
| 61 |
-
return None
|
| 62 |
|
| 63 |
def load_all_models(self):
|
| 64 |
if self.loaded:
|
|
@@ -77,7 +76,6 @@ class ModelManager:
|
|
| 77 |
return self.models
|
| 78 |
|
| 79 |
model_manager = ModelManager()
|
| 80 |
-
global_data['models'] = model_manager.load_all_models()
|
| 81 |
|
| 82 |
class ChatRequest(BaseModel):
|
| 83 |
message: str
|
|
@@ -97,8 +95,8 @@ def generate_chat_response(request, model_data):
|
|
| 97 |
)
|
| 98 |
reply = response['choices'][0]['message']['content']
|
| 99 |
return {"response": reply, "literal": user_input, "model_name": model_data['name']}
|
| 100 |
-
except Exception
|
| 101 |
-
|
| 102 |
|
| 103 |
def normalize_input(input_text):
|
| 104 |
return input_text.strip()
|
|
@@ -129,27 +127,29 @@ def select_best_response(responses):
|
|
| 129 |
|
| 130 |
@app.post("/generate")
|
| 131 |
def generate_chat(request: ChatRequest):
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 5 |
import uvicorn
|
|
|
|
|
|
|
| 6 |
import huggingface_hub
|
| 7 |
import spaces
|
| 8 |
+
import re
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 56 |
def load_model(self, model_config):
|
| 57 |
try:
|
| 58 |
return {"model": Llama.from_pretrained(repo_id=model_config['repo_id'], filename=model_config['filename']), "name": model_config['name']}
|
| 59 |
+
except Exception:
|
| 60 |
+
pass
|
|
|
|
| 61 |
|
| 62 |
def load_all_models(self):
|
| 63 |
if self.loaded:
|
|
|
|
| 76 |
return self.models
|
| 77 |
|
| 78 |
model_manager = ModelManager()
|
|
|
|
| 79 |
|
| 80 |
class ChatRequest(BaseModel):
|
| 81 |
message: str
|
|
|
|
| 95 |
)
|
| 96 |
reply = response['choices'][0]['message']['content']
|
| 97 |
return {"response": reply, "literal": user_input, "model_name": model_data['name']}
|
| 98 |
+
except Exception:
|
| 99 |
+
pass
|
| 100 |
|
| 101 |
def normalize_input(input_text):
|
| 102 |
return input_text.strip()
|
|
|
|
| 127 |
|
| 128 |
@app.post("/generate")
|
| 129 |
def generate_chat(request: ChatRequest):
|
| 130 |
+
try:
|
| 131 |
+
global_data['models'] = model_manager.load_all_models()
|
| 132 |
+
responses = []
|
| 133 |
+
with ThreadPoolExecutor() as executor:
|
| 134 |
+
futures = [executor.submit(generate_chat_response, request, model_data) for model_data in global_data['models']]
|
| 135 |
+
for future in as_completed(futures):
|
| 136 |
+
try:
|
| 137 |
+
response = future.result()
|
| 138 |
+
if response:
|
| 139 |
+
responses.append(response)
|
| 140 |
+
except Exception:
|
| 141 |
+
pass
|
| 142 |
+
|
| 143 |
+
if not responses:
|
| 144 |
+
raise HTTPException(status_code=500, detail="Error: No responses generated.")
|
| 145 |
+
|
| 146 |
+
best_response = select_best_response(responses)
|
| 147 |
+
return {
|
| 148 |
+
"best_response": best_response,
|
| 149 |
+
"all_responses": responses
|
| 150 |
+
}
|
| 151 |
+
except Exception:
|
| 152 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|