Update app.py
Browse files
app.py
CHANGED
@@ -235,8 +235,7 @@ global_data = {
|
|
235 |
model_configs = [
|
236 |
{"repo_id": "Hjgugugjhuhjggg/testing_semifinal-Q2_K-GGUF", "filename": "testing_semifinal-q2_k.gguf", "name": "testing"},
|
237 |
{"repo_id": "bartowski/Llama-3.2-3B-Instruct-uncensored-GGUF", "filename": "Llama-3.2-3B-Instruct-uncensored-Q2_K.gguf", "name": "Llama-3.2-3B-Instruct"},
|
238 |
-
{"repo_id": "Ffftdtd5dtft/Meta-Llama-3.1-
|
239 |
-
{"repo_id": "Hhhbvvkgh/Heidi-Llama-v4-Q2_K-GGUF", "filename": "heidi-llama-v4-q2_k.gguf", "name": "Heidi-Llama-V4"}
|
240 |
]
|
241 |
|
242 |
def normalize_input(input_text):
|
@@ -246,12 +245,19 @@ def normalize_input(input_text):
|
|
246 |
return " ".join(filtered_words)
|
247 |
|
248 |
async def load_models():
|
|
|
249 |
for model in model_configs:
|
250 |
model_path = os.path.join("models", model["filename"])
|
251 |
if not os.path.exists(model_path):
|
252 |
url = f"https://huggingface.co/{model['repo_id']}/resolve/main/{model['filename']}"
|
253 |
-
|
254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
async def generate_model_response(model, inputs):
|
257 |
try:
|
@@ -263,7 +269,9 @@ async def generate_model_response(model, inputs):
|
|
263 |
def get_best_response(responses):
|
264 |
if not responses:
|
265 |
return {"error": "No valid responses from models."}
|
266 |
-
|
|
|
|
|
267 |
|
268 |
async def process_message(message):
|
269 |
inputs = normalize_input(message)
|
@@ -272,17 +280,6 @@ async def process_message(message):
|
|
272 |
best_response = get_best_response(responses)
|
273 |
return best_response
|
274 |
|
275 |
-
app = FastAPI()
|
276 |
-
|
277 |
-
@app.post("/generate")
|
278 |
-
async def generate(request: Request):
|
279 |
-
try:
|
280 |
-
body = await request.json()
|
281 |
-
response = await process_message(body['message'])
|
282 |
-
return JSONResponse(content={"response": response})
|
283 |
-
except Exception as e:
|
284 |
-
return JSONResponse(content={"error": str(e)})
|
285 |
-
|
286 |
def run_uvicorn():
|
287 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
288 |
|
|
|
235 |
model_configs = [
|
236 |
{"repo_id": "Hjgugugjhuhjggg/testing_semifinal-Q2_K-GGUF", "filename": "testing_semifinal-q2_k.gguf", "name": "testing"},
|
237 |
{"repo_id": "bartowski/Llama-3.2-3B-Instruct-uncensored-GGUF", "filename": "Llama-3.2-3B-Instruct-uncensored-Q2_K.gguf", "name": "Llama-3.2-3B-Instruct"},
|
238 |
+
{"repo_id": "Ffftdtd5dtft/Meta-Llama-3.1-13B", "filename": "Meta-Llama-3.1-13B-Q2_K.gguf", "name": "Meta-Llama-3.1-13B"}
|
|
|
239 |
]
|
240 |
|
241 |
def normalize_input(input_text):
|
|
|
245 |
return " ".join(filtered_words)
|
246 |
|
247 |
async def load_models():
|
248 |
+
tasks = []
|
249 |
for model in model_configs:
|
250 |
model_path = os.path.join("models", model["filename"])
|
251 |
if not os.path.exists(model_path):
|
252 |
url = f"https://huggingface.co/{model['repo_id']}/resolve/main/{model['filename']}"
|
253 |
+
tasks.append(download_model(url, model_path))
|
254 |
+
await asyncio.gather(*tasks)
|
255 |
+
for model in model_configs:
|
256 |
+
model_path = os.path.join("models", model["filename"])
|
257 |
+
global_data['models'][model["name"]] = Llama(model_path)
|
258 |
+
|
259 |
+
async def download_model(url, model_path):
|
260 |
+
wget.download(url, model_path)
|
261 |
|
262 |
async def generate_model_response(model, inputs):
|
263 |
try:
|
|
|
269 |
def get_best_response(responses):
|
270 |
if not responses:
|
271 |
return {"error": "No valid responses from models."}
|
272 |
+
scores = [response['score'] for response in responses]
|
273 |
+
best_score_index = scores.index(max(scores))
|
274 |
+
return responses[best_score_index]
|
275 |
|
276 |
async def process_message(message):
|
277 |
inputs = normalize_input(message)
|
|
|
280 |
best_response = get_best_response(responses)
|
281 |
return best_response
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
def run_uvicorn():
|
284 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
285 |
|