Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +55 -99
src/streamlit_app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
| 3 |
-
import re
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
# ==============================
|
| 7 |
# PAGE CONFIG
|
| 8 |
# ==============================
|
| 9 |
-
st.set_page_config(page_title="π» AI
|
| 10 |
-
st.title("π» AI
|
| 11 |
|
| 12 |
# ==============================
|
| 13 |
-
# LOAD MODEL (
|
| 14 |
# ==============================
|
| 15 |
@st.cache_resource
|
| 16 |
def load_model():
|
|
@@ -26,122 +25,79 @@ def load_model():
|
|
| 26 |
model.eval()
|
| 27 |
return tokenizer, model
|
| 28 |
|
| 29 |
-
|
| 30 |
-
with st.spinner("π Loading AI model..."):
|
| 31 |
tokenizer, model = load_model()
|
| 32 |
|
| 33 |
-
st.success("β
|
| 34 |
|
| 35 |
# ==============================
|
| 36 |
-
#
|
| 37 |
# ==============================
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if "Code:" in text:
|
| 41 |
-
text = text.split("Code:")[-1]
|
| 42 |
-
|
| 43 |
-
# Remove non-ascii junk
|
| 44 |
-
text = re.sub(r"[^\x00-\x7F]+", "", text)
|
| 45 |
-
|
| 46 |
-
# Remove unwanted sections
|
| 47 |
-
text = re.sub(r"(Explanation:.*)", "", text, flags=re.DOTALL)
|
| 48 |
-
|
| 49 |
-
# Remove markdown
|
| 50 |
-
text = text.replace("```python", "").replace("```", "")
|
| 51 |
-
|
| 52 |
-
# Filter useful lines only
|
| 53 |
-
lines = text.split("\n")
|
| 54 |
-
clean_lines = []
|
| 55 |
-
|
| 56 |
-
for line in lines:
|
| 57 |
-
line = line.strip()
|
| 58 |
-
if not line:
|
| 59 |
-
continue
|
| 60 |
-
if any(word in line.lower() for word in ["instruction", "task", "response"]):
|
| 61 |
-
continue
|
| 62 |
-
clean_lines.append(line)
|
| 63 |
-
|
| 64 |
-
return "\n".join(clean_lines).strip()
|
| 65 |
|
| 66 |
# ==============================
|
| 67 |
-
#
|
| 68 |
# ==============================
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
### Rules:
|
| 76 |
-
- Only return code
|
| 77 |
-
- No explanation
|
| 78 |
-
- Use simple and correct syntax
|
| 79 |
-
- Provide complete solution
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
"""
|
| 86 |
|
| 87 |
-
inputs = tokenizer(
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
return extract_clean_code(result)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
# SESSION STATE
|
| 109 |
-
# ==============================
|
| 110 |
-
if "history" not in st.session_state:
|
| 111 |
-
st.session_state.history = []
|
| 112 |
|
| 113 |
# ==============================
|
| 114 |
-
#
|
| 115 |
# ==============================
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
with
|
| 122 |
-
|
| 123 |
-
"Select Programming Language",
|
| 124 |
-
["Python", "JavaScript", "SQL", "Java", "C++", "HTML", "CSS"]
|
| 125 |
-
)
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
if st.button("Generate Code"):
|
| 131 |
-
if not user_prompt.strip():
|
| 132 |
-
st.warning("β οΈ Please enter a task")
|
| 133 |
-
else:
|
| 134 |
-
with st.spinner("β‘ Generating clean code..."):
|
| 135 |
-
code = generate_code(user_prompt, language)
|
| 136 |
-
|
| 137 |
-
st.session_state.history.append((user_prompt, code))
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
# ==============================
|
| 142 |
-
if st.session_state.history:
|
| 143 |
-
st.subheader("π Generated Results")
|
| 144 |
|
| 145 |
-
|
| 146 |
-
st.
|
| 147 |
-
st.code(c, language=language.lower())
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
# ==============================
|
| 6 |
# PAGE CONFIG
|
| 7 |
# ==============================
|
| 8 |
+
st.set_page_config(page_title="π» AI Coding Assistant", layout="wide")
|
| 9 |
+
st.title("π» AI Coding Assistant")
|
| 10 |
|
| 11 |
# ==============================
|
| 12 |
+
# LOAD MODEL (LIGHTWEIGHT)
|
| 13 |
# ==============================
|
| 14 |
@st.cache_resource
|
| 15 |
def load_model():
|
|
|
|
| 25 |
model.eval()
|
| 26 |
return tokenizer, model
|
| 27 |
|
| 28 |
+
with st.spinner("π Loading model..."):
|
|
|
|
| 29 |
tokenizer, model = load_model()
|
| 30 |
|
| 31 |
+
st.success("β
Ready")
|
| 32 |
|
| 33 |
# ==============================
|
| 34 |
+
# SESSION STATE (CHAT HISTORY)
|
| 35 |
# ==============================
|
| 36 |
+
if "messages" not in st.session_state:
|
| 37 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# ==============================
|
| 40 |
+
# CHAT DISPLAY
|
| 41 |
# ==============================
|
| 42 |
+
for msg in st.session_state.messages:
|
| 43 |
+
with st.chat_message(msg["role"]):
|
| 44 |
+
if msg["role"] == "assistant":
|
| 45 |
+
st.code(msg["content"])
|
| 46 |
+
else:
|
| 47 |
+
st.markdown(msg["content"])
|
| 48 |
|
| 49 |
+
# ==============================
|
| 50 |
+
# GENERATE RESPONSE
|
| 51 |
+
# ==============================
|
| 52 |
+
def generate_response(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Build conversation prompt
|
| 55 |
+
conversation = ""
|
| 56 |
+
for msg in st.session_state.messages:
|
| 57 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
| 58 |
+
conversation += f"{role}: {msg['content']}\n"
|
| 59 |
|
| 60 |
+
conversation += f"User: {user_input}\nAssistant:"
|
|
|
|
| 61 |
|
| 62 |
+
inputs = tokenizer(conversation, return_tensors="pt", truncation=True)
|
| 63 |
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
outputs = model.generate(
|
| 66 |
+
**inputs,
|
| 67 |
+
max_new_tokens=200,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
temperature=0.3,
|
| 70 |
+
top_p=0.9,
|
| 71 |
+
repetition_penalty=1.1,
|
| 72 |
+
pad_token_id=tokenizer.eos_token_id
|
| 73 |
+
)
|
|
|
|
| 74 |
|
| 75 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 76 |
|
| 77 |
+
# Extract only assistant reply
|
| 78 |
+
if "Assistant:" in result:
|
| 79 |
+
result = result.split("Assistant:")[-1]
|
| 80 |
|
| 81 |
+
return result.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# ==============================
|
| 84 |
+
# CHAT INPUT
|
| 85 |
# ==============================
|
| 86 |
+
user_input = st.chat_input("Ask your coding question...")
|
| 87 |
|
| 88 |
+
if user_input:
|
| 89 |
+
# Add user message
|
| 90 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 91 |
|
| 92 |
+
with st.chat_message("user"):
|
| 93 |
+
st.markdown(user_input)
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Generate response
|
| 96 |
+
with st.spinner("π‘ Thinking..."):
|
| 97 |
+
response = generate_response(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Add assistant message
|
| 100 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
with st.chat_message("assistant"):
|
| 103 |
+
st.code(response)
|
|
|