Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,70 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# 1. Page
|
| 6 |
-
st.set_page_config(page_title="
|
| 7 |
-
st.title("
|
| 8 |
-
st.
|
| 9 |
|
| 10 |
-
# 2. Model Loading
|
| 11 |
@st.cache_resource
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Create the pipeline
|
| 27 |
-
pipe = pipeline(
|
| 28 |
-
"text-generation",
|
| 29 |
-
model=model,
|
| 30 |
-
tokenizer=tokenizer,
|
| 31 |
-
)
|
| 32 |
-
return pipe
|
| 33 |
|
| 34 |
-
|
| 35 |
-
generator = load_model()
|
| 36 |
|
| 37 |
-
# 3.
|
| 38 |
if "messages" not in st.session_state:
|
| 39 |
st.session_state.messages = [
|
| 40 |
-
{
|
|
|
|
|
|
|
|
|
|
| 41 |
]
|
| 42 |
|
| 43 |
-
# Display
|
| 44 |
-
for
|
| 45 |
-
if
|
| 46 |
-
with st.chat_message(
|
| 47 |
-
st.markdown(
|
| 48 |
|
| 49 |
-
# 4.
|
| 50 |
-
if prompt := st.chat_input("
|
| 51 |
-
# Add user message to state
|
| 52 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 53 |
-
|
| 54 |
with st.chat_message("user"):
|
| 55 |
st.markdown(prompt)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
with st.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# 1. Page Config
|
| 6 |
+
st.set_page_config(page_title="Qwen Coder GGUF", page_icon="🤖", layout="wide")
|
| 7 |
+
st.title("🚀 Qwen2.5-Coder (GGUF CPU)")
|
| 8 |
+
st.caption("Optimized for Hugging Face Free Tier")
|
| 9 |
|
| 10 |
+
# 2. Model Loading with specific error handling
|
| 11 |
@st.cache_resource
|
| 12 |
+
def load_llm():
|
| 13 |
+
try:
|
| 14 |
+
# We use the 3B-Q4_K_M for a good balance of logic and RAM usage
|
| 15 |
+
return Llama.from_pretrained(
|
| 16 |
+
repo_id="Qwen/Qwen2.5-Coder-3B-Instruct-GGUF",
|
| 17 |
+
filename="qwen2.5-coder-3b-instruct-q4_k_m.gguf", # Explicit filename
|
| 18 |
+
n_ctx=4096, # Context window
|
| 19 |
+
n_threads=2, # Matches HF Free Tier vCPUs
|
| 20 |
+
verbose=False # Reduces log clutter
|
| 21 |
+
)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
st.error(f"Error loading model: {e}")
|
| 24 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
llm = load_llm()
|
|
|
|
| 27 |
|
| 28 |
+
# 3. Enhanced "Claude-style" System Prompt
|
| 29 |
if "messages" not in st.session_state:
|
| 30 |
st.session_state.messages = [
|
| 31 |
+
{
|
| 32 |
+
"role": "system",
|
| 33 |
+
"content": "You are an expert AI programming assistant. When asked to write code, provide the full file content. Use clear comments, follow best practices, and ensure the code is production-ready."
|
| 34 |
+
}
|
| 35 |
]
|
| 36 |
|
| 37 |
+
# Display history
|
| 38 |
+
for msg in st.session_state.messages:
|
| 39 |
+
if msg["role"] != "system":
|
| 40 |
+
with st.chat_message(msg["role"]):
|
| 41 |
+
st.markdown(msg["content"])
|
| 42 |
|
| 43 |
+
# 4. Generation Logic
|
| 44 |
+
if prompt := st.chat_input("Write a Python script to scrape a website..."):
|
|
|
|
| 45 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
| 46 |
with st.chat_message("user"):
|
| 47 |
st.markdown(prompt)
|
| 48 |
|
| 49 |
+
if llm:
|
| 50 |
+
with st.chat_message("assistant"):
|
| 51 |
+
response_placeholder = st.empty()
|
| 52 |
+
full_response = ""
|
| 53 |
+
|
| 54 |
+
# Stream the response
|
| 55 |
+
output = llm.create_chat_completion(
|
| 56 |
+
messages=st.session_state.messages,
|
| 57 |
+
stream=True,
|
| 58 |
+
max_tokens=1500, # Increased for "Complete Code" tasks
|
| 59 |
+
temperature=0.1 # Lower temperature = more precise code
|
| 60 |
)
|
| 61 |
|
| 62 |
+
for chunk in output:
|
| 63 |
+
if 'content' in chunk['choices'][0]['delta']:
|
| 64 |
+
token = chunk['choices'][0]['delta']['content']
|
| 65 |
+
full_response += token
|
| 66 |
+
response_placeholder.markdown(full_response + "▌")
|
| 67 |
|
| 68 |
+
response_placeholder.markdown(full_response)
|
| 69 |
+
|
| 70 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|