hamxaameer commited on
Commit
209529c
·
verified ·
1 Parent(s): 103f3e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -242
app.py CHANGED
@@ -1,321 +1,253 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import GPT2Tokenizer, AutoModelForCausalLM
4
- from peft import PeftModel, PeftConfig, AutoPeftModelForCausalLM
5
  import os
6
 
7
- # Load model and tokenizer
8
- print("="*70)
9
- print("Loading Pseudo-Code to Code Generator")
10
- print("="*70)
11
-
12
- # Determine device
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
- print(f"Using device: {device}")
15
 
16
- try:
17
- # Method 1: Try loading as PeftModel (LoRA adapters)
18
- print("\nAttempting to load LoRA model from Hugging Face format...")
19
- model_path = "./model" # or wherever you uploaded the model files
 
 
20
 
21
- # Check if model files exist
22
- if os.path.exists(model_path):
23
- print(f"✓ Model directory found: {model_path}")
24
-
25
- # Load with AutoPeftModel (handles LoRA automatically)
26
- model = AutoPeftModelForCausalLM.from_pretrained(
27
- model_path,
28
- device_map={"": device},
29
- torch_dtype=torch.float32,
30
- low_cpu_mem_usage=True
31
- )
32
- tokenizer = GPT2Tokenizer.from_pretrained(model_path)
33
-
34
- print("✓ Model and tokenizer loaded successfully (Hugging Face format)")
35
- else:
36
- # Fallback: Load from current directory
37
- print(f"✗ Model directory not found, trying current directory...")
38
- model = AutoPeftModelForCausalLM.from_pretrained(
39
- ".",
40
- device_map={"": device},
41
- torch_dtype=torch.float32,
42
- low_cpu_mem_usage=True
43
- )
44
- tokenizer = GPT2Tokenizer.from_pretrained(".")
45
- print("✓ Model loaded from current directory")
46
 
47
- # Set model to evaluation mode
48
- model.eval()
49
- print(f"✓ Model ready on {device}")
50
 
51
- # Print model info
52
- try:
53
- trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
54
- total_params = sum(p.numel() for p in model.parameters())
55
- print(f" Total parameters: {total_params:,}")
56
- print(f"✓ Trainable parameters: {trainable_params:,}")
57
- except:
58
- print("✓ Model parameters info not available")
59
-
60
- print("="*70)
61
 
62
- except Exception as e:
63
- print(f"\n✗ Error loading with AutoPeftModel: {e}")
64
- print("\nTrying alternative method: Loading base model + LoRA adapters separately...")
65
 
66
- try:
67
- # Method 2: Load base GPT-2 and merge LoRA adapters
68
- from transformers import GPT2LMHeadModel
69
-
70
- print("Loading base GPT-2 model...")
71
- base_model = GPT2LMHeadModel.from_pretrained("gpt2")
72
-
73
- print("Loading LoRA adapters...")
74
- model = PeftModel.from_pretrained(
75
- base_model,
76
- model_path if os.path.exists(model_path) else ".",
77
- device_map={"": device}
78
- )
79
-
80
- tokenizer = GPT2Tokenizer.from_pretrained(
81
- model_path if os.path.exists(model_path) else "."
82
- )
83
-
84
- model.eval()
85
- print("✓ Model loaded successfully (base + adapters)")
86
-
87
- except Exception as e2:
88
- print(f"\n✗ Alternative method also failed: {e2}")
89
- print("\n" + "="*70)
90
- print("DEPLOYMENT INSTRUCTIONS")
91
- print("="*70)
92
- print("Please upload the model in Hugging Face format, not pickle!")
93
- print("\nFiles needed:")
94
- print(" - adapter_config.json")
95
- print(" - adapter_model.safetensors (or .bin)")
96
- print(" - tokenizer.json")
97
- print(" - tokenizer_config.json")
98
- print(" - special_tokens_map.json")
99
- print(" - vocab.json")
100
- print(" - merges.txt")
101
- print("\nSee SAVE_MODEL_FOR_HF.py for instructions on how to save properly.")
102
- print("="*70)
103
- raise
104
 
105
- def generate_code(pseudocode, indent, line, max_length=128, temperature=0.7, top_p=0.9):
106
  """
107
  Generate code from pseudo-code with line and indent information.
108
 
109
  Args:
110
  pseudocode: Input pseudo-code string
111
- indent: Indentation level
112
- line: Line number
113
- max_length: Maximum length of generated sequence
114
- temperature: Sampling temperature
115
- top_p: Nucleus sampling parameter
116
 
117
  Returns:
118
  Generated code string
119
  """
120
  try:
121
- # Format input with line and indent information
122
- prompt = f"Pseudocode: {pseudocode} | Indent: {indent} | Line: {line}\nCode:"
 
123
 
124
- # Tokenize input
125
- inputs = tokenizer(prompt, return_tensors='pt', padding=True)
126
 
127
- # Move to same device as model
128
- device = next(model.parameters()).device
129
- inputs = {k: v.to(device) for k, v in inputs.items()}
130
 
131
- # Generate
132
  model.eval()
133
  with torch.no_grad():
134
  outputs = model.generate(
135
  **inputs,
136
- max_length=max_length,
137
- temperature=temperature,
138
- top_p=top_p,
139
  do_sample=True,
140
  pad_token_id=tokenizer.eos_token_id,
141
  eos_token_id=tokenizer.eos_token_id,
142
- num_return_sequences=1
 
 
143
  )
144
 
145
  # Decode output
146
  generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
147
 
148
- # Extract only the code part
149
  if "Code:" in generated_text:
150
- code = generated_text.split("Code:")[1].strip()
151
  else:
152
  code = generated_text.strip()
153
 
154
- return code
155
-
156
- except Exception as e:
157
- return f"Error generating code: {str(e)}"
158
-
159
- def gradio_generate_code(pseudocode, indent, line, temperature=0.7, top_p=0.9, max_length=128):
160
- """
161
- Wrapper function for Gradio interface.
162
- """
163
- if not pseudocode.strip():
164
- return "⚠️ Please enter some pseudocode!"
165
-
166
- try:
167
- indent = int(indent)
168
- line = int(line)
169
- generated_code = generate_code(
170
- pseudocode,
171
- indent,
172
- line,
173
- max_length=int(max_length),
174
- temperature=float(temperature),
175
- top_p=float(top_p)
176
- )
177
- return generated_code
178
- except ValueError:
179
- return "⚠️ Indent and Line must be valid numbers!"
180
  except Exception as e:
181
- return f"❌ Error: {str(e)}"
182
 
183
- # Example pseudocodes
184
- examples = [
185
- ["create integer n", 1, 1, 0.7, 0.9, 128],
186
- ["read n", 1, 2, 0.7, 0.9, 128],
187
- ["for i from 0 to n", 1, 3, 0.7, 0.9, 128],
188
- ["print i", 2, 4, 0.7, 0.9, 128],
189
- ["if n is equal to 0", 1, 5, 0.7, 0.9, 128],
190
- ["create string s", 1, 1, 0.7, 0.9, 128],
191
- ["read s", 1, 2, 0.7, 0.9, 128],
192
- ]
 
193
 
194
  # Create Gradio interface
195
- with gr.Blocks(theme=gr.themes.Soft(), title="Pseudo-Code to Code Generator") as demo:
196
- gr.Markdown(
197
- """
198
- # 🐍 Pseudo-Code to Code Generator (GPT-2 + LoRA)
199
-
200
- Convert natural language pseudo-code to executable code using a fine-tuned GPT-2 model with LoRA.
201
-
202
- **Model Details:**
203
- - Base Model: GPT-2
204
- - Training: SPOC Dataset (C++ code examples)
205
- - Optimization: LoRA (Low-Rank Adaptation) + 16-bit precision
206
- - Trained on: 20,000 pseudo-code to code pairs
207
-
208
- **Note:** The model was trained on C++ code examples from the SPOC dataset, so it generates C++-style code.
209
- """
210
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
  with gr.Row():
 
213
  with gr.Column(scale=1):
214
- gr.Markdown("### 📝 Input")
215
-
216
  pseudocode_input = gr.Textbox(
217
- label="Pseudocode",
218
- placeholder="Enter your pseudocode here...\nExample: create integer n",
219
- lines=5,
220
- max_lines=10
221
  )
222
 
223
  with gr.Row():
224
- indent_input = gr.Number(
225
- label="Indent Level",
226
- value=1,
227
- precision=0,
228
- info="Indentation level (0=no indent, 1=first level, etc.)"
229
  )
230
-
231
- line_input = gr.Number(
232
- label="Line Number",
233
- value=1,
234
- precision=0,
235
  info="Line number in the program"
236
  )
237
 
238
- gr.Markdown("### ⚙️ Generation Parameters")
239
 
240
  with gr.Row():
241
- temperature_slider = gr.Slider(
242
- minimum=0.1,
243
- maximum=1.5,
244
- value=0.7,
245
- step=0.1,
246
- label="Temperature",
247
- info="Higher = more creative/random"
248
  )
249
-
250
- top_p_slider = gr.Slider(
251
- minimum=0.1,
252
- maximum=1.0,
253
- value=0.9,
254
- step=0.05,
255
- label="Top-p (Nucleus Sampling)",
256
- info="Probability threshold for sampling"
257
  )
258
 
259
- max_length_slider = gr.Slider(
260
- minimum=64,
261
- maximum=256,
262
- value=128,
263
- step=16,
264
- label="Max Length",
265
- info="Maximum tokens to generate"
266
  )
267
 
268
  generate_btn = gr.Button("🚀 Generate Code", variant="primary", size="lg")
269
 
 
270
  with gr.Column(scale=1):
271
- gr.Markdown("### 💻 Generated Code")
272
-
273
  output = gr.Textbox(
274
- label="Generated Code",
275
  lines=15,
276
- max_lines=20,
277
  show_copy_button=True
278
  )
279
 
280
- gr.Markdown("### 📚 Examples")
281
- gr.Examples(
282
- examples=examples,
283
- inputs=[pseudocode_input, indent_input, line_input, temperature_slider, top_p_slider, max_length_slider],
 
284
  outputs=output,
285
- fn=gradio_generate_code,
286
- cache_examples=False,
287
  )
288
 
289
- gr.Markdown(
290
- """
291
- ---
292
- ### ℹ️ How to Use:
293
- 1. **Enter pseudocode**: Write your natural language description
294
- 2. **Set indent level**: Specify the indentation (0 for no indent, 1 for first level, etc.)
295
- 3. **Set line number**: Indicate the line position in your program
296
- 4. **Adjust parameters** (optional): Fine-tune temperature and top-p for different results
297
- 5. **Click Generate**: Get your code!
298
-
299
- ### 💡 Tips:
300
- - Higher temperature (0.8-1.2) = more creative but potentially less accurate
301
- - Lower temperature (0.5-0.7) = more conservative and predictable
302
- - Top-p controls diversity; 0.9 is usually a good balance
303
- - The model generates C++-style code as it was trained on the SPOC dataset
304
-
305
- ### 🔗 Resources:
306
- - [SPOC Dataset](https://github.com/sumith1896/spoc)
307
- - [Research Paper](https://arxiv.org/pdf/1906.04908)
308
- - Model trained with LoRA for efficiency
309
- """
310
  )
311
 
312
- # Connect button to function
313
- generate_btn.click(
314
- fn=gradio_generate_code,
315
- inputs=[pseudocode_input, indent_input, line_input, temperature_slider, top_p_slider, max_length_slider],
316
  outputs=output
317
  )
 
 
 
 
 
 
 
 
 
318
 
319
- # Launch the app
320
  if __name__ == "__main__":
321
- demo.launch()
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
4
  import os
5
 
6
+ # Model configuration
7
+ MODEL_NAME = "your-username/your-model-name" # Replace with your actual HF model repo
8
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
9
 
10
+ # Load model and tokenizer
11
+ @gr.utils.cache
12
+ def load_model():
13
+ """Load the model and tokenizer with caching"""
14
+ print(f"Loading model from: {MODEL_NAME}")
15
+ print(f"Using device: {DEVICE}")
16
 
17
+ # Load tokenizer
18
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # Set pad token if not set
21
+ if tokenizer.pad_token is None:
22
+ tokenizer.pad_token = tokenizer.eos_token
23
 
24
+ # Load model with appropriate settings
25
+ model = AutoModelForCausalLM.from_pretrained(
26
+ MODEL_NAME,
27
+ torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
28
+ device_map="auto" if DEVICE == "cuda" else None,
29
+ trust_remote_code=True
30
+ )
 
 
 
31
 
32
+ if DEVICE == "cpu":
33
+ model = model.to(DEVICE)
 
34
 
35
+ print("✅ Model and tokenizer loaded successfully!")
36
+ return model, tokenizer
37
+
38
+ # Initialize model and tokenizer
39
+ model, tokenizer = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ def generate_code(pseudocode, indent=1, line=1, temperature=0.7, top_p=0.9, max_length=128):
42
  """
43
  Generate code from pseudo-code with line and indent information.
44
 
45
  Args:
46
  pseudocode: Input pseudo-code string
47
+ indent: Indentation level (1-10)
48
+ line: Line number (1-100)
49
+ temperature: Sampling temperature (0.1-2.0)
50
+ top_p: Nucleus sampling parameter (0.1-1.0)
51
+ max_length: Maximum length of generated sequence (50-512)
52
 
53
  Returns:
54
  Generated code string
55
  """
56
  try:
57
+ # Validate inputs
58
+ if not pseudocode.strip():
59
+ return "❌ Error: Please enter some pseudocode."
60
 
61
+ # Format input with line and indent information (matches training format)
62
+ prompt = f"Pseudocode: {pseudocode.strip()} | Indent: {indent} | Line: {line}\nCode:"
63
 
64
+ # Tokenize input
65
+ inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=256)
66
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
67
 
68
+ # Generate with the model
69
  model.eval()
70
  with torch.no_grad():
71
  outputs = model.generate(
72
  **inputs,
73
+ max_new_tokens=max_length,
74
+ temperature=max(0.1, temperature), # Ensure minimum temperature
75
+ top_p=max(0.1, top_p), # Ensure minimum top_p
76
  do_sample=True,
77
  pad_token_id=tokenizer.eos_token_id,
78
  eos_token_id=tokenizer.eos_token_id,
79
+ num_return_sequences=1,
80
+ repetition_penalty=1.1,
81
+ no_repeat_ngram_size=2
82
  )
83
 
84
  # Decode output
85
  generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
86
 
87
+ # Extract only the code part (remove the prompt)
88
  if "Code:" in generated_text:
89
+ code = generated_text.split("Code:")[-1].strip()
90
  else:
91
  code = generated_text.strip()
92
 
93
+ # Clean up the output
94
+ if code.startswith(prompt):
95
+ code = code[len(prompt):].strip()
96
+
97
+ return code if code else "❌ No code generated. Try adjusting the parameters."
98
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  except Exception as e:
100
+ return f"❌ Error generating code: {str(e)}"
101
 
102
+ def create_examples():
103
+ """Create example inputs for the interface"""
104
+ return [
105
+ ["create string s", 1, 1, 0.7, 0.9, 100],
106
+ ["read input from user", 1, 2, 0.7, 0.9, 100],
107
+ ["if s is empty", 1, 3, 0.7, 0.9, 100],
108
+ ["print hello world", 2, 4, 0.7, 0.9, 100],
109
+ ["for i from 0 to n", 1, 5, 0.7, 0.9, 100],
110
+ ["declare integer array", 1, 1, 0.5, 0.9, 80],
111
+ ["while condition is true", 2, 10, 0.8, 0.95, 120]
112
+ ]
113
 
114
  # Create Gradio interface
115
+ with gr.Blocks(
116
+ theme=gr.themes.Soft(),
117
+ title="🐍 Pseudo-Code to Code Generator",
118
+ css="""
119
+ .gradio-container {
120
+ max-width: 1200px;
121
+ margin: auto;
122
+ }
123
+ .header {
124
+ text-align: center;
125
+ margin-bottom: 30px;
126
+ }
127
+ .info-box {
128
+ background-color: #f0f8ff;
129
+ padding: 15px;
130
+ border-radius: 10px;
131
+ margin: 10px 0;
132
+ }
133
+ """
134
+ ) as demo:
135
+
136
+ # Header
137
+ gr.HTML("""
138
+ <div class="header">
139
+ <h1>🐍 Pseudo-Code to Code Generator</h1>
140
+ <p>Convert natural language pseudo-code to executable code using fine-tuned GPT-2</p>
141
+ </div>
142
+ """)
143
+
144
+ # Info box
145
+ gr.HTML("""
146
+ <div class="info-box">
147
+ <h3>📋 How to use:</h3>
148
+ <ol>
149
+ <li><strong>Enter pseudocode:</strong> Describe what you want the code to do in natural language</li>
150
+ <li><strong>Set context:</strong> Adjust indent level and line number for better structure</li>
151
+ <li><strong>Tune generation:</strong> Modify temperature and top_p for different creativity levels</li>
152
+ <li><strong>Generate:</strong> Click submit to get your code!</li>
153
+ </ol>
154
+ <p><strong>Note:</strong> This model was trained on the SPOC dataset containing C++ code examples.</p>
155
+ </div>
156
+ """)
157
 
158
  with gr.Row():
159
+ # Left column - Inputs
160
  with gr.Column(scale=1):
 
 
161
  pseudocode_input = gr.Textbox(
162
+ label="📝 Pseudocode",
163
+ placeholder="Enter your pseudocode here... (e.g., 'create string variable s')",
164
+ lines=3,
165
+ value="create string s"
166
  )
167
 
168
  with gr.Row():
169
+ indent_input = gr.Slider(
170
+ minimum=1, maximum=10, value=1, step=1,
171
+ label="🔢 Indent Level",
172
+ info="Indentation level for the code"
 
173
  )
174
+ line_input = gr.Slider(
175
+ minimum=1, maximum=100, value=1, step=1,
176
+ label="📍 Line Number",
 
 
177
  info="Line number in the program"
178
  )
179
 
180
+ gr.Markdown("### 🎛️ Generation Parameters")
181
 
182
  with gr.Row():
183
+ temperature_input = gr.Slider(
184
+ minimum=0.1, maximum=2.0, value=0.7, step=0.1,
185
+ label="🌡️ Temperature",
186
+ info="Higher = more creative, Lower = more focused"
 
 
 
187
  )
188
+ top_p_input = gr.Slider(
189
+ minimum=0.1, maximum=1.0, value=0.9, step=0.05,
190
+ label="🎯 Top-p",
191
+ info="Nucleus sampling parameter"
 
 
 
 
192
  )
193
 
194
+ max_length_input = gr.Slider(
195
+ minimum=50, maximum=512, value=128, step=10,
196
+ label="📏 Max Length",
197
+ info="Maximum number of tokens to generate"
 
 
 
198
  )
199
 
200
  generate_btn = gr.Button("🚀 Generate Code", variant="primary", size="lg")
201
 
202
+ # Right column - Output
203
  with gr.Column(scale=1):
 
 
204
  output = gr.Textbox(
205
+ label="💻 Generated Code",
206
  lines=15,
207
+ placeholder="Generated code will appear here...",
208
  show_copy_button=True
209
  )
210
 
211
+ # Examples section
212
+ gr.Markdown("### 📚 Example Inputs")
213
+ examples = gr.Examples(
214
+ examples=create_examples(),
215
+ inputs=[pseudocode_input, indent_input, line_input, temperature_input, top_p_input, max_length_input],
216
  outputs=output,
217
+ fn=generate_code,
218
+ cache_examples=False
219
  )
220
 
221
+ # Event handlers
222
+ generate_btn.click(
223
+ fn=generate_code,
224
+ inputs=[pseudocode_input, indent_input, line_input, temperature_input, top_p_input, max_length_input],
225
+ outputs=output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  )
227
 
228
+ # Also allow Enter key to generate
229
+ pseudocode_input.submit(
230
+ fn=generate_code,
231
+ inputs=[pseudocode_input, indent_input, line_input, temperature_input, top_p_input, max_length_input],
232
  outputs=output
233
  )
234
+
235
+ # Footer
236
+ gr.HTML("""
237
+ <div style="text-align: center; margin-top: 30px; padding: 20px; border-top: 1px solid #eee;">
238
+ <p>🤖 <strong>Model Details:</strong> Fine-tuned GPT-2 with LoRA on SPOC dataset</p>
239
+ <p>📊 <strong>Training:</strong> Pseudo-code to C++ code generation with structural information</p>
240
+ <p>⚡ <strong>Powered by:</strong> Transformers, Safetensors, and Gradio</p>
241
+ </div>
242
+ """)
243
 
244
+ # Launch configuration
245
  if __name__ == "__main__":
246
+ demo.launch(
247
+ server_name="0.0.0.0", # Required for Hugging Face Spaces
248
+ server_port=7860, # Default port for Spaces
249
+ share=False, # Don't create public links in Spaces
250
+ show_api=False, # Disable API docs for cleaner interface
251
+ show_error=True, # Show errors for debugging
252
+ quiet=False # Show startup logs
253
+ )