Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import re
|
| 4 |
+
from transformers import AutoModelForCausalLM, PreTrainedTokenizerFast
|
| 5 |
+
|
| 6 |
+
# ==============================================================================
|
| 7 |
+
# 1. MODEL & TOKENIZER INITIALIZATION
|
| 8 |
+
# ==============================================================================
|
| 9 |
+
# Menggunakan repository organisasi barumu
|
| 10 |
+
repo_id = "VoidSpac3s/Lumina-Medium-390M"
|
| 11 |
+
|
| 12 |
+
print("Loading Tokenizer...")
|
| 13 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained(repo_id)
|
| 14 |
+
|
| 15 |
+
print("Loading Lumina-Medium-390M to CPU...")
|
| 16 |
+
# Tetap menggunakan float32 karena ini yang paling stabil untuk HF Spaces CPU Basic
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
repo_id,
|
| 19 |
+
dtype=torch.float32
|
| 20 |
+
)
|
| 21 |
+
model.to("cpu")
|
| 22 |
+
|
| 23 |
+
# ==============================================================================
|
| 24 |
+
# 2. INFERENCE FUNCTION
|
| 25 |
+
# ==============================================================================
|
| 26 |
+
def generate_prompt(raw_prompt, temperature, top_k, top_p, rep_penalty, max_tokens):
|
| 27 |
+
if not raw_prompt.strip():
|
| 28 |
+
return "⚠️ Please enter a base prompt first."
|
| 29 |
+
|
| 30 |
+
prompt_full = f"<|im_start|>user\nEnhance this prompt: {raw_prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 31 |
+
|
| 32 |
+
input_ids = tokenizer(prompt_full, return_tensors="pt").input_ids.to("cpu")
|
| 33 |
+
input_length = input_ids.shape[1]
|
| 34 |
+
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
output = model.generate(
|
| 37 |
+
input_ids,
|
| 38 |
+
max_new_tokens=max_tokens,
|
| 39 |
+
temperature=temperature,
|
| 40 |
+
top_k=int(top_k),
|
| 41 |
+
top_p=top_p,
|
| 42 |
+
repetition_penalty=rep_penalty,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 45 |
+
pad_token_id=tokenizer.pad_token_id
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Hanya ambil token yang baru saja di-generate
|
| 49 |
+
generated_tokens = output[0][input_length:]
|
| 50 |
+
|
| 51 |
+
enhanced_prompt = tokenizer.decode(generated_tokens, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
| 52 |
+
|
| 53 |
+
# Fallback pembersihan simbol Tokenizer
|
| 54 |
+
enhanced_prompt = enhanced_prompt.replace("Ġ", " ").replace("Ċ", "\n").replace("<|im_end|>", "").strip()
|
| 55 |
+
|
| 56 |
+
# FIX: Membersihkan spasi aneh sebelum tanda baca (contoh: "eyes , highly" menjadi "eyes, highly")
|
| 57 |
+
enhanced_prompt = re.sub(r'\s+([.,!?])', r'\1', enhanced_prompt)
|
| 58 |
+
|
| 59 |
+
return enhanced_prompt
|
| 60 |
+
|
| 61 |
+
# ==============================================================================
|
| 62 |
+
# 3. GRADIO UI
|
| 63 |
+
# ==============================================================================
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 65 |
+
gr.Markdown(
|
| 66 |
+
"""
|
| 67 |
+
# ✨ Lumina-Medium Prompt Enhancer
|
| 68 |
+
**Lumina-Medium-390M** is a custom lightweight language model (~390M Parameters) trained specifically on 2.4 Million data points to enhance base ideas into highly detailed, context-aware image generation prompts.
|
| 69 |
+
"""
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column(scale=2):
|
| 74 |
+
input_text = gr.Textbox(
|
| 75 |
+
label="Base Prompt",
|
| 76 |
+
placeholder="Example: A futuristic cyberpunk samurai cat",
|
| 77 |
+
lines=2
|
| 78 |
+
)
|
| 79 |
+
generate_btn = gr.Button("🚀 Generate Prompt", variant="primary")
|
| 80 |
+
|
| 81 |
+
with gr.Column(scale=3):
|
| 82 |
+
output_text = gr.Textbox(
|
| 83 |
+
label="Lumina Enhanced Prompt",
|
| 84 |
+
lines=6,
|
| 85 |
+
interactive=False
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Advanced Settings
|
| 89 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 90 |
+
with gr.Row():
|
| 91 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature (Creativity)")
|
| 92 |
+
top_k = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top-K")
|
| 93 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-P")
|
| 94 |
+
with gr.Row():
|
| 95 |
+
rep_penalty = gr.Slider(minimum=1.0, maximum=2.0, value=1.1, step=0.05, label="Repetition Penalty")
|
| 96 |
+
max_tokens = gr.Slider(minimum=50, maximum=256, value=150, step=10, label="Max New Tokens")
|
| 97 |
+
|
| 98 |
+
# Button Logic
|
| 99 |
+
generate_btn.click(
|
| 100 |
+
fn=generate_prompt,
|
| 101 |
+
inputs=[input_text, temperature, top_k, top_p, rep_penalty, max_tokens],
|
| 102 |
+
outputs=output_text
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Run the app
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch()
|