Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from llama_cpp import Llama
|
4 |
+
from multiprocessing import Process, Queue
|
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, queue):
|
32 |
+
try:
|
33 |
+
user_input = request.message
|
34 |
+
responses = []
|
35 |
+
for llm in llms:
|
36 |
+
response = llm.create_chat_completion(
|
37 |
+
messages=[{"role": "user", "content": user_input}],
|
38 |
+
top_k=request.top_k,
|
39 |
+
top_p=request.top_p,
|
40 |
+
temperature=request.temperature
|
41 |
+
)
|
42 |
+
reply = response['choices'][0]['message']['content']
|
43 |
+
responses.append(reply)
|
44 |
+
best_response = select_best_response(responses, request)
|
45 |
+
queue.put(best_response)
|
46 |
+
except Exception as e:
|
47 |
+
queue.put(f"Error: {str(e)}")
|
48 |
+
|
49 |
+
def select_best_response(responses, request):
|
50 |
+
coherent_responses = filter_by_coherence(responses, request)
|
51 |
+
best_response = filter_by_similarity(coherent_responses)
|
52 |
+
return best_response
|
53 |
+
|
54 |
+
def filter_by_coherence(responses, request):
|
55 |
+
return responses
|
56 |
+
|
57 |
+
def filter_by_similarity(responses):
|
58 |
+
responses.sort(key=len, reverse=True)
|
59 |
+
best_response = responses[0]
|
60 |
+
for i in range(1, len(responses)):
|
61 |
+
ratio = SequenceMatcher(None, best_response, responses[i]).ratio()
|
62 |
+
if ratio < 0.9:
|
63 |
+
best_response = responses[i]
|
64 |
+
break
|
65 |
+
return best_response
|
66 |
+
|
67 |
+
@app.post("/generate_chat")
|
68 |
+
async def generate_chat(request: ChatRequest):
|
69 |
+
queue = Queue()
|
70 |
+
p = Process(target=generate_chat_response, args=(request, queue))
|
71 |
+
p.start()
|
72 |
+
p.join()
|
73 |
+
response = queue.get()
|
74 |
+
if "Error" in response:
|
75 |
+
raise HTTPException(status_code=500, detail=response)
|
76 |
+
return {"response": response}
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
uvicorn.run(app, host="0.0.0.0", port=8001)
|