Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
placeholder="Speaker ID. Valid only for mult-speaker model")
|
84 |
-
|
85 |
-
input_speed = gr.Slider(
|
86 |
-
minimum=0.1,
|
87 |
-
maximum=10,
|
88 |
-
value=1,
|
89 |
-
step=0.1,
|
90 |
-
label="Speed (larger->faster; smaller->slower)")
|
91 |
-
text_to_speech(language_choices[0],language_to_models[language_choices[0]][0],text_output,input_sid,input_speed)
|
92 |
-
output_audio[idx] = gr.Audio(label="Output")
|
93 |
-
output_info[idx] = gr.HTML(label="Info")
|
94 |
-
idx=idx+1
|
95 |
-
demo=gr.Interface(fn=text_to_speech,
|
96 |
-
title="Image to Text Interpretation",
|
97 |
-
inputs=inputsImg,
|
98 |
-
outputs=[output_txt,output_audio,input_sid,input_speed],
|
99 |
-
description="image to audio demo",
|
100 |
-
article = ""
|
101 |
-
)
|
102 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration, AutoModelForCausalLM, AutoImageProcessor, VisionEncoderDecoderModel, AutoTokenizer
|
3 |
+
import io
|
4 |
+
import base64
|
5 |
+
|
6 |
+
# from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
|
7 |
import torch
|
8 |
+
import open_clip
|
9 |
+
import openai
|
10 |
+
|
11 |
+
from huggingface_hub import hf_hub_download
|
12 |
+
|
13 |
+
# Carga el modelo de clasificación de imagen a texto
|
14 |
+
blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
15 |
+
blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
16 |
+
|
17 |
+
# Carga el modelo de texto a voz
|
18 |
+
openai.api_key = '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'
|
19 |
+
model_id = "base"
|
20 |
+
#model_version = "2022-01-01"
|
21 |
+
whisper = openai.Model(model_id=model_id)
|
22 |
+
|
23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
blip_model_large.to(device)
|
25 |
+
|
26 |
+
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
27 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
28 |
+
|
29 |
+
if use_float_16:
|
30 |
+
inputs = inputs.to(torch.float16)
|
31 |
+
|
32 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
|
33 |
+
|
34 |
+
if tokenizer is not None:
|
35 |
+
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
36 |
+
else:
|
37 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
38 |
+
|
39 |
+
return generated_caption
|
40 |
+
|
41 |
+
|
42 |
+
def generate_caption_coca(model, transform, image):
|
43 |
+
im = transform(image).unsqueeze(0).to(device)
|
44 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
45 |
+
generated = model.generate(im, seq_len=20)
|
46 |
+
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
47 |
+
|
48 |
+
|
49 |
+
def generate_captions(image):
|
50 |
+
|
51 |
+
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
|
52 |
+
print(caption_blip_large)
|
53 |
+
return caption_blip_large
|
54 |
+
|
55 |
+
|
56 |
+
# Define la función que convierte texto en voz
|
57 |
+
def text_to_speech(text):
|
58 |
+
# Genera el audio utilizando el modelo Whisper
|
59 |
+
response = whisper.generate(prompt=text)
|
60 |
+
print(response)
|
61 |
+
# Extrae el audio del resultado
|
62 |
+
audio = response.choices[0].audio
|
63 |
+
|
64 |
+
# Codifica el audio en base64
|
65 |
+
audio_base64 = base64.b64encode(audio).decode("utf-8")
|
66 |
+
|
67 |
+
# Devuelve el audio como un archivo MP3
|
68 |
+
return BytesIO(base64.b64decode(audio_base64))
|
69 |
+
|
70 |
+
# Define la interfaz de usuario utilizando Gradio
|
71 |
+
inputsImg = [
|
72 |
+
gr.Image(type="pil", label="Imagen"),
|
73 |
+
]
|
74 |
+
|
75 |
+
outputs = [ gr.Textbox(label="Caption generated by BLIP-large") ]
|
76 |
+
title = "Clasificación de imagen a texto y conversión de texto a voz"
|
77 |
+
description = "Carga una imagen y obtén una descripción de texto de lo que contiene la imagen, así como un archivo de audio que lee el texto en voz alta."
|
78 |
+
examples = []
|
79 |
+
|
80 |
+
interface = gr.Interface(fn=generate_captions,
|
81 |
+
inputs=inputsImg,
|
82 |
+
outputs=outputs,
|
83 |
+
examples=examples,
|
84 |
+
title=title,
|
85 |
+
description=description)
|
86 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|