Views
No views yet
tokenizer_config.json. You don't need to type complex instructions. Just input a cat, and it outputs the full prompt automatically.a sexy model at home beda sexy model at home bed, close-up shot of a woman lying on a luxurious velvet bed with soft golden lighting, elegant floral decor, minimalist modern furniture, subtle candlelight flicker, realistic textures, sensual atmosphere, high-quality photography, dramatic shadows, and a calm yet intimate mood.
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model_name = "aifeifei798/Qwen3-1.7B-Flux-Prompt" # Replace with your actual repo name
5
6# Load the tokenizer and the model
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForCausalLM.from_pretrained(
9 model_name,
10 torch_dtype="auto",
11 device_map="auto"
12)
13
14def generate_flux_prompt(prompt):
15
16 messages = [{"role": "user", "content": prompt}]
17
18 # Applying the template
19 text = tokenizer.apply_chat_template(
20 messages,
21 tokenize=False,
22 add_generation_prompt=True,
23 # enable_thinking=False
24 )
25
26 model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
27
28 # Generation with optimized parameters for Flux prompting
29 generated_ids = model.generate(
30 **model_inputs,
31 max_new_tokens=512, # [Key] 512 tokens are sufficient for a detailed visual description.
32 do_sample=True, # [Required] Enable sampling to allow for creative variations.
33 temperature=0.7, # [Creativity] 0.7 offers a balance between imaginative detail and coherence.
34 top_p=0.9, # [Focus] Nucleus sampling: filters out very unlikely words.
35 top_k=50, # [Stability] Limits vocabulary to top 50 tokens to prevent hallucinations.
36 repetition_penalty=1.15, # [Variety] Slight penalty to reduce repetitive phrases without breaking grammar.
37 no_repeat_ngram_size=3,
38 )
39
40 # Extracting the newly generated tokens only
41 output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
42
43 # Parsing logic to handle potential reasoning content (if using a reasoning-capable base model)
44 # Looks for the </think> token ID (usually 151668 in Qwen architecture)
45 try:
46 index = len(output_ids) - output_ids[::-1].index(151668)
47 except ValueError:
48 index = 0 # No thinking trace found, output starts from the beginning
49
50 # Decode the final prompt
51 # thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip()
52 content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip()
53
54 return content
55
56# Example Usage
57# if __name__ == "__main__":
58 # user_input = "a sexy model at new york"
59 # print(f"Input: {user_input}\n")
60 # print(f"Generated Flux Prompt:\n{generate_flux_prompt(user_input)}")flux_prompt (Alpaca format)temperature: 0.7 (Recommended)repetition_penalty: 1.05 - 1.1 (Recommended)do_sample=True with temperature around 0.7 to get diverse results every time you run it.