Spaces:
Sleeping
Sleeping
File size: 2,839 Bytes
967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 b52f1c2 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b ddf509e 9dc275b 812346b 9dc275b d144157 9dc275b c7f53d4 9dc275b 7087b82 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b 967d7c5 9dc275b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# ==============================
# PAGE CONFIG
# ==============================
st.set_page_config(page_title="π» AI Coding Assistant", layout="wide")
st.title("π» AI Coding Assistant")
# ==============================
# LOAD MODEL (LIGHTWEIGHT)
# ==============================
@st.cache_resource
def load_model():
model_name = "deepseek-ai/deepseek-coder-1.3b-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float32
)
model.eval()
return tokenizer, model
with st.spinner("π Loading model..."):
tokenizer, model = load_model()
st.success("β
Ready")
# ==============================
# SESSION STATE (CHAT HISTORY)
# ==============================
if "messages" not in st.session_state:
st.session_state.messages = []
# ==============================
# CHAT DISPLAY
# ==============================
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
if msg["role"] == "assistant":
st.code(msg["content"])
else:
st.markdown(msg["content"])
# ==============================
# GENERATE RESPONSE
# ==============================
def generate_response(user_input):
# Build conversation prompt
conversation = ""
for msg in st.session_state.messages:
role = "User" if msg["role"] == "user" else "Assistant"
conversation += f"{role}: {msg['content']}\n"
conversation += f"User: {user_input}\nAssistant:"
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=200,
do_sample=True,
temperature=0.3,
top_p=0.9,
repetition_penalty=1.1,
pad_token_id=tokenizer.eos_token_id
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only assistant reply
if "Assistant:" in result:
result = result.split("Assistant:")[-1]
return result.strip()
# ==============================
# CHAT INPUT
# ==============================
user_input = st.chat_input("Ask your coding question...")
if user_input:
# Add user message
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
# Generate response
with st.spinner("π‘ Thinking..."):
response = generate_response(user_input)
# Add assistant message
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.code(response) |