K00B404 commited on
Commit
f798ab4
·
verified ·
1 Parent(s): c2481c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +286 -84
app.py CHANGED
@@ -1,97 +1,299 @@
1
- """
2
- Copyright 2022 Balacoon
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- TTS interactive demo
5
- """
 
 
 
6
 
7
- import logging
8
- from typing import cast
9
 
10
- import gradio as gr
11
- from balacoon_tts import TTS
12
- from huggingface_hub import hf_hub_download, list_repo_files
 
 
 
 
 
 
 
13
 
14
- # global tts module, initialized from a model selected
15
- tts = None
16
 
17
- gr.api_name="textTOPepe"
18
 
19
- def main():
20
- logging.basicConfig(level=logging.INFO)
 
 
 
 
 
 
 
 
 
21
 
22
- with gr.Blocks() as demo:
23
- gr.Markdown(
24
- """
25
- <h1 align="center">Text-to-Speech</h1>
 
26
 
27
- 1. Write an utterance to generate,
28
- 2. Select the model to synthesize with
29
- 3. Select the speaker
30
- 4. Hit "Generate" and listen to the result!
31
 
32
- When you select a Model for the first time,
33
- it will take a little time to download it.
34
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  )
36
- gr.api_name="textTOPepe"
37
- with gr.Row(variant="panel"):
38
- text = gr.Textbox(label="Text", placeholder="Insert your article here...")
39
-
40
- with gr.Row():
41
- with gr.Column(variant="panel"):
42
- repo_files = list_repo_files(repo_id="balacoon/tts")
43
- model_files = [x for x in repo_files if x.endswith("_cpu.addon")]
44
- model_name = gr.Dropdown(
45
- label="Model",
46
- choices=model_files,
47
- )
48
- with gr.Column(variant="panel"):
49
- speaker = gr.Dropdown(label="Speaker", choices=[])
50
-
51
- def set_model(model_name_str: str):
52
- """
53
- gets value from `model_name`, loads model,
54
- re-initializes tts object, gets list of
55
- speakers that model supports and set them to `speaker`
56
- """
57
- model_path = hf_hub_download(
58
- repo_id="balacoon/tts", filename=model_name_str
59
- )
60
- global tts
61
- tts = TTS(model_path)
62
- speakers = tts.get_speakers()
63
- value = speakers[-1]
64
- return gr.Dropdown.update(
65
- choices=speakers, value=value, visible=True
66
- )
67
-
68
- model_name.change(set_model, inputs=model_name, outputs=speaker)
69
-
70
- with gr.Row(variant="panel"):
71
- generate = gr.Button("Generate")
72
- with gr.Row(variant="panel"):
73
- audio = gr.Audio()
74
-
75
- def synthesize_audio(text_str: str, speaker_str: str = ""):
76
- """
77
- gets utterance to synthesize from `text` Textbox
78
- and speaker name from `speaker` dropdown list.
79
- speaker name might be empty for single-speaker models.
80
- Synthesizes the waveform and updates `audio` with it.
81
- """
82
- if not text_str:
83
- logging.info("text or speaker are not provided")
84
- return None
85
- global tts
86
- if len(text_str) > 10024:
87
- text_str = text_str[:10024]
88
- samples = cast(TTS, tts).synthesize(text_str, speaker_str)
89
- return gr.Audio.update(value=(cast(TTS, tts).get_sampling_rate(), samples))
90
-
91
- generate.click(synthesize_audio, inputs=[text, speaker], outputs=audio)
92
- demo.api_name="textTOPepe"
93
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  if __name__ == "__main__":
97
- main()
 
 
1
+ import gradio as gr
2
+ from gradio_client import Client
3
+ from datasets import load_dataset
4
+ from huggingface_hub import InferenceClient
5
+ import random
6
+ from PIL import Image
7
+ import os
8
+ from random import randint
9
+ from datetime import datetime
10
+ import pathlib
11
+ import numpy as np
12
+ from prompts import __all__ as system_prompts
13
+ from all_models import __all__ as all_models
14
+ # Initialize API clients and other setups
15
+ token = os.getenv('HF_TOKEN')
16
+ sdxl_client = Client("K00B404/Stable-Diffusion-XL-Serverless", hf_token=token)
17
+ seed = randint(0, 12345678)
18
+ hf_client = InferenceClient(
19
+ #"georgesung/llama2_7b_chat_uncensored",
20
+ "Xennon-BD/Qwen-uncensored-v2",
21
+ #"meta-llama/Llama-3.2-1B-Instruct",
22
+ token=token
23
+ )
24
+ hf_img_client = InferenceClient(all_models[0], token=token)
25
 
26
+ qwen_client = Client("K00B404/chatQwenne", hf_token=token)
27
+ personas_dataset = load_dataset("MohamedRashad/FinePersonas-Lite", split="train")
28
+ personas_list = personas_dataset["persona"][:100]
29
+ OUTPUT_DIR = "generated_characters"
30
+ pathlib.Path(OUTPUT_DIR).mkdir(exist_ok=True)
31
 
32
+ def get_timestamp_filename():
33
+ return datetime.now().strftime("%Y%m%d_%H%M%S")
34
 
35
+ def save_outputs(image, description, base_filename):
36
+ try:
37
+ image_path = os.path.join(OUTPUT_DIR, f"{base_filename}.png")
38
+ text_path = os.path.join(OUTPUT_DIR, f"{base_filename}.txt")
39
+ image.save(image_path, format='PNG')
40
+ with open(text_path, 'w', encoding='utf-8') as f:
41
+ f.write(description)
42
+ return image_path, text_path
43
+ except Exception:
44
+ return None, None
45
 
 
 
46
 
 
47
 
48
+ # Define the respond function using the HuggingFace InferenceClient
49
+ def respond(
50
+ message,
51
+ history: list[tuple[str, str]],
52
+ system_message,
53
+ max_tokens,
54
+ temperature,
55
+ top_p,
56
+ stream=False,
57
+ ):
58
+ messages = [{"role": "system", "content": system_message}]
59
 
60
+ for user_msg, assistant_msg in history:
61
+ if user_msg:
62
+ messages.append({"role": "user", "content": user_msg})
63
+ if assistant_msg:
64
+ messages.append({"role": "assistant", "content": assistant_msg})
65
 
66
+ messages.append({"role": "user", "content": message+"|"})
 
 
 
67
 
68
+ response = ""
69
+ if stream:
70
+ for message in hf_client.chat_completion(
71
+ messages,
72
+ max_tokens=max_tokens,
73
+ stream=stream,
74
+ temperature=temperature,
75
+ top_p=top_p,
76
+ ):
77
+ token = message.choices[0].delta.content
78
+ response += token
79
+ yield response
80
+ else:
81
+ response_data = hf_client.chat_completion(
82
+ messages,
83
+ max_tokens=max_tokens,
84
+ stream=stream,
85
+ temperature=temperature,
86
+ top_p=top_p,
87
  )
88
+ yield response_data.choices[0].message.content
89
+
90
+
91
+ # Character Creator Functions
92
+ def generate_character_description(character_prompt, max_length):
93
+ pre_force="a full body image , from head to toe image of "
94
+ pro_force=" 8k photorealistic "
95
+
96
+ try:
97
+ response_generator = respond(
98
+ message=pre_force+character_prompt+pro_force,
99
+ history=[],
100
+ system_message=system_prompts[0],
101
+ max_tokens=max_length // 4,
102
+ temperature=0.9,
103
+ top_p=0.95,
104
+ stream=False,
105
+ )
106
+ result = "".join(response_generator)
107
+ return result[:max_length]
108
+ except Exception as e:
109
+ return f"Error: {str(e)}"
110
+
111
+
112
+ '''try:
113
+ result = qwen_client.predict(
114
+ message=character_prompt,
115
+ system_message=system_prompts[0],
116
+ max_tokens=max_length // 4,
117
+ temperature=0.9,
118
+ top_p=0.95,
119
+ api_name="/chat"
120
+ )
121
+ return result[:max_length]
122
+ except Exception as e:
123
+ return f"Error: {str(e)}"'''
124
+
125
+
126
+ def generate_character(
127
+ #model="stabilityai/stable-diffusion-2-1",
128
+ character_concept,
129
+ generate_description,
130
+ negative_prompt,
131
+ num_inference_steps,
132
+ guidance_scale,
133
+ max_description_length,
134
+ lora_repo_id
135
+ ):
136
+
137
+ '''
138
+ prompt (str) — The prompt to generate an image from.
139
+ negative_prompt (List[str, optional) — One or several prompt to guide what NOT to include in image generation.
140
+ height (float, optional) — The height in pixels of the image to generate.
141
+ width (float, optional) — The width in pixels of the image to generate.
142
+ num_inference_steps (int, optional) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
143
+ guidance_scale (float, optional) — A higher guidance scale value encourages the model to generate images closely linked to the text prompt, but values too high may cause saturation and other artifacts.
144
+ model (str, optional) — The model to use for inference. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed Inference Endpoint. If not provided, the default recommended text-to-image model will be used. Defaults to None.
145
+ scheduler (str, optional) — Override the scheduler with a compatible one.
146
+ target_size (TextToImageTargetSize, optional) — The size in pixel of the output image
147
+ seed (int, optional) — Seed for the random number generator.
148
+
149
+ Returns
150
+ Image
151
+ '''
152
+ description = generate_character_description(character_concept, max_description_length) if generate_description else character_concept
153
+ prompt = f"{character_concept}\n{description}"
154
+ if lora_repo_id:
155
+ prompt += f" <lora:{lora_repo_id}:1.0>"
156
+ try:
157
+ #seed = randint(0, 12345678)
158
+ # output is a PIL.Image object
159
+ #image = hf_img_client.text_to_image(prompt=prompt, negative_prompt=negative_prompt,num_inference_steps=num_inference_steps,guidance_scale=guidance_scale,seed=seed)
160
+ # Save the image to a file
161
+ ''' base_filename = get_timestamp_filename()
162
+ image_path = os.path.join(OUTPUT_DIR, f"{base_filename}.png")
163
+ image.save(image_path, format='PNG')
164
+
165
+ # Save the description
166
+ text_path = os.path.join(OUTPUT_DIR, f"{base_filename}.txt")
167
+ with open(text_path, 'w', encoding='utf-8') as f:
168
+ f.write(description)
169
+
170
+ save_message = f"Files saved as:\nImage: {image_path}\nDescription: {text_path}"
171
+ return image, description, save_message
172
+ except Exception as e:
173
+ return None, f"Error: {str(e)}", ""
174
+ '''
175
+
176
+ image = sdxl_client.predict(
177
+ prompt=prompt, is_negative=negative_prompt, steps=num_inference_steps, cfg_scale=guidance_scale, sampler="DIMM", seed=-1, strength=0.9, api_name="/query"
178
+ )
179
+ base_filename = get_timestamp_filename()
180
+ image_path, text_path = save_outputs(image, description, base_filename)
181
+ save_message = f"Files saved as:\nImage: {image_path}\nDescription: {text_path}" if image_path and text_path else "Files not saved locally"
182
+ return image, description, save_message
183
+ except Exception as e:
184
+ return None, f"Error: {str(e)}", ""
185
+
186
+
187
+
188
 
189
+ # Boxed Inpainting Functions
190
+ def query_gligen(image, prompt, box_x1, box_y1, box_x2, box_y2, object_prompt):
191
+ # Simulate the GLIGEN API call
192
+ return image # Placeholder for actual implementation
193
+
194
+ def process_drawn_boxes(image_data, boxes, object_prompt, main_prompt):
195
+ try:
196
+ image = image_data["composite"] # Use the final edited image
197
+ if not boxes or len(boxes) == 0:
198
+ return image, "No boxes drawn. Please draw a box."
199
+ box = boxes[-1]
200
+ height, width = image.size
201
+ normalized_box = [box[0] / width, box[1] / height, box[2] / width, box[3] / height]
202
+ result_image = query_gligen(
203
+ image=image,
204
+ prompt=main_prompt,
205
+ box_x1=normalized_box[0],
206
+ box_y1=normalized_box[1],
207
+ box_x2=normalized_box[2],
208
+ box_y2=normalized_box[3],
209
+ object_prompt=object_prompt,
210
+ )
211
+ base_filename = get_timestamp_filename()
212
+ image_path, _ = save_outputs(result_image, f"GLIGEN: {main_prompt} - Object: {object_prompt}", base_filename)
213
+ save_message = f"Image saved as: {image_path}" if image_path else "File not saved locally"
214
+ return result_image, save_message
215
+ except Exception as e:
216
+ return image_data["composite"], f"Error: {str(e)}"
217
+
218
+ def capture_box_drawing(image, sketch_data, object_prompt, main_prompt):
219
+ """
220
+ Handles box drawing based on the freehand sketch.
221
+ """
222
+ try:
223
+ # Check if the user has drawn anything
224
+ if sketch_data is None:
225
+ return image, "No box drawn. Please draw a rectangular box."
226
+
227
+ # Convert the sketch into bounding box coordinates
228
+ # Extract non-transparent pixels from the sketch
229
+ sketch_np = np.array(sketch_data)
230
+ rows, cols = np.where(sketch_np[..., -1] > 0) # Alpha > 0 indicates a drawing
231
+
232
+ if rows.size == 0 or cols.size == 0:
233
+ return image, "No box detected. Ensure you draw a closed box."
234
+
235
+ # Calculate bounding box
236
+ x1, y1, x2, y2 = cols.min(), rows.min(), cols.max(), rows.max()
237
+
238
+ # Process the image with the drawn bounding box
239
+ result_image = query_gligen(
240
+ image=image,
241
+ prompt=main_prompt,
242
+ box_x1=x1 / image.width,
243
+ box_y1=y1 / image.height,
244
+ box_x2=x2 / image.width,
245
+ box_y2=y2 / image.height,
246
+ object_prompt=object_prompt,
247
+ )
248
+
249
+ # Save and return
250
+ base_filename = get_timestamp_filename()
251
+ image_path, _ = save_outputs(result_image, f"Boxed Inpainting: {main_prompt}", base_filename)
252
+ save_message = f"Image saved as: {image_path}" if image_path else "File not saved locally"
253
+ return result_image, save_message
254
+ except Exception as e:
255
+ return image, f"Error: {str(e)}"
256
+
257
+ # Interface
258
+ with gr.Blocks() as demo:
259
+ with gr.Tabs():
260
+ with gr.Tab("Character Creator"):
261
+ gr.Markdown("### Generate Characters")
262
+ with gr.Row():
263
+ char_concept = gr.Textbox(label="Character Concept")
264
+ generate_desc = gr.Checkbox(label="Generate Description", value=True)
265
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="deformed, distorted, disfigured, poorly drawn, bad anatomy, wrong anatomy,extra limb, missing limb, floating limbs, mutated hands and fingers, disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation,clothing, cloths ,pants, under pants, top, covered skin, underwear,gear, cloth, belt, cartoon, missing vagina,small vagina, small breasts, small nipples")
266
+ with gr.Row():
267
+ steps = gr.Slider(1, 50, value=20, step=1,label="Steps")
268
+ guidance = gr.Slider(1, 20, value=7, step=1,label="Guidance")
269
+ max_desc_len = gr.Number(value=1024, label="Max Description Length")
270
+ lora_repo = gr.Textbox(label="Lora Repo ID")
271
+ char_image = gr.Image(label="Generated Character")
272
+ char_desc = gr.Textbox(label="Generated Description")
273
+ save_info = gr.Textbox(label="Save Info")
274
+ gen_btn = gr.Button("Generate")
275
+
276
+ gen_btn.click(
277
+ generate_character,
278
+ inputs=[char_concept, generate_desc, negative_prompt, steps, guidance, max_desc_len, lora_repo],
279
+ outputs=[char_image, char_desc, save_info],
280
+ )
281
+
282
+ with gr.Tab("Boxed Inpainting"):
283
+ gr.Markdown("### Boxed Inpainting Tool")
284
+ image_input = gr.ImageEditor(label="Edit and Draw", type="numpy", crop_size=None)
285
+ boxes_input = gr.HighlightedText(label="Draw Boxes")
286
+ object_prompt = gr.Textbox(label="Object Prompt")
287
+ main_prompt = gr.Textbox(label="Main Prompt")
288
+ inp_image = gr.Image(label="Processed Image")
289
+ inp_save_info = gr.Textbox(label="Save Info")
290
+ inp_btn = gr.Button("Process")
291
+ inp_btn.click(
292
+ process_drawn_boxes,
293
+ inputs=[image_input, boxes_input, object_prompt, main_prompt],
294
+ outputs=[inp_image, inp_save_info],
295
+ )
296
 
297
  if __name__ == "__main__":
298
+ demo.launch()
299
+