hamxaameer commited on
Commit
262de9f
·
1 Parent(s): b22edd6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +620 -0
app.py ADDED
@@ -0,0 +1,620 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import torch
4
+ import numpy as np
5
+ from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
6
+ from nltk.tokenize import word_tokenize
7
+ import nltk
8
+ import time
9
+ import os
10
+
11
+ # Download required NLTK data
12
+ try:
13
+ nltk.download('punkt', quiet=True)
14
+ nltk.download('punkt_tab', quiet=True)
15
+ except:
16
+ pass
17
+
18
+ # Global variables to store loaded model
19
+ loaded_model = None
20
+ loaded_tokenizer = None
21
+ loaded_config = None
22
+ generation_history = []
23
+
24
+ def load_model_from_pickle(pickle_file):
25
+ """Load model from uploaded pickle file"""
26
+ global loaded_model, loaded_tokenizer, loaded_config
27
+
28
+ try:
29
+ # Load pickle file
30
+ with open(pickle_file.name, 'rb') as f:
31
+ model_package = pickle.load(f)
32
+
33
+ loaded_model = model_package['model']
34
+ loaded_tokenizer = model_package['tokenizer']
35
+ loaded_config = model_package['config']
36
+
37
+ # Set model to evaluation mode
38
+ loaded_model.eval()
39
+
40
+ # Move to appropriate device
41
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
42
+ loaded_model = loaded_model.to(device)
43
+
44
+ config_info = f"""✅ Model loaded successfully!
45
+
46
+ 📊 Model Configuration:
47
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
48
+ • Base Model: {loaded_config.get('model_name', 'GPT-2')}
49
+ • Training Epochs: {loaded_config.get('num_epochs', 'N/A')}
50
+ • Training Samples: {loaded_config.get('training_samples', 'N/A'):,}
51
+ • Validation Samples: {loaded_config.get('validation_samples', 'N/A'):,}
52
+ • BLEU Score: {loaded_config.get('bleu_score', 0):.4f}
53
+ • Perplexity: {loaded_config.get('perplexity', 0):.2f}
54
+ • Final Loss: {loaded_config.get('final_loss', 0):.4f}
55
+ • Device: {device}
56
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
57
+
58
+ 🚀 Model is ready to generate code!
59
+ """
60
+
61
+ return config_info
62
+
63
+ except Exception as e:
64
+ return f"❌ Error loading model: {str(e)}\n\nPlease ensure you uploaded the correct best_model.pkl file."
65
+
66
+ def calculate_bleu_score(reference, hypothesis):
67
+ """Calculate BLEU score between reference and generated code"""
68
+ try:
69
+ # Tokenize
70
+ ref_tokens = word_tokenize(reference.lower())
71
+ hyp_tokens = word_tokenize(hypothesis.lower())
72
+
73
+ # Calculate BLEU with smoothing
74
+ smooth = SmoothingFunction()
75
+ bleu_1 = sentence_bleu([ref_tokens], hyp_tokens, weights=(1, 0, 0, 0), smoothing_function=smooth.method1)
76
+ bleu_2 = sentence_bleu([ref_tokens], hyp_tokens, weights=(0.5, 0.5, 0, 0), smoothing_function=smooth.method1)
77
+ bleu_3 = sentence_bleu([ref_tokens], hyp_tokens, weights=(0.33, 0.33, 0.33, 0), smoothing_function=smooth.method1)
78
+ bleu_4 = sentence_bleu([ref_tokens], hyp_tokens, weights=(0.25, 0.25, 0.25, 0.25), smoothing_function=smooth.method1)
79
+
80
+ return bleu_1, bleu_2, bleu_3, bleu_4
81
+ except Exception as e:
82
+ return 0.0, 0.0, 0.0, 0.0
83
+
84
+ def calculate_code_metrics(reference, generated):
85
+ """Calculate various code similarity metrics"""
86
+ try:
87
+ # Length ratio
88
+ len_ratio = len(generated) / max(len(reference), 1)
89
+
90
+ # Word overlap
91
+ ref_words = set(reference.lower().split())
92
+ gen_words = set(generated.lower().split())
93
+
94
+ if len(ref_words) > 0:
95
+ precision = len(ref_words.intersection(gen_words)) / len(gen_words) if len(gen_words) > 0 else 0
96
+ recall = len(ref_words.intersection(gen_words)) / len(ref_words)
97
+ f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
98
+ else:
99
+ precision = recall = f1 = 0
100
+
101
+ # Character-level similarity
102
+ char_overlap = sum(1 for c in generated if c in reference) / max(len(generated), 1)
103
+
104
+ return {
105
+ 'length_ratio': len_ratio,
106
+ 'precision': precision,
107
+ 'recall': recall,
108
+ 'f1_score': f1,
109
+ 'char_overlap': char_overlap
110
+ }
111
+ except Exception as e:
112
+ return {
113
+ 'length_ratio': 0,
114
+ 'precision': 0,
115
+ 'recall': 0,
116
+ 'f1_score': 0,
117
+ 'char_overlap': 0
118
+ }
119
+
120
+ def generate_code_from_pseudo(pseudo_code, max_length, temperature, top_k, top_p, num_sequences, reference_code):
121
+ """Generate code from pseudo-code using loaded model"""
122
+ global loaded_model, loaded_tokenizer, generation_history
123
+
124
+ if loaded_model is None or loaded_tokenizer is None:
125
+ return "❌ Please upload and load a model first!", "", "", ""
126
+
127
+ if not pseudo_code.strip():
128
+ return "❌ Please enter pseudo-code description!", "", "", ""
129
+
130
+ try:
131
+ start_time = time.time()
132
+
133
+ # Format input
134
+ prompt = f"<PSEUDO> {pseudo_code.strip()} <SEP> <CODE>"
135
+
136
+ # Tokenize
137
+ device = next(loaded_model.parameters()).device
138
+ inputs = loaded_tokenizer(prompt, return_tensors='pt').to(device)
139
+
140
+ # Generate (ensure type safety for parameters)
141
+ with torch.no_grad():
142
+ outputs = loaded_model.generate(
143
+ **inputs,
144
+ max_length=int(max_length),
145
+ temperature=float(temperature),
146
+ top_k=int(top_k),
147
+ top_p=float(top_p),
148
+ do_sample=True,
149
+ num_return_sequences=int(num_sequences),
150
+ pad_token_id=loaded_tokenizer.pad_token_id,
151
+ eos_token_id=loaded_tokenizer.eos_token_id,
152
+ )
153
+
154
+ generation_time = time.time() - start_time
155
+
156
+ # Decode all sequences
157
+ generated_codes = []
158
+ for output in outputs:
159
+ generated = loaded_tokenizer.decode(output, skip_special_tokens=False)
160
+
161
+ # Extract code part
162
+ if '<CODE>' in generated:
163
+ code = generated.split('<CODE>')[-1].strip()
164
+ # Remove special tokens
165
+ code = code.replace('<PAD>', '').replace('<SEP>', '').strip()
166
+ else:
167
+ code = generated
168
+
169
+ generated_codes.append(code)
170
+
171
+ # Use the first generated code as primary output
172
+ primary_code = generated_codes[0]
173
+
174
+ # Calculate metrics if reference code is provided
175
+ metrics_output = ""
176
+ bleu_output = ""
177
+
178
+ if reference_code and reference_code.strip():
179
+ # Calculate BLEU scores
180
+ bleu_1, bleu_2, bleu_3, bleu_4 = calculate_bleu_score(reference_code, primary_code)
181
+
182
+ bleu_output = f"""📊 BLEU Scores:
183
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
184
+ • BLEU-1 (Unigram): {bleu_1:.4f} ({bleu_1*100:.2f}%)
185
+ • BLEU-2 (Bigram): {bleu_2:.4f} ({bleu_2*100:.2f}%)
186
+ • BLEU-3 (Trigram): {bleu_3:.4f} ({bleu_3*100:.2f}%)
187
+ • BLEU-4 (4-gram): {bleu_4:.4f} ({bleu_4*100:.2f}%)
188
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
189
+
190
+ 💡 Interpretation:
191
+ • BLEU > 0.4: Excellent match
192
+ • BLEU 0.3-0.4: Good match
193
+ • BLEU 0.2-0.3: Fair match
194
+ • BLEU < 0.2: Poor match
195
+ """
196
+
197
+ # Calculate additional metrics
198
+ code_metrics = calculate_code_metrics(reference_code, primary_code)
199
+
200
+ metrics_output = f"""📈 Additional Metrics:
201
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
202
+ • Length Ratio: {code_metrics['length_ratio']:.3f}
203
+ • Precision: {code_metrics['precision']:.4f} ({code_metrics['precision']*100:.2f}%)
204
+ • Recall: {code_metrics['recall']:.4f} ({code_metrics['recall']*100:.2f}%)
205
+ • F1-Score: {code_metrics['f1_score']:.4f} ({code_metrics['f1_score']*100:.2f}%)
206
+ • Character Overlap: {code_metrics['char_overlap']:.4f} ({code_metrics['char_overlap']*100:.2f}%)
207
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
208
+
209
+ ⏱️ Generation Time: {generation_time:.2f}s
210
+ 📝 Sequences Generated: {num_sequences}
211
+ 🔢 Output Length: {len(primary_code)} characters
212
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
213
+ """
214
+ else:
215
+ metrics_output = f"""⏱️ Generation Time: {generation_time:.2f}s
216
+ 📝 Sequences Generated: {num_sequences}
217
+ 🔢 Output Length: {len(primary_code)} characters
218
+
219
+ 💡 Tip: Provide reference code to see BLEU scores and similarity metrics!
220
+ """
221
+
222
+ # Format alternative sequences
223
+ alternatives = ""
224
+ if num_sequences > 1:
225
+ alternatives = "🔄 Alternative Generations:\n" + "━"*50 + "\n\n"
226
+ for i, code in enumerate(generated_codes[1:], 2):
227
+ alternatives += f"Variation {i}:\n```python\n{code}\n```\n\n"
228
+
229
+ # Add to history
230
+ generation_history.append({
231
+ 'pseudo': pseudo_code,
232
+ 'generated': primary_code,
233
+ 'bleu_4': bleu_4 if reference_code else None,
234
+ 'time': generation_time
235
+ })
236
+
237
+ return primary_code, metrics_output, bleu_output, alternatives
238
+
239
+ except Exception as e:
240
+ return f"❌ Error generating code: {str(e)}", "", "", ""
241
+
242
+ def show_examples(example_name):
243
+ """Load example pseudo-code"""
244
+ examples = {
245
+ "Basic Loop": "create a list of numbers from 1 to 10",
246
+ "Function Definition": "define a function to calculate the sum of two numbers",
247
+ "List Iteration": "iterate through a list and print each element",
248
+ "Conditional Check": "check if a number is even or odd",
249
+ "Sorting": "sort a list in descending order",
250
+ "Maximum Element": "create a function to find maximum element in array",
251
+ "Binary Search": "implement binary search algorithm",
252
+ "Factorial": "create a recursive function to calculate factorial",
253
+ "Palindrome": "check if a string is palindrome",
254
+ "Fibonacci": "generate fibonacci sequence up to n terms"
255
+ }
256
+ return examples.get(example_name, "")
257
+
258
+ def clear_all():
259
+ """Clear all inputs and outputs"""
260
+ return "", "", "", "", "", 150, 0.7, 50, 0.95, 1
261
+
262
+ def show_history():
263
+ """Display generation history"""
264
+ if not generation_history:
265
+ return "No generation history yet. Start generating code!"
266
+
267
+ history_text = "📜 Generation History:\n" + "="*60 + "\n\n"
268
+
269
+ for i, entry in enumerate(reversed(generation_history[-10:]), 1): # Show last 10
270
+ history_text += f"{i}. Pseudo: {entry['pseudo'][:60]}...\n"
271
+ history_text += f" Time: {entry['time']:.2f}s"
272
+ if entry['bleu_4'] is not None:
273
+ history_text += f" | BLEU-4: {entry['bleu_4']:.4f}"
274
+ history_text += f"\n Code: {entry['generated'][:80]}...\n\n"
275
+
276
+ return history_text
277
+
278
+ # Create Gradio interface with custom CSS
279
+ custom_css = """
280
+ .gradio-container {
281
+ font-family: 'Arial', sans-serif;
282
+ }
283
+ .output-code {
284
+ font-family: 'Courier New', monospace;
285
+ font-size: 14px;
286
+ }
287
+ .metrics-box {
288
+ background-color: #f0f8ff;
289
+ border-radius: 8px;
290
+ padding: 10px;
291
+ }
292
+ """
293
+
294
+ with gr.Blocks(title="🚀 GPT-2 Pseudo-Code to Code Generator", theme=gr.themes.Soft(), css=custom_css) as demo:
295
+
296
+ gr.Markdown("""
297
+ # 🚀 GPT-2 Pseudo-Code to Python Code Generator
298
+
299
+ **Transform natural language descriptions into executable Python code using fine-tuned GPT-2!**
300
+
301
+ This model is trained on the SPOC (Search-based Pseudo-code to Code) dataset and can generate Python code from pseudo-code descriptions.
302
+ """)
303
+
304
+ with gr.Tabs():
305
+ # Tab 1: Code Generation
306
+ with gr.Tab("💻 Code Generation"):
307
+ with gr.Row():
308
+ with gr.Column(scale=1):
309
+ gr.Markdown("### 📥 Step 1: Load Model")
310
+ model_file = gr.File(
311
+ label="Upload best_model.pkl",
312
+ file_types=[".pkl"],
313
+ type="filepath"
314
+ )
315
+ load_btn = gr.Button("🔄 Load Model", variant="primary", size="lg")
316
+ model_status = gr.Textbox(
317
+ label="Model Status",
318
+ lines=15,
319
+ interactive=False,
320
+ placeholder="Upload model file and click 'Load Model'..."
321
+ )
322
+
323
+ gr.Markdown("---")
324
+
325
+ with gr.Row():
326
+ with gr.Column(scale=1):
327
+ gr.Markdown("### ✍️ Step 2: Enter Pseudo-Code")
328
+
329
+ # Example selector
330
+ with gr.Row():
331
+ example_dropdown = gr.Dropdown(
332
+ choices=["Basic Loop", "Function Definition", "List Iteration",
333
+ "Conditional Check", "Sorting", "Maximum Element",
334
+ "Binary Search", "Factorial", "Palindrome", "Fibonacci"],
335
+ label="📚 Load Example",
336
+ value=None
337
+ )
338
+
339
+ pseudo_input = gr.Textbox(
340
+ label="Pseudo-Code Description",
341
+ placeholder="Example: create a function to calculate factorial of a number",
342
+ lines=4
343
+ )
344
+
345
+ reference_code = gr.Code(
346
+ label="Reference Code (Optional - for BLEU score calculation)",
347
+ language="python",
348
+ lines=4,
349
+ placeholder="Paste reference code here to calculate BLEU scores..."
350
+ )
351
+
352
+ gr.Markdown("### ⚙️ Generation Parameters")
353
+ with gr.Row():
354
+ max_length = gr.Slider(
355
+ minimum=50,
356
+ maximum=500,
357
+ value=150,
358
+ step=10,
359
+ label="Max Length",
360
+ info="Maximum tokens to generate"
361
+ )
362
+ temperature = gr.Slider(
363
+ minimum=0.1,
364
+ maximum=1.5,
365
+ value=0.7,
366
+ step=0.1,
367
+ label="Temperature",
368
+ info="Higher = more creative"
369
+ )
370
+
371
+ with gr.Row():
372
+ top_k = gr.Slider(
373
+ minimum=10,
374
+ maximum=100,
375
+ value=50,
376
+ step=5,
377
+ label="Top-K",
378
+ info="Vocabulary filtering"
379
+ )
380
+ top_p = gr.Slider(
381
+ minimum=0.5,
382
+ maximum=1.0,
383
+ value=0.95,
384
+ step=0.05,
385
+ label="Top-P",
386
+ info="Nucleus sampling"
387
+ )
388
+
389
+ num_sequences = gr.Slider(
390
+ minimum=1,
391
+ maximum=5,
392
+ value=1,
393
+ step=1,
394
+ label="Number of Variations",
395
+ info="Generate multiple versions"
396
+ )
397
+
398
+ with gr.Row():
399
+ generate_btn = gr.Button("✨ Generate Code", variant="primary", size="lg")
400
+ clear_btn = gr.Button("🗑️ Clear All", variant="secondary")
401
+
402
+ with gr.Column(scale=1):
403
+ gr.Markdown("### 💻 Generated Python Code")
404
+ code_output = gr.Code(
405
+ label="Generated Code",
406
+ language="python",
407
+ lines=12,
408
+ elem_classes="output-code"
409
+ )
410
+
411
+ with gr.Row():
412
+ with gr.Column():
413
+ metrics_output = gr.Textbox(
414
+ label="📊 Performance Metrics",
415
+ lines=8,
416
+ interactive=False,
417
+ elem_classes="metrics-box"
418
+ )
419
+ with gr.Column():
420
+ bleu_output = gr.Textbox(
421
+ label="🎯 BLEU Scores",
422
+ lines=8,
423
+ interactive=False,
424
+ elem_classes="metrics-box"
425
+ )
426
+
427
+ alternatives_output = gr.Markdown(
428
+ label="🔄 Alternative Generations"
429
+ )
430
+
431
+ # Tab 2: Information & Guide
432
+ with gr.Tab("📖 Guide & Examples"):
433
+ gr.Markdown("""
434
+ ## 📚 How to Use
435
+
436
+ ### 1️⃣ Load Your Model
437
+ - Upload the `best_model.pkl` file (trained GPT-2 model)
438
+ - Click "Load Model" and wait for confirmation
439
+ - You'll see model configuration and training metrics
440
+
441
+ ### 2️⃣ Generate Code
442
+ - **Quick Start**: Select an example from the dropdown
443
+ - **Custom Input**: Type your own pseudo-code description
444
+ - **Optional**: Add reference code to calculate BLEU scores
445
+ - Adjust generation parameters for different outputs
446
+ - Click "Generate Code"
447
+
448
+ ### 3️⃣ Understand the Metrics
449
+
450
+ #### 🎯 BLEU Score (Bilingual Evaluation Understudy)
451
+ - Measures similarity between generated and reference code
452
+ - **BLEU-1**: Word-level similarity (unigrams)
453
+ - **BLEU-2**: 2-word phrase similarity (bigrams)
454
+ - **BLEU-3**: 3-word phrase similarity (trigrams)
455
+ - **BLEU-4**: 4-word phrase similarity (most comprehensive)
456
+
457
+ **Score Interpretation:**
458
+ - 🟢 **> 0.4**: Excellent match - Generated code is very similar to reference
459
+ - 🟡 **0.3-0.4**: Good match - Code captures most key elements
460
+ - 🟠 **0.2-0.3**: Fair match - Some similarity exists
461
+ - 🔴 **< 0.2**: Poor match - Significant differences
462
+
463
+ #### 📈 Additional Metrics
464
+ - **Precision**: How many generated words appear in reference
465
+ - **Recall**: How many reference words appear in generated code
466
+ - **F1-Score**: Harmonic mean of precision and recall
467
+ - **Length Ratio**: Generated vs reference code length
468
+ - **Character Overlap**: Character-level similarity
469
+
470
+ ### 🎛️ Generation Parameters
471
+
472
+ | Parameter | Low Value | High Value | Use Case |
473
+ |-----------|-----------|------------|----------|
474
+ | **Temperature** | 0.1-0.3 | 0.8-1.2 | Low: Deterministic, focused<br>High: Creative, diverse |
475
+ | **Top-K** | 10-30 | 60-100 | Low: Conservative choices<br>High: More variety |
476
+ | **Top-P** | 0.5-0.8 | 0.9-1.0 | Low: Safe predictions<br>High: Exploratory |
477
+ | **Max Length** | 50-100 | 200-500 | Short: Simple code<br>Long: Complex implementations |
478
+
479
+ ---
480
+
481
+ ## 💡 Example Pseudo-Code Prompts
482
+
483
+ ### Basic Operations
484
+ ```
485
+ create a list of numbers from 1 to 10
486
+ define a function to calculate the sum of two numbers
487
+ iterate through a list and print each element
488
+ ```
489
+
490
+ ### Conditionals & Logic
491
+ ```
492
+ check if a number is even or odd
493
+ find the maximum of three numbers
494
+ validate if a string is empty
495
+ ```
496
+
497
+ ### Data Structures
498
+ ```
499
+ sort a list in descending order
500
+ remove duplicates from a list
501
+ merge two dictionaries
502
+ ```
503
+
504
+ ### Algorithms
505
+ ```
506
+ implement binary search algorithm
507
+ create a recursive function to calculate factorial
508
+ generate fibonacci sequence up to n terms
509
+ check if a string is palindrome
510
+ ```
511
+
512
+ ### Advanced
513
+ ```
514
+ create a class to represent a student with name and grades
515
+ implement a function to read CSV file and return dataframe
516
+ create a decorator to measure function execution time
517
+ ```
518
+
519
+ ---
520
+
521
+ ## 🎓 About the Model
522
+
523
+ This model is fine-tuned on the **SPOC (Search-based Pseudo-code to Code)** dataset:
524
+ - 📄 Paper: [SPOC: Search-based Pseudo-code to Code](https://arxiv.org/pdf/1906.04908)
525
+ - 🏛️ Source: Stanford University
526
+ - 🤖 Base Model: GPT-2 (Decoder-Only Transformer)
527
+ - 📊 Training: 10,000+ pseudo-code to code pairs
528
+ - 🎯 Task: Causal Language Modeling
529
+
530
+ ---
531
+
532
+ ## ⚠️ Limitations
533
+
534
+ - Model may not handle very complex algorithms perfectly
535
+ - Generated code should be tested before production use
536
+ - Best results with clear, specific pseudo-code descriptions
537
+ - Model trained on C++ code, adapted for Python generation
538
+
539
+ ---
540
+
541
+ ## 🤝 Tips for Best Results
542
+
543
+ 1. ✅ **Be Specific**: "create a function to sort list in ascending order" vs "sort list"
544
+ 2. ✅ **Use Action Words**: "create", "define", "implement", "calculate"
545
+ 3. ✅ **Mention Data Types**: "list", "string", "dictionary", "integer"
546
+ 4. ✅ **Include Details**: "recursive function" vs just "function"
547
+ 5. ✅ **Try Variations**: Generate multiple times with different temperatures
548
+
549
+ """)
550
+
551
+ # Tab 3: History
552
+ with gr.Tab("📜 History"):
553
+ gr.Markdown("## 📊 Generation History")
554
+ history_display = gr.Textbox(
555
+ label="Recent Generations",
556
+ lines=20,
557
+ interactive=False
558
+ )
559
+ refresh_history_btn = gr.Button("🔄 Refresh History", variant="secondary")
560
+
561
+ gr.Markdown("""
562
+ ---
563
+ ### 🌟 Features
564
+ - ✅ Upload and use custom trained models
565
+ - ✅ BLEU score calculation for quality assessment
566
+ - ✅ Multiple evaluation metrics (Precision, Recall, F1)
567
+ - ✅ Generate multiple code variations
568
+ - ✅ Real-time performance tracking
569
+ - ✅ Example prompts library
570
+ - ✅ Generation history
571
+
572
+ ### 📝 Citation
573
+ If you use this model, please cite:
574
+ ```
575
+ @article{kulal2019spoc,
576
+ title={SPOC: Search-based Pseudo-code to Code},
577
+ author={Kulal, Sumith and Pasupat, Panupong and Chandra, Kartik and Lee, Mina and Padon, Oded and Aiken, Alex and Liang, Percy},
578
+ journal={arXiv preprint arXiv:1906.04908},
579
+ year={2019}
580
+ }
581
+ ```
582
+
583
+ **Built with ❤️ using HuggingFace Transformers & Gradio**
584
+ """)
585
+
586
+ # Event handlers
587
+ load_btn.click(
588
+ fn=load_model_from_pickle,
589
+ inputs=[model_file],
590
+ outputs=[model_status]
591
+ )
592
+
593
+ example_dropdown.change(
594
+ fn=show_examples,
595
+ inputs=[example_dropdown],
596
+ outputs=[pseudo_input]
597
+ )
598
+
599
+ generate_btn.click(
600
+ fn=generate_code_from_pseudo,
601
+ inputs=[pseudo_input, max_length, temperature, top_k, top_p, num_sequences, reference_code],
602
+ outputs=[code_output, metrics_output, bleu_output, alternatives_output]
603
+ )
604
+
605
+ clear_btn.click(
606
+ fn=clear_all,
607
+ inputs=[],
608
+ outputs=[pseudo_input, reference_code, code_output, metrics_output, bleu_output,
609
+ max_length, temperature, top_k, top_p, num_sequences]
610
+ )
611
+
612
+ refresh_history_btn.click(
613
+ fn=show_history,
614
+ inputs=[],
615
+ outputs=[history_display]
616
+ )
617
+
618
+ # Launch the interface
619
+ if __name__ == "__main__":
620
+ demo.launch(share=False)