Mohansai2004 commited on
Commit
c739cf0
·
1 Parent(s): 53e6fb8

feat: switch to deepseek model for token-free operation

Browse files
Files changed (2) hide show
  1. app.py +148 -58
  2. requirements.txt +16 -4
app.py CHANGED
@@ -4,6 +4,20 @@ import torch
4
  import gc
5
  from PIL import Image
6
  import io
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Set page configuration
9
  st.set_page_config(
@@ -49,18 +63,59 @@ st.markdown("""
49
  </style>
50
  """, unsafe_allow_html=True)
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  @st.cache_resource
53
  def load_model():
54
  try:
 
55
  model_id = "deepseek-ai/deepseek-coder-1.3b-base"
56
 
 
57
  tokenizer = AutoTokenizer.from_pretrained(
58
  model_id,
59
  trust_remote_code=True,
60
- padding_side='left'
 
61
  )
62
- tokenizer.pad_token = tokenizer.eos_token
63
 
 
 
 
 
 
 
 
 
 
64
  model = AutoModelForCausalLM.from_pretrained(
65
  model_id,
66
  torch_dtype=torch.float32,
@@ -69,82 +124,101 @@ def load_model():
69
  max_memory={'cpu': '16GB'}
70
  )
71
 
 
 
 
 
 
 
 
72
  model.eval()
73
  torch.set_num_threads(8)
74
  gc.collect()
75
  return model, tokenizer
76
 
77
  except Exception as e:
 
78
  st.error(f"Error loading model: {str(e)}")
79
  st.stop()
80
 
81
- def generate_response(prompt, image=None):
82
- model, tokenizer = load_model()
83
-
84
  try:
 
 
 
 
 
 
 
85
  code_prompt = f"""Write professional code based on the given requirements.
86
  Language: {prompt.split('code for:')[0] if 'code for:' in prompt else 'any'}
87
  Requirements: {prompt}
88
 
89
- Here's the implementation:
90
- ```"""
91
 
 
92
  inputs = tokenizer(
93
  code_prompt,
94
  return_tensors="pt",
95
  padding=True,
 
96
  max_length=1024,
97
- truncation=True
 
98
  )
99
 
 
 
 
 
 
100
  with torch.inference_mode():
101
- outputs = model.generate(
102
- inputs["input_ids"],
103
- max_length=2048, # Increased for longer code
104
- temperature=0.5, # More focused
105
- top_p=0.95,
106
- top_k=50,
107
- repetition_penalty=1.2,
108
- do_sample=True,
109
- num_return_sequences=1,
110
- pad_token_id=tokenizer.eos_token_id
111
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
114
- # Clean up response
115
- code = response.split("```")[1] if "```" in response else response
116
- return code.strip()
117
 
118
  except Exception as e:
 
119
  return f"Error: {str(e)}"
120
 
121
- def create_sidebar():
122
- with st.sidebar:
123
- st.image("https://raw.githubusercontent.com/streamlit/streamlit/develop/examples/streamlit_app_example.png",
124
- width=100)
125
- st.title("🛠️ Settings")
126
-
127
- task = st.selectbox(
128
- "Select Task",
129
- ["💻 Code Generation", "🖼️ Image Analysis", "📚 Concept Explanation"]
130
- )
131
-
132
- st.markdown("---")
133
-
134
- if st.button("♻️ Clear Cache", use_container_width=True):
135
- st.cache_resource.clear()
136
- st.success("Cache cleared successfully!")
137
-
138
- st.markdown("""
139
- ### 🌟 Pro Tips
140
- - Use detailed descriptions
141
- - Specify edge cases
142
- - Include example inputs/outputs
143
- """)
144
-
145
- return task.split()[1] # Return without emoji
146
-
147
  def code_generation_ui():
 
 
 
 
 
 
 
148
  col1, col2 = st.columns([2, 1])
149
 
150
  with col1:
@@ -184,16 +258,21 @@ def code_generation_ui():
184
  generate = st.button("🚀 Generate Code", use_container_width=True)
185
 
186
  if generate and prompt:
187
- with st.spinner("🔮 Generating your code..."):
188
- options = {
189
- "comments": add_comments,
190
- "tests": include_tests,
191
- "error_handling": error_handling
192
- }
193
- code = generate_enhanced_response(prompt, language, template, options)
 
 
 
 
 
194
 
195
- st.markdown("### 📋 Generated Code")
196
- with st.expander("Show Code", expanded=True):
197
  st.code(code, language=language.lower())
198
 
199
  col1, col2 = st.columns([1, 1])
@@ -207,6 +286,17 @@ def code_generation_ui():
207
  with col2:
208
  st.button("📋 Copy to Clipboard")
209
 
 
 
 
 
 
 
 
 
 
 
 
210
  def main():
211
  task = create_sidebar()
212
 
 
4
  import gc
5
  from PIL import Image
6
  import io
7
+ import logging
8
+ import sys
9
+
10
+ # Set up logging
11
+ logging.basicConfig(level=logging.DEBUG)
12
+ logger = logging.getLogger(__name__)
13
+
14
+ # Add debug info to page
15
+ debug_container = st.empty()
16
+
17
+ def debug_info(msg):
18
+ logger.debug(msg)
19
+ if st.session_state.get('show_debug', False):
20
+ debug_container.info(msg)
21
 
22
  # Set page configuration
23
  st.set_page_config(
 
63
  </style>
64
  """, unsafe_allow_html=True)
65
 
66
+ # Add debug toggle to sidebar
67
+ def create_sidebar():
68
+ with st.sidebar:
69
+ st.image("https://raw.githubusercontent.com/streamlit/streamlit/develop/examples/streamlit_app_example.png",
70
+ width=100)
71
+ st.title("🛠️ Settings")
72
+
73
+ # Add debug toggle
74
+ st.session_state.show_debug = st.checkbox("Show Debug Info", value=False)
75
+
76
+ task = st.selectbox(
77
+ "Select Task",
78
+ ["💻 Code Generation", "🖼️ Image Analysis", "📚 Concept Explanation"]
79
+ )
80
+
81
+ st.markdown("---")
82
+
83
+ if st.button("♻️ Clear Cache", use_container_width=True):
84
+ st.cache_resource.clear()
85
+ st.success("Cache cleared successfully!")
86
+
87
+ st.markdown("""
88
+ ### 🌟 Pro Tips
89
+ - Use detailed descriptions
90
+ - Specify edge cases
91
+ - Include example inputs/outputs
92
+ """)
93
+
94
+ return task.split()[1] # Return without emoji
95
+
96
  @st.cache_resource
97
  def load_model():
98
  try:
99
+ debug_info("Loading model...")
100
  model_id = "deepseek-ai/deepseek-coder-1.3b-base"
101
 
102
+ debug_info(f"Initializing tokenizer from {model_id}")
103
  tokenizer = AutoTokenizer.from_pretrained(
104
  model_id,
105
  trust_remote_code=True,
106
+ padding_side='left',
107
+ truncation_side='left'
108
  )
 
109
 
110
+ # Validate tokenizer configuration
111
+ debug_info(f"Tokenizer pad_token: {tokenizer.pad_token}")
112
+ debug_info(f"Tokenizer vocab size: {len(tokenizer)}")
113
+
114
+ if not hasattr(tokenizer, 'pad_token') or tokenizer.pad_token is None:
115
+ debug_info("Setting default pad token")
116
+ tokenizer.pad_token = '[PAD]'
117
+
118
+ debug_info("Loading model weights...")
119
  model = AutoModelForCausalLM.from_pretrained(
120
  model_id,
121
  torch_dtype=torch.float32,
 
124
  max_memory={'cpu': '16GB'}
125
  )
126
 
127
+ # Validate model configuration
128
+ debug_info(f"Model device: {next(model.parameters()).device}")
129
+ debug_info(f"Model memory: {torch.cuda.max_memory_allocated() if torch.cuda.is_available() else 'CPU only'}")
130
+
131
+ # Ensure model knows about pad token
132
+ model.config.pad_token_id = tokenizer.pad_token_id
133
+ model.config.eos_token_id = tokenizer.eos_token_id
134
  model.eval()
135
  torch.set_num_threads(8)
136
  gc.collect()
137
  return model, tokenizer
138
 
139
  except Exception as e:
140
+ logger.error(f"Model loading error: {str(e)}")
141
  st.error(f"Error loading model: {str(e)}")
142
  st.stop()
143
 
144
+ def generate_response_streaming(prompt, model, tokenizer, placeholder):
 
 
145
  try:
146
+ debug_info("Starting text generation...")
147
+ debug_info(f"Input prompt length: {len(prompt)}")
148
+
149
+ # Validate inputs
150
+ if not all([model, tokenizer, placeholder]):
151
+ raise ValueError("Missing required components")
152
+
153
  code_prompt = f"""Write professional code based on the given requirements.
154
  Language: {prompt.split('code for:')[0] if 'code for:' in prompt else 'any'}
155
  Requirements: {prompt}
156
 
157
+ Here's the implementation:"""
 
158
 
159
+ # Create input tensors with proper attention masks
160
  inputs = tokenizer(
161
  code_prompt,
162
  return_tensors="pt",
163
  padding=True,
164
+ truncation=True,
165
  max_length=1024,
166
+ add_special_tokens=True,
167
+ return_attention_mask=True
168
  )
169
 
170
+ # Ensure input tensors are properly shaped
171
+ attention_mask = inputs['attention_mask']
172
+ input_ids = inputs['input_ids']
173
+
174
+ generated_text = ""
175
  with torch.inference_mode():
176
+ while input_ids.shape[1] < 2048:
177
+ outputs = model.generate(
178
+ input_ids=input_ids,
179
+ attention_mask=attention_mask,
180
+ max_new_tokens=1, # Generate one token at a time
181
+ pad_token_id=tokenizer.pad_token_id,
182
+ eos_token_id=tokenizer.eos_token_id,
183
+ do_sample=True,
184
+ temperature=0.5,
185
+ top_p=0.95,
186
+ repetition_penalty=1.1,
187
+ )
188
+
189
+ # Get next token and update tensors
190
+ next_token = outputs[:, -1:]
191
+ input_ids = torch.cat([input_ids, next_token], dim=1)
192
+ attention_mask = torch.ones_like(input_ids)
193
+
194
+ # Update display
195
+ current_text = tokenizer.decode(input_ids[0], skip_special_tokens=True)
196
+ generated_text = current_text.replace(code_prompt, "").strip()
197
+ placeholder.code(generated_text)
198
+
199
+ # Check for completion
200
+ if next_token[0, 0].item() == tokenizer.eos_token_id:
201
+ break
202
+
203
+ # Add validation checks during generation
204
+ if attention_mask.shape != input_ids.shape:
205
+ debug_info(f"Shape mismatch - attention: {attention_mask.shape}, ids: {input_ids.shape}")
206
 
207
+ debug_info(f"Generation complete. Output length: {len(generated_text)}")
208
+ return generated_text
 
 
209
 
210
  except Exception as e:
211
+ logger.error(f"Generation error: {str(e)}")
212
  return f"Error: {str(e)}"
213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
  def code_generation_ui():
215
+ debug_info("Initializing UI components")
216
+
217
+ # Validate PROGRAMMING_LANGUAGES is defined
218
+ if 'PROGRAMMING_LANGUAGES' not in globals():
219
+ st.error("Programming languages configuration not found")
220
+ return
221
+
222
  col1, col2 = st.columns([2, 1])
223
 
224
  with col1:
 
258
  generate = st.button("🚀 Generate Code", use_container_width=True)
259
 
260
  if generate and prompt:
261
+ debug_info(f"Generating code for language: {language}")
262
+ debug_info(f"Template: {template}")
263
+ debug_info(f"Options: comments={add_comments}, tests={include_tests}")
264
+
265
+ st.markdown("### 📋 Generated Code")
266
+
267
+ # Create a placeholder for streaming output
268
+ code_placeholder = st.empty()
269
+
270
+ with st.spinner("🔮 Generating..."):
271
+ model, tokenizer = load_model()
272
+ code = generate_response_streaming(prompt, model, tokenizer, code_placeholder)
273
 
274
+ # After generation complete, show final version with copy/download buttons
275
+ with st.expander("Final Code", expanded=True):
276
  st.code(code, language=language.lower())
277
 
278
  col1, col2 = st.columns([1, 1])
 
286
  with col2:
287
  st.button("📋 Copy to Clipboard")
288
 
289
+ # Add global variables check
290
+ if 'PROGRAMMING_LANGUAGES' not in globals():
291
+ PROGRAMMING_LANGUAGES = {
292
+ "Web Development": ["HTML", "CSS", "JavaScript", "TypeScript", "PHP"],
293
+ "Backend": ["Python", "Java", "C#", "Ruby", "Go", "Node.js"],
294
+ "Data & ML": ["Python", "R", "SQL", "Julia"],
295
+ "Mobile": ["Swift", "Kotlin", "Java", "React Native"],
296
+ "System": ["C", "C++", "Rust", "Shell"]
297
+ }
298
+ debug_info("Initialized PROGRAMMING_LANGUAGES")
299
+
300
  def main():
301
  task = create_sidebar()
302
 
requirements.txt CHANGED
@@ -1,16 +1,28 @@
1
  # Core dependencies
2
  streamlit>=1.41.1
 
 
 
3
  torch>=2.0.0
4
  transformers>=4.33.0
5
  accelerate>=0.21.0
 
 
 
6
 
7
  # UI enhancements
 
8
  streamlit-ace>=0.1.1
9
  streamlit-extras>=0.3.0
10
  streamlit-code-editor>=0.1.6
11
 
12
- # Model dependencies
13
- sentencepiece>=0.1.99
14
  Pillow>=9.0.0
15
- einops>=0.6.1
16
- scikit-learn
 
 
 
 
 
 
 
1
  # Core dependencies
2
  streamlit>=1.41.1
3
+ watchdog>=3.0.0
4
+
5
+ # Model and ML
6
  torch>=2.0.0
7
  transformers>=4.33.0
8
  accelerate>=0.21.0
9
+ sentencepiece>=0.1.99
10
+ einops>=0.6.1
11
+ scikit-learn>=1.3.0
12
 
13
  # UI enhancements
14
+ streamlit-option-menu>=0.3.2
15
  streamlit-ace>=0.1.1
16
  streamlit-extras>=0.3.0
17
  streamlit-code-editor>=0.1.6
18
 
19
+ # Image processing
 
20
  Pillow>=9.0.0
21
+
22
+ # Performance optimizations
23
+ rich>=13.5.2
24
+ tqdm>=4.65.0
25
+ numpy>=1.24.0
26
+
27
+ # Memory management
28
+ psutil>=5.9.0