Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from llama_cpp import Llama
|
4 |
+
from concurrent.futures import ProcessPoolExecutor
|
5 |
+
import uvicorn
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from difflib import SequenceMatcher
|
8 |
+
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
models = [
|
14 |
+
{"repo_id": "Ffftdtd5dtft/gpt2-xl-Q2_K-GGUF", "filename": "gpt2-xl-q2_k.gguf"},
|
15 |
+
{"repo_id": "Ffftdtd5dtft/Meta-Llama-3.1-8B-Instruct-Q2_K-GGUF", "filename": "meta-llama-3.1-8b-instruct-q2_k.gguf"},
|
16 |
+
{"repo_id": "Ffftdtd5dtft/gemma-2-9b-it-Q2_K-GGUF", "filename": "gemma-2-9b-it-q2_k.gguf"},
|
17 |
+
{"repo_id": "Ffftdtd5dtft/gemma-2-27b-Q2_K-GGUF", "filename": "gemma-2-27b-q2_k.gguf"},
|
18 |
+
]
|
19 |
+
|
20 |
+
llms = []
|
21 |
+
for model in models:
|
22 |
+
llm = Llama.from_pretrained(repo_id=model['repo_id'], filename=model['filename'])
|
23 |
+
llms.append(llm)
|
24 |
+
|
25 |
+
class ChatRequest(BaseModel):
|
26 |
+
message: str
|
27 |
+
top_k: int = 50
|
28 |
+
top_p: float = 0.95
|
29 |
+
temperature: float = 0.7
|
30 |
+
|
31 |
+
def generate_chat_response(request, llm):
|
32 |
+
try:
|
33 |
+
user_input = request.message
|
34 |
+
response = llm.create_chat_completion(
|
35 |
+
messages=[{"role": "user", "content": user_input}],
|
36 |
+
top_k=request.top_k,
|
37 |
+
top_p=request.top_p,
|
38 |
+
temperature=request.temperature
|
39 |
+
)
|
40 |
+
reply = response['choices'][0]['message']['content']
|
41 |
+
return reply
|
42 |
+
except Exception as e:
|
43 |
+
return f"Error: {str(e)}"
|
44 |
+
|
45 |
+
def select_best_response(responses, request):
|
46 |
+
coherent_responses = filter_by_coherence(responses, request)
|
47 |
+
best_response = filter_by_similarity(coherent_responses)
|
48 |
+
return best_response
|
49 |
+
|
50 |
+
def filter_by_coherence(responses, request):
|
51 |
+
return responses
|
52 |
+
|
53 |
+
def filter_by_similarity(responses):
|
54 |
+
responses.sort(key=len, reverse=True)
|
55 |
+
best_response = responses[0]
|
56 |
+
for i in range(1, len(responses)):
|
57 |
+
ratio = SequenceMatcher(None, best_response, responses[i]).ratio()
|
58 |
+
if ratio < 0.9:
|
59 |
+
best_response = responses[i]
|
60 |
+
break
|
61 |
+
return best_response
|
62 |
+
|
63 |
+
@app.post("/generate_chat")
|
64 |
+
async def generate_chat(request: ChatRequest):
|
65 |
+
with ProcessPoolExecutor() as executor:
|
66 |
+
futures = [executor.submit(generate_chat_response, request, llm) for llm in llms]
|
67 |
+
responses = [future.result() for future in futures]
|
68 |
+
|
69 |
+
if any("Error" in response for response in responses):
|
70 |
+
error_response = next(response for response in responses if "Error" in response)
|
71 |
+
raise HTTPException(status_code=500, detail=error_response)
|
72 |
+
|
73 |
+
best_response = select_best_response(responses, request)
|
74 |
+
return {"response": best_response}
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|