pradeep4321 commited on
Commit
8c113c3
Β·
verified Β·
1 Parent(s): 9dc275b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +74 -29
src/streamlit_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import torch
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
  # ==============================
@@ -9,7 +10,7 @@ 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():
@@ -31,54 +32,72 @@ with st.spinner("πŸ”„ Loading model..."):
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
@@ -86,18 +105,44 @@ def generate_response(user_input):
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import torch
3
+ import re
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
  # ==============================
 
10
  st.title("πŸ’» AI Coding Assistant")
11
 
12
  # ==============================
13
+ # LOAD MODEL (HF SAFE)
14
  # ==============================
15
  @st.cache_resource
16
  def load_model():
 
32
  st.success("βœ… Ready")
33
 
34
  # ==============================
35
+ # SESSION STATE
36
  # ==============================
37
  if "messages" not in st.session_state:
38
  st.session_state.messages = []
39
 
40
  # ==============================
41
+ # CLEAN CODE EXTRACTION
42
  # ==============================
43
+ def extract_code(text):
44
+ match = re.search(r"```python(.*?)```", text, re.DOTALL)
45
+
46
+ if match:
47
+ return match.group(1).strip()
48
+
49
+ # fallback cleaning
50
+ text = re.sub(r"[^\x00-\x7F]+", "", text)
51
+ return text.strip()
52
 
53
  # ==============================
54
  # GENERATE RESPONSE
55
  # ==============================
56
  def generate_response(user_input):
57
 
58
+ prompt = f"""
59
+ You are a professional coding assistant.
60
+
61
+ Return ONLY valid code.
62
+
63
+ Rules:
64
+ - Only code
65
+ - No explanation
66
+ - Use simple syntax
67
+ - Must be complete
68
 
69
+ User Request:
70
+ {user_input}
71
 
72
+ Output:
73
+ ```python
74
+ """
75
+
76
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
77
 
78
  with torch.no_grad():
79
  outputs = model.generate(
80
  **inputs,
81
+ max_new_tokens=120,
82
+ do_sample=False, # πŸ”₯ deterministic (important)
83
+ temperature=0.0,
84
+ repetition_penalty=1.2,
 
85
  pad_token_id=tokenizer.eos_token_id
86
  )
87
 
88
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
89
 
90
+ return extract_code(result)
 
 
91
 
92
+ # ==============================
93
+ # DISPLAY CHAT
94
+ # ==============================
95
+ for msg in st.session_state.messages:
96
+ with st.chat_message(msg["role"]):
97
+ if msg["role"] == "assistant":
98
+ st.code(msg["content"], language="python")
99
+ else:
100
+ st.markdown(msg["content"])
101
 
102
  # ==============================
103
  # CHAT INPUT
 
105
  user_input = st.chat_input("Ask your coding question...")
106
 
107
  if user_input:
108
+ # Show user message
109
  st.session_state.messages.append({"role": "user", "content": user_input})
110
 
111
  with st.chat_message("user"):
112
  st.markdown(user_input)
113
 
114
  # Generate response
115
+ with st.spinner("πŸ’‘ Generating code..."):
116
  response = generate_response(user_input)
117
 
118
+ # Show assistant response
119
  st.session_state.messages.append({"role": "assistant", "content": response})
120
 
121
  with st.chat_message("assistant"):
122
+ st.code(response, language="python")
123
+
124
+ # ==============================
125
+ # QUICK ACTION BUTTONS
126
+ # ==============================
127
+ st.sidebar.title("⚑ Actions")
128
+
129
+ if st.sidebar.button("🧹 Clear Chat"):
130
+ st.session_state.messages = []
131
+
132
+ if st.sidebar.button("πŸ“– Explain Last Code"):
133
+ if st.session_state.messages:
134
+ last_code = st.session_state.messages[-1]["content"]
135
+ st.session_state.messages.append({
136
+ "role": "user",
137
+ "content": f"Explain this code:\n{last_code}"
138
+ })
139
+ st.experimental_rerun()
140
+
141
+ if st.sidebar.button("πŸ›  Fix Last Code"):
142
+ if st.session_state.messages:
143
+ last_code = st.session_state.messages[-1]["content"]
144
+ st.session_state.messages.append({
145
+ "role": "user",
146
+ "content": f"Fix this code:\n{last_code}"
147
+ })
148
+ st.experimental_rerun()