Spaces:
Runtime error
Runtime error
Update functions.py
Browse files- functions.py +50 -50
functions.py
CHANGED
|
@@ -1,50 +1,50 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
from transformers import AutoProcessor, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
from parler_tts import ParlerTTSForConditionalGeneration
|
| 4 |
-
import torch
|
| 5 |
-
import soundfile as sf
|
| 6 |
-
|
| 7 |
-
def generate_image_caption(image_path):
|
| 8 |
-
|
| 9 |
-
model_name = "microsoft/Florence-2-large"
|
| 10 |
-
prompt = "<
|
| 11 |
-
|
| 12 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 13 |
-
processor = AutoProcessor.from_pretrained(model_name)
|
| 14 |
-
|
| 15 |
-
image = Image.open(image_path)
|
| 16 |
-
|
| 17 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 18 |
-
|
| 19 |
-
generated_ids = model.generate(
|
| 20 |
-
input_ids=inputs["input_ids"],
|
| 21 |
-
pixel_values=inputs["pixel_values"],
|
| 22 |
-
max_new_tokens=1024,
|
| 23 |
-
num_beams=3,
|
| 24 |
-
do_sample=False
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 28 |
-
caption = processor.post_process_generation(generated_text, task="<
|
| 29 |
-
|
| 30 |
-
return caption
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def convert_text_to_speech(text, device="cpu"):
|
| 36 |
-
|
| 37 |
-
model_name = "parler-tts/parler_tts_mini_v0.1"
|
| 38 |
-
|
| 39 |
-
model = ParlerTTSForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 40 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 41 |
-
|
| 42 |
-
description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
| 43 |
-
|
| 44 |
-
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
| 45 |
-
prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
|
| 46 |
-
|
| 47 |
-
generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
| 48 |
-
audio_arr = generation.cpu().numpy().squeeze()
|
| 49 |
-
|
| 50 |
-
return audio_arr
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 4 |
+
import torch
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
|
| 7 |
+
def generate_image_caption(image_path):
|
| 8 |
+
|
| 9 |
+
model_name = "microsoft/Florence-2-large"
|
| 10 |
+
prompt = "<DETAILED_CAPTION>"
|
| 11 |
+
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 13 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
image = Image.open(image_path)
|
| 16 |
+
|
| 17 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 18 |
+
|
| 19 |
+
generated_ids = model.generate(
|
| 20 |
+
input_ids=inputs["input_ids"],
|
| 21 |
+
pixel_values=inputs["pixel_values"],
|
| 22 |
+
max_new_tokens=1024,
|
| 23 |
+
num_beams=3,
|
| 24 |
+
do_sample=False
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 28 |
+
caption = processor.post_process_generation(generated_text, task="<DETAILED_CAPTION>", image_size=(image.width, image.height))
|
| 29 |
+
|
| 30 |
+
return caption
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def convert_text_to_speech(text, device="cpu"):
|
| 36 |
+
|
| 37 |
+
model_name = "parler-tts/parler_tts_mini_v0.1"
|
| 38 |
+
|
| 39 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 41 |
+
|
| 42 |
+
description = "A female speaker with a slightly low-pitched voice delivers her words quite expressively, in a very confined sounding environment with clear audio quality. She speaks very fast."
|
| 43 |
+
|
| 44 |
+
input_ids = tokenizer(description, return_tensors="pt").input_ids.to(device)
|
| 45 |
+
prompt_input_ids = tokenizer(text, return_tensors="pt").input_ids.to(device)
|
| 46 |
+
|
| 47 |
+
generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
| 48 |
+
audio_arr = generation.cpu().numpy().squeeze()
|
| 49 |
+
|
| 50 |
+
return audio_arr
|