CryptoCreeper commited on
Commit
8dc695b
·
verified ·
1 Parent(s): e306ed6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from diffusers import DiffusionPipeline
4
+ import torch
5
+ import re
6
+ import time
7
+
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+
10
+ models = {
11
+ "Normal": "Qwen/Qwen3-0.6B",
12
+ "Thinking": "Qwen/Qwen2.5-1.5B-Instruct"
13
+ }
14
+
15
+ loaded_models = {}
16
+ loaded_tokenizers = {}
17
+
18
+ image_model_id = "SimianLuo/LCM_Dreamshaper_v7"
19
+ image_pipe = DiffusionPipeline.from_pretrained(image_model_id)
20
+ image_pipe.to(device)
21
+
22
+ def get_chat_model(mode):
23
+ model_id = models[mode]
24
+ if model_id not in loaded_models:
25
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
26
+ model = AutoModelForCausalLM.from_pretrained(
27
+ model_id,
28
+ torch_dtype="auto",
29
+ device_map="auto"
30
+ )
31
+ loaded_models[model_id] = model
32
+ loaded_tokenizers[model_id] = tokenizer
33
+ return loaded_models[model_id], loaded_tokenizers[model_id]
34
+
35
+ def chat_logic(user_input, mode):
36
+ model, tokenizer = get_chat_model(mode)
37
+ messages = [{"role": "user", "content": user_input}]
38
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
39
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
40
+ generated_ids = model.generate(**model_inputs, max_new_tokens=1024)
41
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
42
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
43
+ cleaned_response = re.sub(r'<think>.*?</think>\s*\n?', '', response, flags=re.DOTALL)
44
+ return cleaned_response.strip()
45
+
46
+ def image_logic(prompt, width, height, steps):
47
+ start_time = time.time()
48
+ final_prompt = f"{prompt}, centered and realistic (if applicable)"
49
+ yield "💥 IGNITING... (Image generator AI)...", None
50
+
51
+ image = image_pipe(
52
+ prompt=final_prompt,
53
+ width=int(width),
54
+ height=int(height),
55
+ num_inference_steps=int(steps),
56
+ guidance_scale=8.0,
57
+ lcm_origin_steps=50,
58
+ output_type="pil"
59
+ ).images[0]
60
+
61
+ duration = round(time.time() - start_time, 2)
62
+ yield f"💥 EXPLODED in {duration}s", image
63
+
64
+ creeper_css = """
65
+ body { background-color: #000000; }
66
+ .gradio-container { background-color: #1e1e1e; border: 10px solid #2e8b57 !important; font-family: 'Courier New', Courier, monospace; color: #00ff00; }
67
+ footer { display: none !important; }
68
+ .gr-button-primary { background-color: #4A7023 !important; border: 4px solid #000 !important; color: white !important; font-weight: bold; text-transform: uppercase; }
69
+ .gr-button-primary:hover { background-color: #5ea032 !important; box-shadow: 0 0 20px #2e8b57; }
70
+ label span { color: #2e8b57 !important; font-weight: bold; font-size: 1.2em; }
71
+ textarea, input, .gr-box, .gr-input { background-color: #2e2e2e !important; color: #00ff00 !important; border: 3px solid #4A7023 !important; }
72
+ .tabs { border-bottom: 5px solid #4A7023 !important; }
73
+ .tab-nav button.selected { background-color: #4A7023 !important; color: white !important; }
74
+ """
75
+
76
+ with gr.Blocks(css=creeper_css, title="CREEPER AI HUB") as demo:
77
+ gr.Markdown("# 🟩 CREEPER AI HUB 🟩")
78
+
79
+ with gr.Tabs():
80
+ with gr.TabItem("SSSSS-CHAT"):
81
+ gr.Markdown("### Qwen Chat System")
82
+ with gr.Row():
83
+ mode_radio = gr.Radio(choices=["Normal", "Thinking"], value="Normal", label="Select Brain Mode")
84
+ with gr.Column():
85
+ chat_input = gr.Textbox(lines=4, placeholder="Ssssss... Talk to the Creeper...", label="Message")
86
+ chat_output = gr.Textbox(label="Creeper Says")
87
+ chat_btn = gr.Button("EXPLODE TEXT", variant="primary")
88
+
89
+ chat_btn.click(fn=chat_logic, inputs=[chat_input, mode_radio], outputs=chat_output)
90
+
91
+ with gr.TabItem("TNT-IMAGE"):
92
+ gr.Markdown("### Image Generator System")
93
+ with gr.Row():
94
+ with gr.Column(scale=1):
95
+ img_prompt = gr.Textbox(label="Visual Idea", placeholder="A pixelated forest...", lines=3)
96
+ with gr.Row():
97
+ w_slider = gr.Slider(256, 768, 512, step=64, label="Block Width")
98
+ h_slider = gr.Slider(256, 768, 512, step=64, label="Block Height")
99
+ s_slider = gr.Slider(4, 12, 5, step=1, label="Detonation Steps")
100
+ img_btn = gr.Button("EXPLODE IMAGE", variant="primary")
101
+ with gr.Column(scale=1):
102
+ img_status = gr.Markdown("### Status: 🟢 Armed")
103
+ img_output = gr.Image(label="Rendered Loot")
104
+
105
+ img_btn.click(fn=image_logic, inputs=[img_prompt, w_slider, h_slider, s_slider], outputs=[img_status, img_output])
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()