Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,12 @@ import redis
|
|
| 4 |
import torch
|
| 5 |
import scipy
|
| 6 |
from transformers import (
|
| 7 |
-
pipeline, AutoTokenizer, AutoModelForCausalLM, AutoProcessor,
|
| 8 |
-
MusicgenForConditionalGeneration, WhisperProcessor, WhisperForConditionalGeneration,
|
| 9 |
MarianMTModel, MarianTokenizer, BartTokenizer, BartForConditionalGeneration
|
| 10 |
)
|
| 11 |
from diffusers import (
|
| 12 |
-
FluxPipeline, StableDiffusionPipeline, DPMSolverMultistepScheduler,
|
| 13 |
StableDiffusionImg2ImgPipeline, DiffusionPipeline
|
| 14 |
)
|
| 15 |
from diffusers.utils import export_to_video
|
|
@@ -22,16 +22,18 @@ import multiprocessing
|
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
redis_client = redis.Redis(
|
| 25 |
-
host=os.getenv('REDIS_HOST'),
|
| 26 |
-
port=os.getenv('REDIS_PORT'),
|
| 27 |
password=os.getenv("REDIS_PASSWORD")
|
| 28 |
)
|
| 29 |
|
| 30 |
huggingface_token = os.getenv('HF_TOKEN')
|
| 31 |
|
|
|
|
| 32 |
def generate_unique_id():
|
| 33 |
return str(uuid.uuid4())
|
| 34 |
|
|
|
|
| 35 |
def store_special_tokens(tokenizer, model_name):
|
| 36 |
special_tokens = {
|
| 37 |
'pad_token': tokenizer.pad_token,
|
|
@@ -45,6 +47,7 @@ def store_special_tokens(tokenizer, model_name):
|
|
| 45 |
}
|
| 46 |
redis_client.hmset(f"tokenizer_special_tokens:{model_name}", special_tokens)
|
| 47 |
|
|
|
|
| 48 |
def load_special_tokens(tokenizer, model_name):
|
| 49 |
special_tokens = redis_client.hgetall(f"tokenizer_special_tokens:{model_name}")
|
| 50 |
if special_tokens:
|
|
@@ -57,6 +60,7 @@ def load_special_tokens(tokenizer, model_name):
|
|
| 57 |
tokenizer.bos_token = special_tokens.get('bos_token', '').decode("utf-8")
|
| 58 |
tokenizer.bos_token_id = int(special_tokens.get('bos_token_id', -1))
|
| 59 |
|
|
|
|
| 60 |
def train_and_store_transformers_model(model_name, data):
|
| 61 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 62 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
@@ -69,6 +73,7 @@ def train_and_store_transformers_model(model_name, data):
|
|
| 69 |
tokenizer_data = tokenizer.save_pretrained("transformers_tokenizer")
|
| 70 |
redis_client.set(f"transformers_tokenizer:{model_name}", tokenizer_data)
|
| 71 |
|
|
|
|
| 72 |
def generate_transformers_response_from_redis(model_name, prompt):
|
| 73 |
unique_id = generate_unique_id()
|
| 74 |
model_data = redis_client.get(f"transformers_model:{model_name}:state_dict")
|
|
@@ -85,6 +90,7 @@ def generate_transformers_response_from_redis(model_name, prompt):
|
|
| 85 |
redis_client.set(f"transformers_response:{unique_id}", response)
|
| 86 |
return response
|
| 87 |
|
|
|
|
| 88 |
def train_and_store_diffusers_model(model_name, data):
|
| 89 |
pipe = FluxPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16)
|
| 90 |
pipe.enable_model_cpu_offload()
|
|
@@ -94,6 +100,7 @@ def train_and_store_diffusers_model(model_name, data):
|
|
| 94 |
model_data = f.read()
|
| 95 |
redis_client.set(f"diffusers_model:{model_name}", model_data)
|
| 96 |
|
|
|
|
| 97 |
def generate_diffusers_image_from_redis(model_name, prompt):
|
| 98 |
unique_id = generate_unique_id()
|
| 99 |
model_data = redis_client.get(f"diffusers_model:{model_name}")
|
|
@@ -101,12 +108,14 @@ def generate_diffusers_image_from_redis(model_name, prompt):
|
|
| 101 |
f.write(model_data)
|
| 102 |
pipe = FluxPipeline.from_pretrained("diffusers_model", torch_dtype=torch.bfloat16)
|
| 103 |
pipe.enable_model_cpu_offload()
|
| 104 |
-
image = pipe(prompt, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256,
|
|
|
|
| 105 |
image_path = f"images/diffusers_{unique_id}.png"
|
| 106 |
image.save(image_path)
|
| 107 |
redis_client.set(f"diffusers_image:{unique_id}", image_path)
|
| 108 |
return image
|
| 109 |
|
|
|
|
| 110 |
def train_and_store_musicgen_model(model_name, data):
|
| 111 |
processor = AutoProcessor.from_pretrained(model_name)
|
| 112 |
model = MusicgenForConditionalGeneration.from_pretrained(model_name)
|
|
@@ -118,6 +127,7 @@ def train_and_store_musicgen_model(model_name, data):
|
|
| 118 |
processor_data = processor.save_pretrained("musicgen_processor")
|
| 119 |
redis_client.set(f"musicgen_processor:{model_name}", processor_data)
|
| 120 |
|
|
|
|
| 121 |
def generate_musicgen_audio_from_redis(model_name, text_prompts):
|
| 122 |
unique_id = generate_unique_id()
|
| 123 |
model_data = redis_client.get(f"musicgen_model:{model_name}:state_dict")
|
|
@@ -134,6 +144,7 @@ def generate_musicgen_audio_from_redis(model_name, text_prompts):
|
|
| 134 |
redis_client.set(f"musicgen_audio:{unique_id}", audio_path)
|
| 135 |
return audio_path
|
| 136 |
|
|
|
|
| 137 |
def train_and_store_stable_diffusion_model(model_name, data):
|
| 138 |
pipe = StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 139 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
|
@@ -144,6 +155,7 @@ def train_and_store_stable_diffusion_model(model_name, data):
|
|
| 144 |
model_data = f.read()
|
| 145 |
redis_client.set(f"stable_diffusion_model:{model_name}", model_data)
|
| 146 |
|
|
|
|
| 147 |
def generate_stable_diffusion_image_from_redis(model_name, prompt):
|
| 148 |
unique_id = generate_unique_id()
|
| 149 |
model_data = redis_client.get(f"stable_diffusion_model:{model_name}")
|
|
@@ -158,6 +170,7 @@ def generate_stable_diffusion_image_from_redis(model_name, prompt):
|
|
| 158 |
redis_client.set(f"stable_diffusion_image:{unique_id}", image_path)
|
| 159 |
return image
|
| 160 |
|
|
|
|
| 161 |
def train_and_store_img2img_model(model_name, data):
|
| 162 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 163 |
pipe = pipe.to("cuda")
|
|
@@ -167,6 +180,7 @@ def train_and_store_img2img_model(model_name, data):
|
|
| 167 |
model_data = f.read()
|
| 168 |
redis_client.set(f"img2img_model:{model_name}", model_data)
|
| 169 |
|
|
|
|
| 170 |
def generate_img2img_from_redis(model_name, init_image, prompt, strength=0.75):
|
| 171 |
unique_id = generate_unique_id()
|
| 172 |
model_data = redis_client.get(f"img2img_model:{model_name}")
|
|
@@ -181,6 +195,7 @@ def generate_img2img_from_redis(model_name, init_image, prompt, strength=0.75):
|
|
| 181 |
redis_client.set(f"img2img_image:{unique_id}", image_path)
|
| 182 |
return image
|
| 183 |
|
|
|
|
| 184 |
def train_and_store_marianmt_model(model_name, data):
|
| 185 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 186 |
model = MarianMTModel.from_pretrained(model_name)
|
|
@@ -192,6 +207,7 @@ def train_and_store_marianmt_model(model_name, data):
|
|
| 192 |
tokenizer_data = tokenizer.save_pretrained("marianmt_tokenizer")
|
| 193 |
redis_client.set(f"marianmt_tokenizer:{model_name}", tokenizer_data)
|
| 194 |
|
|
|
|
| 195 |
def translate_text_from_redis(model_name, text, src_lang, tgt_lang):
|
| 196 |
unique_id = generate_unique_id()
|
| 197 |
model_data = redis_client.get(f"marianmt_model:{model_name}:state_dict")
|
|
@@ -207,6 +223,7 @@ def translate_text_from_redis(model_name, text, src_lang, tgt_lang):
|
|
| 207 |
redis_client.set(f"marianmt_translation:{unique_id}", translation)
|
| 208 |
return translation
|
| 209 |
|
|
|
|
| 210 |
def train_and_store_bart_model(model_name, data):
|
| 211 |
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 212 |
model = BartForConditionalGeneration.from_pretrained(model_name)
|
|
@@ -218,6 +235,7 @@ def train_and_store_bart_model(model_name, data):
|
|
| 218 |
tokenizer_data = tokenizer.save_pretrained("bart_tokenizer")
|
| 219 |
redis_client.set(f"bart_tokenizer:{model_name}", tokenizer_data)
|
| 220 |
|
|
|
|
| 221 |
def summarize_text_from_redis(model_name, text):
|
| 222 |
unique_id = generate_unique_id()
|
| 223 |
model_data = redis_client.get(f"bart_model:{model_name}:state_dict")
|
|
@@ -234,6 +252,7 @@ def summarize_text_from_redis(model_name, text):
|
|
| 234 |
redis_client.set(f"bart_summary:{unique_id}", summary)
|
| 235 |
return summary
|
| 236 |
|
|
|
|
| 237 |
def auto_train_and_store(model_name, task, data):
|
| 238 |
if task == "text-generation":
|
| 239 |
train_and_store_transformers_model(model_name, data)
|
|
@@ -250,6 +269,7 @@ def auto_train_and_store(model_name, task, data):
|
|
| 250 |
elif task == "summarization":
|
| 251 |
train_and_store_bart_model(model_name, data)
|
| 252 |
|
|
|
|
| 253 |
def transcribe_audio_from_redis(audio_file):
|
| 254 |
audio_file_path = "audio_file.wav"
|
| 255 |
with open(audio_file_path, "wb") as f:
|
|
@@ -263,6 +283,7 @@ def transcribe_audio_from_redis(audio_file):
|
|
| 263 |
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 264 |
return transcription[0]
|
| 265 |
|
|
|
|
| 266 |
def generate_image_from_redis(model_name, prompt, model_type):
|
| 267 |
if model_type == "diffusers":
|
| 268 |
image = generate_diffusers_image_from_redis(model_name, prompt)
|
|
@@ -272,8 +293,10 @@ def generate_image_from_redis(model_name, prompt, model_type):
|
|
| 272 |
image = generate_img2img_from_redis(model_name, "init_image.png", prompt)
|
| 273 |
return image
|
| 274 |
|
|
|
|
| 275 |
def generate_video_from_redis(prompt):
|
| 276 |
-
pipe = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b", torch_dtype=torch.float16,
|
|
|
|
| 277 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 278 |
pipe.enable_model_cpu_offload()
|
| 279 |
video_frames = pipe(prompt, num_inference_steps=25).frames
|
|
@@ -282,6 +305,7 @@ def generate_video_from_redis(prompt):
|
|
| 282 |
redis_client.set(f"video_{unique_id}", video_path)
|
| 283 |
return video_path
|
| 284 |
|
|
|
|
| 285 |
def generate_random_response(prompts, generator):
|
| 286 |
responses = []
|
| 287 |
for prompt in prompts:
|
|
@@ -289,16 +313,19 @@ def generate_random_response(prompts, generator):
|
|
| 289 |
responses.append(response)
|
| 290 |
return responses
|
| 291 |
|
|
|
|
| 292 |
def process_parallel(tasks):
|
| 293 |
with multiprocessing.Pool() as pool:
|
| 294 |
results = pool.map(lambda task: task(), tasks)
|
| 295 |
return results
|
| 296 |
|
|
|
|
| 297 |
def generate_response_from_prompt(prompt, model_name="google/flan-t5-xl"):
|
| 298 |
generator = pipeline('text-generation', model=model_name, tokenizer=model_name)
|
| 299 |
responses = generate_random_response([prompt], generator)
|
| 300 |
return responses[0]
|
| 301 |
|
|
|
|
| 302 |
def generate_image_from_prompt(prompt, image_type, model_name="CompVis/stable-diffusion-v1-4"):
|
| 303 |
if image_type == "diffusers":
|
| 304 |
image = generate_diffusers_image_from_redis(model_name, prompt)
|
|
@@ -308,65 +335,98 @@ def generate_image_from_prompt(prompt, image_type, model_name="CompVis/stable-di
|
|
| 308 |
image = generate_img2img_from_redis(model_name, "init_image.png", prompt)
|
| 309 |
return image
|
| 310 |
|
|
|
|
| 311 |
def gradio_app():
|
| 312 |
with gr.Blocks() as app:
|
| 313 |
-
gr.Markdown(
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
gr.
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
gr.
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
gr.
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
app.launch()
|
| 370 |
|
|
|
|
| 371 |
if __name__ == "__main__":
|
| 372 |
gradio_app()
|
|
|
|
| 4 |
import torch
|
| 5 |
import scipy
|
| 6 |
from transformers import (
|
| 7 |
+
pipeline, AutoTokenizer, AutoModelForCausalLM, AutoProcessor,
|
| 8 |
+
MusicgenForConditionalGeneration, WhisperProcessor, WhisperForConditionalGeneration,
|
| 9 |
MarianMTModel, MarianTokenizer, BartTokenizer, BartForConditionalGeneration
|
| 10 |
)
|
| 11 |
from diffusers import (
|
| 12 |
+
FluxPipeline, StableDiffusionPipeline, DPMSolverMultistepScheduler,
|
| 13 |
StableDiffusionImg2ImgPipeline, DiffusionPipeline
|
| 14 |
)
|
| 15 |
from diffusers.utils import export_to_video
|
|
|
|
| 22 |
load_dotenv()
|
| 23 |
|
| 24 |
redis_client = redis.Redis(
|
| 25 |
+
host=os.getenv('REDIS_HOST'),
|
| 26 |
+
port=os.getenv('REDIS_PORT'),
|
| 27 |
password=os.getenv("REDIS_PASSWORD")
|
| 28 |
)
|
| 29 |
|
| 30 |
huggingface_token = os.getenv('HF_TOKEN')
|
| 31 |
|
| 32 |
+
|
| 33 |
def generate_unique_id():
|
| 34 |
return str(uuid.uuid4())
|
| 35 |
|
| 36 |
+
|
| 37 |
def store_special_tokens(tokenizer, model_name):
|
| 38 |
special_tokens = {
|
| 39 |
'pad_token': tokenizer.pad_token,
|
|
|
|
| 47 |
}
|
| 48 |
redis_client.hmset(f"tokenizer_special_tokens:{model_name}", special_tokens)
|
| 49 |
|
| 50 |
+
|
| 51 |
def load_special_tokens(tokenizer, model_name):
|
| 52 |
special_tokens = redis_client.hgetall(f"tokenizer_special_tokens:{model_name}")
|
| 53 |
if special_tokens:
|
|
|
|
| 60 |
tokenizer.bos_token = special_tokens.get('bos_token', '').decode("utf-8")
|
| 61 |
tokenizer.bos_token_id = int(special_tokens.get('bos_token_id', -1))
|
| 62 |
|
| 63 |
+
|
| 64 |
def train_and_store_transformers_model(model_name, data):
|
| 65 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 66 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
| 73 |
tokenizer_data = tokenizer.save_pretrained("transformers_tokenizer")
|
| 74 |
redis_client.set(f"transformers_tokenizer:{model_name}", tokenizer_data)
|
| 75 |
|
| 76 |
+
|
| 77 |
def generate_transformers_response_from_redis(model_name, prompt):
|
| 78 |
unique_id = generate_unique_id()
|
| 79 |
model_data = redis_client.get(f"transformers_model:{model_name}:state_dict")
|
|
|
|
| 90 |
redis_client.set(f"transformers_response:{unique_id}", response)
|
| 91 |
return response
|
| 92 |
|
| 93 |
+
|
| 94 |
def train_and_store_diffusers_model(model_name, data):
|
| 95 |
pipe = FluxPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16)
|
| 96 |
pipe.enable_model_cpu_offload()
|
|
|
|
| 100 |
model_data = f.read()
|
| 101 |
redis_client.set(f"diffusers_model:{model_name}", model_data)
|
| 102 |
|
| 103 |
+
|
| 104 |
def generate_diffusers_image_from_redis(model_name, prompt):
|
| 105 |
unique_id = generate_unique_id()
|
| 106 |
model_data = redis_client.get(f"diffusers_model:{model_name}")
|
|
|
|
| 108 |
f.write(model_data)
|
| 109 |
pipe = FluxPipeline.from_pretrained("diffusers_model", torch_dtype=torch.bfloat16)
|
| 110 |
pipe.enable_model_cpu_offload()
|
| 111 |
+
image = pipe(prompt, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256,
|
| 112 |
+
generator=torch.Generator("cpu").manual_seed(0)).images[0]
|
| 113 |
image_path = f"images/diffusers_{unique_id}.png"
|
| 114 |
image.save(image_path)
|
| 115 |
redis_client.set(f"diffusers_image:{unique_id}", image_path)
|
| 116 |
return image
|
| 117 |
|
| 118 |
+
|
| 119 |
def train_and_store_musicgen_model(model_name, data):
|
| 120 |
processor = AutoProcessor.from_pretrained(model_name)
|
| 121 |
model = MusicgenForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
| 127 |
processor_data = processor.save_pretrained("musicgen_processor")
|
| 128 |
redis_client.set(f"musicgen_processor:{model_name}", processor_data)
|
| 129 |
|
| 130 |
+
|
| 131 |
def generate_musicgen_audio_from_redis(model_name, text_prompts):
|
| 132 |
unique_id = generate_unique_id()
|
| 133 |
model_data = redis_client.get(f"musicgen_model:{model_name}:state_dict")
|
|
|
|
| 144 |
redis_client.set(f"musicgen_audio:{unique_id}", audio_path)
|
| 145 |
return audio_path
|
| 146 |
|
| 147 |
+
|
| 148 |
def train_and_store_stable_diffusion_model(model_name, data):
|
| 149 |
pipe = StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 150 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
|
|
|
| 155 |
model_data = f.read()
|
| 156 |
redis_client.set(f"stable_diffusion_model:{model_name}", model_data)
|
| 157 |
|
| 158 |
+
|
| 159 |
def generate_stable_diffusion_image_from_redis(model_name, prompt):
|
| 160 |
unique_id = generate_unique_id()
|
| 161 |
model_data = redis_client.get(f"stable_diffusion_model:{model_name}")
|
|
|
|
| 170 |
redis_client.set(f"stable_diffusion_image:{unique_id}", image_path)
|
| 171 |
return image
|
| 172 |
|
| 173 |
+
|
| 174 |
def train_and_store_img2img_model(model_name, data):
|
| 175 |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 176 |
pipe = pipe.to("cuda")
|
|
|
|
| 180 |
model_data = f.read()
|
| 181 |
redis_client.set(f"img2img_model:{model_name}", model_data)
|
| 182 |
|
| 183 |
+
|
| 184 |
def generate_img2img_from_redis(model_name, init_image, prompt, strength=0.75):
|
| 185 |
unique_id = generate_unique_id()
|
| 186 |
model_data = redis_client.get(f"img2img_model:{model_name}")
|
|
|
|
| 195 |
redis_client.set(f"img2img_image:{unique_id}", image_path)
|
| 196 |
return image
|
| 197 |
|
| 198 |
+
|
| 199 |
def train_and_store_marianmt_model(model_name, data):
|
| 200 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 201 |
model = MarianMTModel.from_pretrained(model_name)
|
|
|
|
| 207 |
tokenizer_data = tokenizer.save_pretrained("marianmt_tokenizer")
|
| 208 |
redis_client.set(f"marianmt_tokenizer:{model_name}", tokenizer_data)
|
| 209 |
|
| 210 |
+
|
| 211 |
def translate_text_from_redis(model_name, text, src_lang, tgt_lang):
|
| 212 |
unique_id = generate_unique_id()
|
| 213 |
model_data = redis_client.get(f"marianmt_model:{model_name}:state_dict")
|
|
|
|
| 223 |
redis_client.set(f"marianmt_translation:{unique_id}", translation)
|
| 224 |
return translation
|
| 225 |
|
| 226 |
+
|
| 227 |
def train_and_store_bart_model(model_name, data):
|
| 228 |
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 229 |
model = BartForConditionalGeneration.from_pretrained(model_name)
|
|
|
|
| 235 |
tokenizer_data = tokenizer.save_pretrained("bart_tokenizer")
|
| 236 |
redis_client.set(f"bart_tokenizer:{model_name}", tokenizer_data)
|
| 237 |
|
| 238 |
+
|
| 239 |
def summarize_text_from_redis(model_name, text):
|
| 240 |
unique_id = generate_unique_id()
|
| 241 |
model_data = redis_client.get(f"bart_model:{model_name}:state_dict")
|
|
|
|
| 252 |
redis_client.set(f"bart_summary:{unique_id}", summary)
|
| 253 |
return summary
|
| 254 |
|
| 255 |
+
|
| 256 |
def auto_train_and_store(model_name, task, data):
|
| 257 |
if task == "text-generation":
|
| 258 |
train_and_store_transformers_model(model_name, data)
|
|
|
|
| 269 |
elif task == "summarization":
|
| 270 |
train_and_store_bart_model(model_name, data)
|
| 271 |
|
| 272 |
+
|
| 273 |
def transcribe_audio_from_redis(audio_file):
|
| 274 |
audio_file_path = "audio_file.wav"
|
| 275 |
with open(audio_file_path, "wb") as f:
|
|
|
|
| 283 |
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 284 |
return transcription[0]
|
| 285 |
|
| 286 |
+
|
| 287 |
def generate_image_from_redis(model_name, prompt, model_type):
|
| 288 |
if model_type == "diffusers":
|
| 289 |
image = generate_diffusers_image_from_redis(model_name, prompt)
|
|
|
|
| 293 |
image = generate_img2img_from_redis(model_name, "init_image.png", prompt)
|
| 294 |
return image
|
| 295 |
|
| 296 |
+
|
| 297 |
def generate_video_from_redis(prompt):
|
| 298 |
+
pipe = DiffusionPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b", torch_dtype=torch.float16,
|
| 299 |
+
variant="fp16")
|
| 300 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 301 |
pipe.enable_model_cpu_offload()
|
| 302 |
video_frames = pipe(prompt, num_inference_steps=25).frames
|
|
|
|
| 305 |
redis_client.set(f"video_{unique_id}", video_path)
|
| 306 |
return video_path
|
| 307 |
|
| 308 |
+
|
| 309 |
def generate_random_response(prompts, generator):
|
| 310 |
responses = []
|
| 311 |
for prompt in prompts:
|
|
|
|
| 313 |
responses.append(response)
|
| 314 |
return responses
|
| 315 |
|
| 316 |
+
|
| 317 |
def process_parallel(tasks):
|
| 318 |
with multiprocessing.Pool() as pool:
|
| 319 |
results = pool.map(lambda task: task(), tasks)
|
| 320 |
return results
|
| 321 |
|
| 322 |
+
|
| 323 |
def generate_response_from_prompt(prompt, model_name="google/flan-t5-xl"):
|
| 324 |
generator = pipeline('text-generation', model=model_name, tokenizer=model_name)
|
| 325 |
responses = generate_random_response([prompt], generator)
|
| 326 |
return responses[0]
|
| 327 |
|
| 328 |
+
|
| 329 |
def generate_image_from_prompt(prompt, image_type, model_name="CompVis/stable-diffusion-v1-4"):
|
| 330 |
if image_type == "diffusers":
|
| 331 |
image = generate_diffusers_image_from_redis(model_name, prompt)
|
|
|
|
| 335 |
image = generate_img2img_from_redis(model_name, "init_image.png", prompt)
|
| 336 |
return image
|
| 337 |
|
| 338 |
+
|
| 339 |
def gradio_app():
|
| 340 |
with gr.Blocks() as app:
|
| 341 |
+
gr.Markdown(
|
| 342 |
+
"""
|
| 343 |
+
# IA Generativa con Transformers y Diffusers
|
| 344 |
+
Explora diferentes modelos de IA para generar texto, im谩genes, audio, video y m谩s.
|
| 345 |
+
"""
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
with gr.Tab("Texto"):
|
| 349 |
+
with gr.Row():
|
| 350 |
+
with gr.Column():
|
| 351 |
+
prompt_text = gr.Textbox(label="Texto de Entrada", placeholder="Ingresa tu prompt de texto aqu铆...")
|
| 352 |
+
text_button = gr.Button("Generar Texto", variant="primary")
|
| 353 |
+
with gr.Column():
|
| 354 |
+
text_output = gr.Textbox(label="Respuesta")
|
| 355 |
+
text_button.click(generate_response_from_prompt, inputs=prompt_text, outputs=text_output)
|
| 356 |
+
|
| 357 |
+
with gr.Tab("Imagen"):
|
| 358 |
+
with gr.Row():
|
| 359 |
+
with gr.Column():
|
| 360 |
+
prompt_image = gr.Textbox(label="Prompt de Imagen",
|
| 361 |
+
placeholder="Ingresa tu prompt de imagen aqu铆...")
|
| 362 |
+
image_type = gr.Dropdown(["diffusers", "stable-diffusion", "img2img"], label="Tipo de Modelo",
|
| 363 |
+
value="stable-diffusion")
|
| 364 |
+
model_name_image = gr.Textbox(label="Nombre del Modelo",
|
| 365 |
+
value="CompVis/stable-diffusion-v1-4")
|
| 366 |
+
image_button = gr.Button("Generar Imagen", variant="primary")
|
| 367 |
+
with gr.Column():
|
| 368 |
+
image_output = gr.Image(label="Imagen Generada")
|
| 369 |
+
image_button.click(generate_image_from_prompt, inputs=[prompt_image, image_type, model_name_image],
|
| 370 |
+
outputs=image_output)
|
| 371 |
+
|
| 372 |
+
with gr.Tab("Video"):
|
| 373 |
+
with gr.Row():
|
| 374 |
+
with gr.Column():
|
| 375 |
+
prompt_video = gr.Textbox(label="Prompt de Video", placeholder="Ingresa tu prompt de video aqu铆...")
|
| 376 |
+
video_button = gr.Button("Generar Video", variant="primary")
|
| 377 |
+
with gr.Column():
|
| 378 |
+
video_output = gr.Video(label="Video Generado")
|
| 379 |
+
video_button.click(generate_video_from_redis, inputs=prompt_video, outputs=video_output)
|
| 380 |
+
|
| 381 |
+
with gr.Tab("Audio"):
|
| 382 |
+
with gr.Row():
|
| 383 |
+
with gr.Column():
|
| 384 |
+
model_name_audio = gr.Textbox(label="Nombre del Modelo", value="facebook/musicgen-small")
|
| 385 |
+
text_prompts_audio = gr.Textbox(label="Prompts de Audio",
|
| 386 |
+
placeholder="Ingresa tus prompts de audio aqu铆...")
|
| 387 |
+
audio_button = gr.Button("Generar Audio", variant="primary")
|
| 388 |
+
with gr.Column():
|
| 389 |
+
audio_output = gr.Audio(label="Audio Generado")
|
| 390 |
+
audio_button.click(generate_musicgen_audio_from_redis, inputs=[model_name_audio, text_prompts_audio],
|
| 391 |
+
outputs=audio_output)
|
| 392 |
+
|
| 393 |
+
with gr.Tab("Transcripci贸n"):
|
| 394 |
+
with gr.Row():
|
| 395 |
+
with gr.Column():
|
| 396 |
+
audio_file = gr.Audio(type="filepath", label="Archivo de Audio")
|
| 397 |
+
audio_button = gr.Button("Transcribir Audio", variant="primary")
|
| 398 |
+
with gr.Column():
|
| 399 |
+
transcription_output = gr.Textbox(label="Transcripci贸n")
|
| 400 |
+
audio_button.click(transcribe_audio_from_redis, inputs=audio_file, outputs=transcription_output)
|
| 401 |
+
|
| 402 |
+
with gr.Tab("Traducci贸n"):
|
| 403 |
+
with gr.Row():
|
| 404 |
+
with gr.Column():
|
| 405 |
+
model_name_translate = gr.Textbox(label="Nombre del Modelo", value="Helsinki-NLP/opus-mt-en-es")
|
| 406 |
+
text_input = gr.Textbox(label="Texto a Traducir", placeholder="Ingresa el texto a traducir...")
|
| 407 |
+
src_lang_input = gr.Textbox(label="Idioma de Origen", value="en")
|
| 408 |
+
tgt_lang_input = gr.Textbox(label="Idioma de Destino", value="es")
|
| 409 |
+
translate_button = gr.Button("Traducir Texto", variant="primary")
|
| 410 |
+
with gr.Column():
|
| 411 |
+
translation_output = gr.Textbox(label="Traducci贸n")
|
| 412 |
+
translate_button.click(translate_text_from_redis,
|
| 413 |
+
inputs=[model_name_translate, text_input, src_lang_input, tgt_lang_input],
|
| 414 |
+
outputs=translation_output)
|
| 415 |
+
|
| 416 |
+
with gr.Tab("Resumen"):
|
| 417 |
+
with gr.Row():
|
| 418 |
+
with gr.Column():
|
| 419 |
+
model_name_summarize = gr.Textbox(label="Nombre del Modelo", value="facebook/bart-large-cnn")
|
| 420 |
+
text_to_summarize = gr.Textbox(label="Texto para Resumir",
|
| 421 |
+
placeholder="Ingresa el texto a resumir...")
|
| 422 |
+
summarize_button = gr.Button("Generar Resumen", variant="primary")
|
| 423 |
+
with gr.Column():
|
| 424 |
+
summary_output = gr.Textbox(label="Resumen")
|
| 425 |
+
summarize_button.click(summarize_text_from_redis, inputs=[model_name_summarize, text_to_summarize],
|
| 426 |
+
outputs=summary_output)
|
| 427 |
|
| 428 |
app.launch()
|
| 429 |
|
| 430 |
+
|
| 431 |
if __name__ == "__main__":
|
| 432 |
gradio_app()
|