Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
from llama_cpp import Llama
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# LOAD MODEL
|
| 8 |
-
# =========================
|
| 9 |
-
model_path = hf_hub_download(
|
| 10 |
-
repo_id="Mikecode123/ALX",
|
| 11 |
-
filename="qwen2-1_5b-instruct-q4_0.gguf",
|
| 12 |
-
token=os.getenv("HF_TOKEN")
|
| 13 |
-
)
|
| 14 |
|
|
|
|
| 15 |
llm = Llama(
|
| 16 |
-
model_path=
|
| 17 |
-
n_ctx=
|
| 18 |
-
n_threads=
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def chat(message, history):
|
| 25 |
-
history = history or []
|
| 26 |
-
|
| 27 |
-
messages = []
|
| 28 |
-
|
| 29 |
-
# SAFE: ensure correct format
|
| 30 |
-
for msg in history:
|
| 31 |
-
if isinstance(msg, dict):
|
| 32 |
-
messages.append({
|
| 33 |
-
"role": msg.get("role", ""),
|
| 34 |
-
"content": str(msg.get("content", ""))
|
| 35 |
-
})
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"content": str(message)
|
| 41 |
-
})
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
max_tokens=300,
|
| 47 |
temperature=0.7
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
messages.append({
|
| 54 |
-
"role": "assistant",
|
| 55 |
-
"content": response
|
| 56 |
-
})
|
| 57 |
-
|
| 58 |
-
return "", messages
|
| 59 |
-
|
| 60 |
-
# =========================
|
| 61 |
-
# UI (GRADIO 4 SAFE STYLE BUT BACKWARD COMPATIBLE)
|
| 62 |
-
# =========================
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
gr.Markdown("# 🧠 Living Legend AI Chatbot")
|
| 65 |
-
|
| 66 |
-
chatbot = gr.Chatbot() # IMPORTANT FIX
|
| 67 |
-
msg = gr.Textbox()
|
| 68 |
-
clear = gr.Button("Clear")
|
| 69 |
-
|
| 70 |
-
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 71 |
-
|
| 72 |
-
clear.click(lambda: [], None, chatbot)
|
| 73 |
-
|
| 74 |
-
demo.queue()
|
| 75 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from llama_cpp import Llama
|
|
|
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Load model once on startup
|
| 8 |
llm = Llama(
|
| 9 |
+
model_path="qwen2-1_5b-instruct-q4_0.gguf",
|
| 10 |
+
n_ctx=2048,
|
| 11 |
+
n_threads=2
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Request body structure
|
| 15 |
+
class ChatRequest(BaseModel):
|
| 16 |
+
message: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def home():
|
| 20 |
+
return {"status": "AI server running"}
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
@app.post("/chat")
|
| 23 |
+
def chat(req: ChatRequest):
|
| 24 |
+
output = llm.create_completion(
|
| 25 |
+
prompt=req.message,
|
| 26 |
max_tokens=300,
|
| 27 |
temperature=0.7
|
| 28 |
)
|
| 29 |
|
| 30 |
+
return {
|
| 31 |
+
"response": output["choices"][0]["text"].strip()
|
| 32 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|