Spaces:
Runtime error
Runtime error
Update app.py
Browse files11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575823456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import osimport gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
# =========================
# LOAD MODEL FROM HF REPO
# =========================
model_path = hf_hub_download(
repo_id="Mikecode123/ALX",
filename="qwen2-1_5b-instruct-q4_0.gguf"
)
llm = Llama(
model_path=model_path,
n_ctx=1024,
n_threads=2
)
# =========================
# CHAT FUNCTION
# =========================
def chat(prompt, history):
messages = []
# convert gradio history to chat format
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": prompt})
output = llm.create_chat_completion(
messages=messages,
max_tokens=500,
temperature=0.7
)
response = output["choices"][0]["message"]["content"]
history.append((prompt, response))
return "", history
# =========================
# GRADIO UI
# =========================
with gr.Blocks() as demo:
gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
chatbot = gr.Chatbot()
msg = gr.Textbox(label="Ask your AI")
clear = gr.Button("Clear")
msg.submit(chat, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot)
# =========================
# LAUNCH
# =========================
import gradio as grfrom llama_cpp import Llamafrom huggingface_hub import hf_hub_download
# =========================# LOAD MODEL FROM HF REPO# =========================model_path = hf_hub_download( repo_id="Mikecode123/ALX", filename="qwen2-1_5b-instruct-q4_0.gguf", token=os.getenv("HF_TOKEN") # secure access)
llm = Llama( model_path=model_path, n_ctx=512, # reduced to avoid OOM (important in 2026 Spaces) n_threads=1 # safer for low resources)
# =========================# CHAT FUNCTION# =========================def chat(prompt, history): messages = []
for user_msg, bot_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": prompt})
output = llm.create_chat_completion( messages=messages, max_tokens=300, temperature=0.7 )
response = output["choices"][0]["message"]["content"] history.append((prompt, response))
return "", history
# =========================# GRADIO UI# =========================with gr.Blocks() as demo: gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
chatbot = gr.Chatbot() msg = gr.Textbox(label="Ask your AI") clear = gr.Button("Clear")
msg.submit(chat, [msg, chatbot], [msg, chatbot]) clear.click(lambda: [], None, chatbot)
# =========================# LAUNCH# =========================demo.launch()app.py+59-58