Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from typing import Dict, List
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id="microsoft/Phi-3-mini-4k-instruct-gguf",
|
| 10 |
+
filename="Phi-3-mini-4k-instruct-q4.gguf",
|
| 11 |
+
cache_dir='/content/cache',
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.post("/chat")
|
| 16 |
+
async def chat(request: Request):
|
| 17 |
+
try:
|
| 18 |
+
data = await request.json()
|
| 19 |
+
messages = data.get("messages", [])
|
| 20 |
+
|
| 21 |
+
response = llm.create_chat_completion(
|
| 22 |
+
messages=messages
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
return JSONResponse(content={"response": response['choices'][0]['message']['content']})
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
import uvicorn
|
| 32 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|