Spaces:
Runtime error
Runtime error
Update app.py
Browse files121234467891011121356781617189101112132122232425262728293031323334351415161737383940411819202143444522232425262728295051525354555657585960616263646566676869707172737475303132import osimport gradio as grfrom fastapi import FastAPIfrom pydantic import BaseModelfrom llama_cpp import Llamafrom huggingface_hub import hf_hub_download
# =========================# LOAD MODEL# =========================model_path = hf_hub_download( repo_id="Mikecode123/ALX", filename="qwen2-1_5b-instruct-q4_0.gguf", token=os.getenv("HF_TOKEN"))app = FastAPI()
# Load model once on startupllm = Llama( model_path=model_path, n_ctx=512, n_threads=1 model_path="qwen2-1_5b-instruct-q4_0.gguf", n_ctx=2048, n_threads=2)
# =========================# CHAT FUNCTION (STRICT MESSAGES FORMAT)# =========================def chat(message, history): history = history or []
messages = []
# SAFE: ensure correct format for msg in history: if isinstance(msg, dict): messages.append({ "role": msg.get("role", ""), "content": str(msg.get("content", "")) })# Request body structureclass ChatRequest(BaseModel): message: str
# add user message messages.append({ "role": "user", "content": str(message) })@app.get("/")def home(): return {"status": "AI server running"}
# inference output = llm.create_chat_completion( messages=messages,@app.post("/chat")def chat(req: ChatRequest): output = llm.create_completion( prompt=req.message, max_tokens=300, temperature=0.7 )
response = output["choices"][0]["message"]["content"]
# append assistant reply messages.append({ "role": "assistant", "content": response })
return "", messages
# =========================# UI (GRADIO 4 SAFE STYLE BUT BACKWARD COMPATIBLE)# =========================with gr.Blocks() as demo: gr.Markdown("# 🧠 Living Legend AI Chatbot")
chatbot = gr.Chatbot() # IMPORTANT FIX msg = gr.Textbox() clear = gr.Button("Clear")
msg.submit(chat, [msg, chatbot], [msg, chatbot])
clear.click(lambda: [], None, chatbot)
demo.queue()demo.launch(server_name="0.0.0.0", server_port=7860) return { "response": output["choices"][0]["text"].strip() }app.py+20-63