Spaces:
Sleeping
Sleeping
File size: 9,789 Bytes
49ab6e1 1ccae3b 3d3e355 1ccae3b 49ab6e1 1ccae3b 49ab6e1 1ccae3b 3d3e355 1ccae3b 49ab6e1 1ccae3b 49ab6e1 486ba3c 49ab6e1 486ba3c 49ab6e1 486ba3c 49ab6e1 486ba3c 49ab6e1 486ba3c 49ab6e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# app.py - Jekyll Master AI Demo (FIXED)
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import warnings
warnings.filterwarnings("ignore")
# ================= CONFIGURATION =================
MODEL_ID = "daffaaditya/jekyll-master-ai" # Model Anda yang sudah diupload
print("=" * 60)
print("π Jekyll Master AI - Live Demo")
print(f"π¦ Using model: {MODEL_ID}")
print("=" * 60)
# ================= LOAD MODEL =================
def load_model():
"""Load model"""
print("π₯ Loading model...")
try:
# Load tokenizer dan model langsung dari repo Anda
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID,
trust_remote_code=True
)
# Load model dengan quantization untuk hemat memory
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto" if torch.cuda.is_available() else None,
trust_remote_code=True,
low_cpu_mem_usage=True
)
print("β
Model loaded successfully!")
if torch.cuda.is_available():
print(f"π± Device: GPU ({torch.cuda.get_device_name(0)})")
else:
print(f"π± Device: CPU")
except Exception as e:
print(f"β Error loading model: {e}")
print("π Using fallback mode...")
# Fallback untuk testing
tokenizer = None
model = None
return tokenizer, model
# Load model - only once at startup
tokenizer, model = load_model()
# ================= GENERATION FUNCTION =================
def generate_jekyll_code(instruction, max_tokens=500, temperature=0.7):
"""Generate Jekyll code"""
try:
print(f"\nπ₯ Instruction: {instruction[:50]}...")
# Jika model tidak loaded, beri contoh
if model is None or tokenizer is None:
example_output = """# Jekyll Master AI - Example Output
# Model is loading or in fallback mode
# Here's an example _config.yml for a tech blog:
title: "Tech Blog"
description: "A blog about technology and programming"
baseurl: ""
url: "https://yourblog.com"
theme: minima
markdown: kramdown
permalink: pretty
author:
name: "Your Name"
email: "you@example.com"
plugins:
- jekyll-feed
- jekyll-seo-tag
# Try the live demo when model is fully loaded!"""
return example_output
# Format prompt sederhana
prompt = f"Generate Jekyll code for: {instruction}\n\nCode:"
# Tokenize
inputs = tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=512
)
if torch.cuda.is_available():
inputs = inputs.to("cuda")
# Generate
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_tokens,
temperature=temperature,
do_sample=True,
top_p=0.9,
repetition_penalty=1.1,
pad_token_id=tokenizer.pad_token_id if tokenizer.pad_token_id else tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id
)
# Decode
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract code after prompt
if prompt in generated:
generated = generated.split(prompt)[-1].strip()
# Clean up
generated = generated.replace(prompt, "").strip()
# Jika output kosong
if not generated or len(generated) < 20:
generated = """# Generated Jekyll Code
# Example _config.yml structure:
title: "Your Site Title"
description: "Your site description"
baseurl: ""
url: "https://yoursite.com"
theme: minima
# For better results, be more specific in your request!"""
print(f"π€ Generated {len(generated)} characters")
return generated
except Exception as e:
error_msg = f"Error: {str(e)}"
print(f"β {error_msg}")
return f"# Error\n{error_msg}\n\nPlease try again or simplify your request."
# ================= GRADIO INTERFACE =================
def create_interface():
"""Create simple Gradio interface"""
# Contoh instruksi
examples = [
["Buat file _config.yml untuk blog teknologi"],
["Buat layout post dengan featured image"],
["Buat include untuk navigation bar"],
["Buat plugin untuk reading time"],
["Buat file Sass untuk buttons"],
]
with gr.Blocks(title="Jekyll Master AI") as demo:
# Custom CSS via HTML element
gr.HTML("""
<style>
.gradio-container {
max-width: 1200px;
margin: 0 auto;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
}
.header {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 10px;
margin-bottom: 20px;
}
.example-btn {
margin: 5px;
}
.footer {
text-align: center;
margin-top: 30px;
padding-top: 20px;
border-top: 1px solid #eee;
color: #666;
}
.gradio-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
}
.gradio-button:hover {
opacity: 0.9;
}
textarea, input {
border-radius: 8px !important;
}
</style>
""")
# Header
gr.HTML("""
<div class="header">
<h1 style="margin: 0">π― Jekyll Master AI</h1>
<p style="font-size: 1.2em; opacity: 0.9">Fine-tuned AI untuk Jekyll Static Site Generator</p>
<p>Generate clean, production-ready code untuk website Jekyll Anda.</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
# Input
instruction = gr.Textbox(
label="Instruksi",
placeholder="Contoh: Buat file _config.yml untuk blog teknologi...",
lines=4
)
# Parameters
with gr.Accordion("βοΈ Parameters", open=False):
max_tokens = gr.Slider(
minimum=100,
maximum=1000,
value=500,
step=50,
label="Max Tokens"
)
temperature = gr.Slider(
minimum=0.1,
maximum=1.5,
value=0.7,
step=0.1,
label="Temperature"
)
# Buttons
with gr.Row():
generate_btn = gr.Button("π Generate Code", variant="primary")
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
with gr.Column(scale=2):
# Output
output = gr.Code(
label="Generated Jekyll Code",
language="yaml",
lines=20
)
# Quick examples
gr.Markdown("### π Contoh Cepat")
with gr.Row():
for text, example in [
("π Config", "Buat _config.yml untuk blog"),
("π¨ Layout", "Buat layout post"),
("π§ Plugin", "Buat plugin Jekyll"),
]:
btn = gr.Button(text, size="sm")
btn.click(lambda ex=example: ex, outputs=instruction)
# Full examples
gr.Examples(
examples=examples,
inputs=instruction,
outputs=output,
fn=generate_jekyll_code,
cache_examples=False,
label="Klik contoh untuk mencoba:"
)
# Footer
gr.HTML(f"""
<div class="footer">
<p>
<strong>Model:</strong> <a href="https://huggingface.co/{MODEL_ID}" target="_blank">{MODEL_ID}</a> |
<strong>Dataset:</strong> <a href="https://huggingface.co/datasets/daffaaditya/jekyll-master-dataset" target="_blank">Jekyll Master Dataset</a> |
<strong>Built with:</strong> Transformers + Gradio
</p>
</div>
""")
# ============ EVENT HANDLERS ============
# Generate button
generate_btn.click(
fn=generate_jekyll_code,
inputs=[instruction, max_tokens, temperature],
outputs=output
)
# Clear button
clear_btn.click(
fn=lambda: ("", ""),
inputs=[],
outputs=[instruction, output]
)
# Enter key submit
instruction.submit(
fn=generate_jekyll_code,
inputs=[instruction, max_tokens, temperature],
outputs=output
)
return demo
# ================= LAUNCH =================
if __name__ == "__main__":
print("π Launching Jekyll Master AI Demo...")
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |