Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,321 +1,253 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
-
from peft import PeftModel, PeftConfig, AutoPeftModelForCausalLM
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 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 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 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
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
print("✓ Model parameters info not available")
|
| 59 |
-
|
| 60 |
-
print("="*70)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
print("\nTrying alternative method: Loading base model + LoRA adapters separately...")
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 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
|
| 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 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
Returns:
|
| 118 |
Generated code string
|
| 119 |
"""
|
| 120 |
try:
|
| 121 |
-
#
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
inputs = {k: v.to(
|
| 130 |
|
| 131 |
-
# Generate
|
| 132 |
model.eval()
|
| 133 |
with torch.no_grad():
|
| 134 |
outputs = model.generate(
|
| 135 |
**inputs,
|
| 136 |
-
|
| 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 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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 |
-
|
| 184 |
-
|
| 185 |
-
[
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
]
|
|
|
|
| 193 |
|
| 194 |
# Create Gradio interface
|
| 195 |
-
with gr.Blocks(
|
| 196 |
-
gr.
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
-
|
| 205 |
-
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 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
|
| 219 |
-
lines=
|
| 220 |
-
|
| 221 |
)
|
| 222 |
|
| 223 |
with gr.Row():
|
| 224 |
-
indent_input = gr.
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
info="Indentation level (0=no indent, 1=first level, etc.)"
|
| 229 |
)
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
label="Line Number",
|
| 233 |
-
value=1,
|
| 234 |
-
precision=0,
|
| 235 |
info="Line number in the program"
|
| 236 |
)
|
| 237 |
|
| 238 |
-
gr.Markdown("###
|
| 239 |
|
| 240 |
with gr.Row():
|
| 241 |
-
|
| 242 |
-
minimum=0.1,
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
step=0.1,
|
| 246 |
-
label="Temperature",
|
| 247 |
-
info="Higher = more creative/random"
|
| 248 |
)
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
value=0.9,
|
| 254 |
-
step=0.05,
|
| 255 |
-
label="Top-p (Nucleus Sampling)",
|
| 256 |
-
info="Probability threshold for sampling"
|
| 257 |
)
|
| 258 |
|
| 259 |
-
|
| 260 |
-
minimum=
|
| 261 |
-
|
| 262 |
-
|
| 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 |
-
|
| 277 |
show_copy_button=True
|
| 278 |
)
|
| 279 |
|
| 280 |
-
|
| 281 |
-
gr.
|
| 282 |
-
|
| 283 |
-
|
|
|
|
| 284 |
outputs=output,
|
| 285 |
-
fn=
|
| 286 |
-
cache_examples=False
|
| 287 |
)
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 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 |
-
#
|
| 313 |
-
|
| 314 |
-
fn=
|
| 315 |
-
inputs=[pseudocode_input, indent_input, line_input,
|
| 316 |
outputs=output
|
| 317 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
# Launch
|
| 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 |
+
)
|