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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +27 -64
src/streamlit_app.py CHANGED
@@ -6,11 +6,11 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  # ==============================
7
  # PAGE CONFIG
8
  # ==============================
9
- st.set_page_config(page_title="πŸ’» AI Coding Assistant", layout="wide")
10
- st.title("πŸ’» AI Coding Assistant")
11
 
12
  # ==============================
13
- # LOAD MODEL (HF SAFE)
14
  # ==============================
15
  @st.cache_resource
16
  def load_model():
@@ -38,16 +38,11 @@ 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
  # ==============================
@@ -55,23 +50,8 @@ def extract_code(text):
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
 
@@ -79,70 +59,53 @@ Output:
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
104
  # ==============================
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()
 
6
  # ==============================
7
  # PAGE CONFIG
8
  # ==============================
9
+ st.set_page_config(page_title="πŸ€– AI Assistant", layout="wide")
10
+ st.title("πŸ€– Simple AI Assistant")
11
 
12
  # ==============================
13
+ # LOAD MODEL
14
  # ==============================
15
  @st.cache_resource
16
  def load_model():
 
38
  st.session_state.messages = []
39
 
40
  # ==============================
41
+ # CLEAN OUTPUT
42
  # ==============================
43
+ def clean_text(text):
 
 
 
 
 
 
44
  text = re.sub(r"[^\x00-\x7F]+", "", text)
45
+ text = text.replace("```", "")
46
  return text.strip()
47
 
48
  # ==============================
 
50
  # ==============================
51
  def generate_response(user_input):
52
 
53
+ prompt = f"""User: {user_input}
54
+ Assistant:"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
57
 
 
59
  outputs = model.generate(
60
  **inputs,
61
  max_new_tokens=120,
62
+ do_sample=True,
63
+ temperature=0.5,
64
+ top_p=0.9,
65
+ repetition_penalty=1.1,
66
  pad_token_id=tokenizer.eos_token_id
67
  )
68
 
69
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
70
 
71
+ # Extract only assistant reply
72
+ if "Assistant:" in result:
73
+ result = result.split("Assistant:")[-1]
74
+
75
+ return clean_text(result)
76
 
77
  # ==============================
78
  # DISPLAY CHAT
79
  # ==============================
80
  for msg in st.session_state.messages:
81
  with st.chat_message(msg["role"]):
82
+ st.markdown(msg["content"])
 
 
 
83
 
84
  # ==============================
85
+ # INPUT BOX
86
  # ==============================
87
+ user_input = st.chat_input("Type your message...")
88
 
89
  if user_input:
90
+ # User message
91
  st.session_state.messages.append({"role": "user", "content": user_input})
92
 
93
  with st.chat_message("user"):
94
  st.markdown(user_input)
95
 
96
+ # AI response
97
+ with st.spinner("πŸ€– Thinking..."):
98
  response = generate_response(user_input)
99
 
 
100
  st.session_state.messages.append({"role": "assistant", "content": response})
101
 
102
  with st.chat_message("assistant"):
103
+ st.markdown(response)
104
 
105
  # ==============================
106
+ # SIDEBAR
107
  # ==============================
108
+ st.sidebar.title("βš™οΈ Options")
109
 
110
  if st.sidebar.button("🧹 Clear Chat"):
111
+ st.session_state.messages = []