Spaces:
Runtime error
Runtime error
Update app.py
Browse files1212345567891011121314715916171819201415162122232425262728293031181920323322232425343536373839303132404142434445464748495051525354555657585960616263from fastapi import FastAPIfrom pydantic import BaseModelimport osimport gradio as grfrom llama_cpp import Llamafrom huggingface_hub import hf_hub_download
app = FastAPI()# =========================# LOAD MODEL# =========================model_path = hf_hub_download( repo_id="Mikecode123/ALX", filename="qwen2-1_5b-instruct-q4_0.gguf", token=os.getenv("HF_TOKEN"))
# Load model once on startupllm = Llama( model_path="qwen2-1_5b-instruct-q4_0.gguf", model_path=model_path, n_ctx=2048, n_threads=2)
# Request body structureclass ChatRequest(BaseModel): message: str# =========================# CHAT FUNCTION (FIXED)# =========================def chat(message, history): # convert Gradio tuple history -> messages format messages = []
for user, bot in history: messages.append({"role": "user", "content": user}) messages.append({"role": "assistant", "content": bot})
@app.get("/")def home(): return {"status": "AI server running"} messages.append({"role": "user", "content": message})
@app.post("/chat")def chat(req: ChatRequest): output = llm.create_completion( prompt=req.message, output = llm.create_chat_completion( messages=messages, max_tokens=300, temperature=0.7 )
return { "response": output["choices"][0]["text"].strip() } reply = output["choices"][0]["message"]["content"]
history.append((message, reply))
return history, history
# =========================# UI (FIXED GRADIO 4)# =========================with gr.Blocks() as demo: gr.Markdown("# 🧠 Living Legend AI Chat")
chatbot = gr.Chatbot() msg = gr.Textbox()
state = gr.State([])
def respond(message, history): new_history, updated_state = chat(message, history) return "", new_history, updated_state
msg.submit(respond, [msg, state], [msg, chatbot, state])
demo.launch()app.py+49-18