CryptoCreeper commited on
Commit
ecda46a
Β·
verified Β·
1 Parent(s): 60733cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -140
app.py CHANGED
@@ -4,7 +4,6 @@ from diffusers import DiffusionPipeline
4
  import torch
5
  import re
6
  import time
7
- import gc
8
  import soundfile as sf
9
  from qwen_tts import Qwen3TTSModel
10
  from langdetect import detect
@@ -12,19 +11,18 @@ import os
12
 
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
 
15
- # --- Chat ---
16
  chat_models = {
17
  "Normal": "Qwen/Qwen3-0.6B",
18
  "Thinking": "Qwen/Qwen2.5-1.5B-Instruct"
19
  }
20
  loaded_chat_models = {}
21
  loaded_chat_tokenizers = {}
22
- chat_model_loaded = {}
23
 
24
  def load_chat_model(mode):
25
  model_id = chat_models[mode]
26
  if model_id not in loaded_chat_models:
27
- gr.Info(f"🟑 Loading {mode} Brain...")
28
  tokenizer = AutoTokenizer.from_pretrained(model_id)
29
  model = AutoModelForCausalLM.from_pretrained(
30
  model_id,
@@ -33,204 +31,142 @@ def load_chat_model(mode):
33
  )
34
  loaded_chat_models[model_id] = model
35
  loaded_chat_tokenizers[model_id] = tokenizer
36
- chat_model_loaded[model_id] = True
37
- return "🟒 Model Loaded"
38
- return "🟒 Model Already Loaded"
39
 
40
  def chat_logic(user_input, mode):
41
  model_id = chat_models[mode]
42
- if model_id not in chat_model_loaded:
43
- return "❌ Model Not Loaded"
44
  model, tokenizer = loaded_chat_models[model_id], loaded_chat_tokenizers[model_id]
45
  messages = [{"role": "user", "content": user_input}]
46
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
47
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
48
  generated_ids = model.generate(**model_inputs, max_new_tokens=1024)
49
- generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
50
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
51
- cleaned_response = re.sub(r'<think>.*?</think>\s*\n?', '', response, flags=re.DOTALL)
52
- return cleaned_response.strip()
53
-
54
- def clear_chat_model(password):
55
- if password != "Creeper":
56
- return "❌ Incorrect password"
57
- for model_id in list(loaded_chat_models.keys()):
58
- del loaded_chat_models[model_id]
59
- del loaded_chat_tokenizers[model_id]
60
- chat_model_loaded.pop(model_id, None)
61
-
62
- gc.collect()
63
- torch.cuda.empty_cache()
64
- if torch.cuda.is_available():
65
- torch.cuda.ipc_collect()
66
- return "πŸ”΄ Model Not Loaded (RAM Flushed)"
67
-
68
- # --- Image ---
69
  image_model_id = "stabilityai/sdxl-turbo"
70
  image_pipe = None
71
- image_model_loaded = False
72
 
73
  def load_image_model():
74
- global image_pipe, image_model_loaded
75
  if image_pipe is None:
76
- gr.Info("🟑 Priming TNT (Image Gen)...")
77
  pipe = DiffusionPipeline.from_pretrained(
78
  image_model_id,
79
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
80
  )
81
  pipe.to(device)
82
  image_pipe = pipe
83
- image_model_loaded = True
84
- return "🟒 Model Loaded"
85
- return "🟒 Model Already Loaded"
86
 
87
  def image_logic(prompt, width, height, steps):
88
- if not image_model_loaded or image_pipe is None:
89
- yield "❌ Model Not Loaded", None
90
  return
91
- start_time = time.time()
92
- final_prompt = f"{prompt}, centered and realistic (if applicable)"
93
  yield "πŸ’₯ IGNITING...", None
94
  image = image_pipe(
95
- prompt=final_prompt,
96
- width=int(width),
97
- height=int(height),
98
- num_inference_steps=int(steps),
99
- guidance_scale=0.0,
100
- output_type="pil"
101
  ).images[0]
102
- duration = round(time.time() - start_time, 2)
103
- yield f"πŸ’₯ EXPLODED in {duration}s", image
104
-
105
- def clear_image_model(password):
106
- global image_pipe, image_model_loaded
107
- if password != "Creeper":
108
- return "❌ Incorrect password"
109
- if image_pipe:
110
- del image_pipe
111
- image_pipe = None
112
- image_model_loaded = False
113
- gc.collect()
114
- torch.cuda.empty_cache()
115
- if torch.cuda.is_available():
116
- torch.cuda.ipc_collect()
117
- return "πŸ”΄ Model Not Loaded (RAM Flushed)"
118
-
119
- # --- TTS ---
120
  tts_model_id = "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice"
121
  SUPPORTED_VOICES = ['aiden', 'dylan', 'eric', 'ono_anna', 'ryan', 'serena', 'sohee', 'uncle_fu', 'vivian']
122
  tts_model = None
123
- tts_model_loaded = False
124
 
125
  def load_tts_model():
126
- global tts_model, tts_model_loaded
127
  if tts_model is None:
128
- gr.Info("🟑 Tuning Note-Blocks (TTS)...")
129
  tts_model = Qwen3TTSModel.from_pretrained(
130
  tts_model_id,
131
  device_map=device,
132
  torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
133
  )
134
- tts_model_loaded = True
135
- return "🟒 Model Loaded"
136
- return "🟒 Model Already Loaded"
137
 
138
- def tts_logic(text, voice, instructions, auto_detect):
139
- if not tts_model_loaded or tts_model is None:
140
- return None, "❌ Model Not Loaded"
141
  try:
142
- lang_map = {'zh': 'Chinese', 'en': 'English', 'jp': 'Japanese', 'ko': 'Korean'}
143
- detected_lang = "English"
144
- if auto_detect:
145
- try:
146
- raw_lang = detect(text).split('-')[0]
147
- detected_lang = lang_map.get(raw_lang, "English")
148
- except: pass
149
  wavs, sr = tts_model.generate_custom_voice(
150
- language=detected_lang, speaker=voice, instruct=instructions, text=text
 
 
 
151
  )
152
  output_path = "creeper_voice.wav"
153
  sf.write(output_path, wavs[0], sr)
154
- return output_path, f"Speaker: {voice} | Lang: {detected_lang}"
155
  except Exception as e:
156
- return None, f"Error: {str(e)}"
157
-
158
- def clear_tts_model(password):
159
- global tts_model, tts_model_loaded
160
- if password != "Creeper":
161
- return "❌ Incorrect password"
162
- if tts_model:
163
- del tts_model
164
- tts_model = None
165
- tts_model_loaded = False
166
- gc.collect()
167
- torch.cuda.empty_cache()
168
- if torch.cuda.is_available():
169
- torch.cuda.ipc_collect()
170
- return "πŸ”΄ Model Not Loaded (RAM Flushed)"
171
-
172
- # --- UI ---
173
  creeper_css = """
174
  body { background-color: #000000; }
175
- .gradio-container { background-color: #1e1e1e; border: 10px solid #2e8b57 !important; font-family: 'Courier New', Courier, monospace; color: #00ff00; }
176
- footer { display: none !important; }
177
- .gr-button-primary { background-color: #4A7023 !important; border: 4px solid #000 !important; color: white !important; font-weight: bold; text-transform: uppercase; }
178
- label span { color: #2e8b57 !important; font-weight: bold; font-size: 1.2em; }
179
- textarea, input, select, .gr-dropdown { background-color: #2e2e2e !important; color: #00ff00 !important; border: 3px solid #4A7023 !important; }
180
  """
181
 
182
  with gr.Blocks(css=creeper_css, title="CREEPER AI HUB") as demo:
183
  gr.Markdown("# 🟩 CREEPER AI HUB 🟩")
 
184
  with gr.Tabs():
 
185
  with gr.TabItem("SSSSS-CHAT"):
186
- chat_status = gr.Label("πŸ”΄ Model Not Loaded", label="Status")
187
  with gr.Row():
188
- mode_radio = gr.Radio(["Normal", "Thinking"], value="Normal", label="Mode")
189
- load_chat_btn = gr.Button("Load Chat")
190
- chat_pw = gr.Textbox(label="Password", type="password")
191
- clear_chat_btn = gr.Button("Clear RAM")
192
- chat_input = gr.Textbox(label="Message")
193
- chat_output = gr.Textbox(label="Creeper Says")
194
  chat_btn = gr.Button("EXPLODE TEXT", variant="primary")
 
195
  load_chat_btn.click(load_chat_model, mode_radio, chat_status)
196
- chat_btn.click(chat_logic, [chat_input, mode_radio], chat_output)
197
- clear_chat_btn.click(clear_chat_model, chat_pw, chat_status)
198
 
 
199
  with gr.TabItem("TNT-IMAGE"):
200
- img_status = gr.Label("πŸ”΄ Model Not Loaded", label="Status")
201
  with gr.Row():
202
- with gr.Column():
203
- img_prompt = gr.Textbox(label="Prompt")
204
- w_s = gr.Slider(256, 1024, 512, step=64, label="Width")
205
- h_s = gr.Slider(256, 1024, 512, step=64, label="Height")
206
- s_s = gr.Slider(1, 10, 4, step=1, label="Steps")
207
- load_img_btn = gr.Button("Load Image")
208
- img_btn = gr.Button("EXPLODE IMAGE", variant="primary")
209
- img_pw = gr.Textbox(label="Password", type="password")
210
- clear_img_btn = gr.Button("Clear RAM")
211
- img_out = gr.Image(label="Loot")
 
212
  load_img_btn.click(load_image_model, None, img_status)
213
  img_btn.click(image_logic, [img_prompt, w_s, h_s, s_s], [img_status, img_out])
214
- clear_img_btn.click(clear_image_model, img_pw, img_status)
215
 
216
- with gr.TabItem("NOTE-BLOCK (TTS)"):
217
- tts_status = gr.Label("πŸ”΄ Model Not Loaded", label="Status")
 
 
218
  with gr.Row():
219
- with gr.Column():
220
- tts_in = gr.Textbox(label="Text")
221
- voice_sel = gr.Dropdown(SUPPORTED_VOICES, value="vivian", label="Voice")
222
- auto_l = gr.Checkbox(True, label="Auto-detect")
223
- style_in = gr.Textbox("Speak naturally", label="Style")
224
- load_tts_btn = gr.Button("Load TTS")
225
- tts_btn = gr.Button("EXPLODE AUDIO", variant="primary")
226
- tts_pw = gr.Textbox(label="Password", type="password")
227
- clear_tts_btn = gr.Button("Clear RAM")
228
- with gr.Column():
229
- aud_out = gr.Audio(label="Audio", type="filepath")
230
- meta_out = gr.Label(label="Metadata")
231
  load_tts_btn.click(load_tts_model, None, tts_status)
232
- tts_btn.click(tts_logic, [tts_in, voice_sel, style_in, auto_l], [aud_out, meta_out])
233
- clear_tts_btn.click(clear_tts_model, tts_pw, tts_status)
234
 
235
  if __name__ == "__main__":
236
  demo.launch()
 
4
  import torch
5
  import re
6
  import time
 
7
  import soundfile as sf
8
  from qwen_tts import Qwen3TTSModel
9
  from langdetect import detect
 
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
 
14
+ # --- Chat Logic ---
15
  chat_models = {
16
  "Normal": "Qwen/Qwen3-0.6B",
17
  "Thinking": "Qwen/Qwen2.5-1.5B-Instruct"
18
  }
19
  loaded_chat_models = {}
20
  loaded_chat_tokenizers = {}
 
21
 
22
  def load_chat_model(mode):
23
  model_id = chat_models[mode]
24
  if model_id not in loaded_chat_models:
25
+ gr.Info(f"Loading {mode} Brain...")
26
  tokenizer = AutoTokenizer.from_pretrained(model_id)
27
  model = AutoModelForCausalLM.from_pretrained(
28
  model_id,
 
31
  )
32
  loaded_chat_models[model_id] = model
33
  loaded_chat_tokenizers[model_id] = tokenizer
34
+ return "Status: 🟒 Loaded"
35
+ return "Status: 🟒 Loaded"
 
36
 
37
  def chat_logic(user_input, mode):
38
  model_id = chat_models[mode]
39
+ if model_id not in loaded_chat_models:
40
+ return "❌ Please click 'Load Model' first!"
41
  model, tokenizer = loaded_chat_models[model_id], loaded_chat_tokenizers[model_id]
42
  messages = [{"role": "user", "content": user_input}]
43
  text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
44
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
45
  generated_ids = model.generate(**model_inputs, max_new_tokens=1024)
46
+ response = tokenizer.batch_decode([generated_ids[0][len(model_inputs.input_ids[0]):]], skip_special_tokens=True)[0]
47
+ return re.sub(r'<think>.*?</think>\s*\n?', '', response, flags=re.DOTALL).strip()
48
+
49
+ # --- Image Logic ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  image_model_id = "stabilityai/sdxl-turbo"
51
  image_pipe = None
 
52
 
53
  def load_image_model():
54
+ global image_pipe
55
  if image_pipe is None:
56
+ gr.Info("Priming TNT Engine...")
57
  pipe = DiffusionPipeline.from_pretrained(
58
  image_model_id,
59
  torch_dtype=torch.float16 if device == "cuda" else torch.float32
60
  )
61
  pipe.to(device)
62
  image_pipe = pipe
63
+ return "Status: 🟒 Loaded"
64
+ return "Status: 🟒 Loaded"
 
65
 
66
  def image_logic(prompt, width, height, steps):
67
+ if image_pipe is None:
68
+ yield "❌ Load Model First", None
69
  return
 
 
70
  yield "πŸ’₯ IGNITING...", None
71
  image = image_pipe(
72
+ prompt=prompt,
73
+ width=int(width),
74
+ height=int(height),
75
+ num_inference_steps=int(steps),
76
+ guidance_scale=0.0
 
77
  ).images[0]
78
+ yield "πŸ’₯ EXPLODED", image
79
+
80
+ # --- TTS Logic ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  tts_model_id = "Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice"
82
  SUPPORTED_VOICES = ['aiden', 'dylan', 'eric', 'ono_anna', 'ryan', 'serena', 'sohee', 'uncle_fu', 'vivian']
83
  tts_model = None
 
84
 
85
  def load_tts_model():
86
+ global tts_model
87
  if tts_model is None:
88
+ gr.Info("Tuning Note-Blocks...")
89
  tts_model = Qwen3TTSModel.from_pretrained(
90
  tts_model_id,
91
  device_map=device,
92
  torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
93
  )
94
+ return "Status: 🟒 Loaded"
95
+ return "Status: 🟒 Loaded"
 
96
 
97
+ def tts_logic(text, voice, inst, auto):
98
+ if tts_model is None:
99
+ return None, "Status: ❌ Not Loaded"
100
  try:
 
 
 
 
 
 
 
101
  wavs, sr = tts_model.generate_custom_voice(
102
+ language="English",
103
+ speaker=voice,
104
+ instruct=inst,
105
+ text=text
106
  )
107
  output_path = "creeper_voice.wav"
108
  sf.write(output_path, wavs[0], sr)
109
+ return output_path, "Status: 🟒 Audio Generated"
110
  except Exception as e:
111
+ return None, f"Status: ❌ Error: {str(e)}"
112
+
113
+ # --- UI Styles ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  creeper_css = """
115
  body { background-color: #000000; }
116
+ .gradio-container { background-color: #1e1e1e; border: 8px solid #2e8b57; color: #00ff00; }
117
+ .small-status { font-size: 0.8em; color: #2e8b57; margin-top: -10px; }
118
+ .gr-button-primary { background-color: #4A7023 !important; border: 2px solid #000 !important; }
 
 
119
  """
120
 
121
  with gr.Blocks(css=creeper_css, title="CREEPER AI HUB") as demo:
122
  gr.Markdown("# 🟩 CREEPER AI HUB 🟩")
123
+
124
  with gr.Tabs():
125
+ # --- Chat Tab ---
126
  with gr.TabItem("SSSSS-CHAT"):
 
127
  with gr.Row():
128
+ chat_status = gr.Markdown("Status: πŸ”΄ Not Loaded", elem_classes=["small-status"])
129
+ with gr.Row():
130
+ mode_radio = gr.Radio(["Normal", "Thinking"], value="Normal", label="Brain Mode")
131
+ load_chat_btn = gr.Button("Load Model")
132
+ chat_in = gr.Textbox(label="Message", lines=3)
133
+ chat_out = gr.Textbox(label="Creeper Says")
134
  chat_btn = gr.Button("EXPLODE TEXT", variant="primary")
135
+
136
  load_chat_btn.click(load_chat_model, mode_radio, chat_status)
137
+ chat_btn.click(chat_logic, [chat_in, mode_radio], chat_out)
 
138
 
139
+ # --- Image Tab ---
140
  with gr.TabItem("TNT-IMAGE"):
 
141
  with gr.Row():
142
+ img_status = gr.Markdown("Status: πŸ”΄ Not Loaded", elem_classes=["small-status"])
143
+ with gr.Row():
144
+ img_prompt = gr.Textbox(label="Visual Idea", placeholder="Pixel art forest...")
145
+ load_img_btn = gr.Button("Load Model")
146
+ with gr.Row():
147
+ w_s = gr.Slider(256, 1024, 512, step=64, label="Width")
148
+ h_s = gr.Slider(256, 1024, 512, step=64, label="Height")
149
+ s_s = gr.Slider(1, 10, 4, step=1, label="Steps")
150
+ img_btn = gr.Button("EXPLODE IMAGE", variant="primary")
151
+ img_out = gr.Image(label="Output")
152
+
153
  load_img_btn.click(load_image_model, None, img_status)
154
  img_btn.click(image_logic, [img_prompt, w_s, h_s, s_s], [img_status, img_out])
 
155
 
156
+ # --- TTS Tab ---
157
+ with gr.TabItem("NOTE-BLOCK"):
158
+ with gr.Row():
159
+ tts_status = gr.Markdown("Status: πŸ”΄ Not Loaded", elem_classes=["small-status"])
160
  with gr.Row():
161
+ voice_sel = gr.Dropdown(SUPPORTED_VOICES, value="vivian", label="Speaker")
162
+ load_tts_btn = gr.Button("Load Model")
163
+ tts_in = gr.Textbox(label="Text to Speak")
164
+ style_in = gr.Textbox("Speak naturally", label="Instructions")
165
+ tts_btn = gr.Button("EXPLODE AUDIO", variant="primary")
166
+ aud_out = gr.Audio(label="Audio Output")
167
+
 
 
 
 
 
168
  load_tts_btn.click(load_tts_model, None, tts_status)
169
+ tts_btn.click(tts_logic, [tts_in, voice_sel, style_in, gr.State(True)], [aud_out, tts_status])
 
170
 
171
  if __name__ == "__main__":
172
  demo.launch()