Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,13 @@ import torch
|
|
3 |
from transformers import (
|
4 |
BlipProcessor,
|
5 |
BlipForQuestionAnswering,
|
6 |
-
pipeline
|
7 |
-
AutoTokenizer,
|
8 |
-
AutoModelForCausalLM
|
9 |
)
|
|
|
10 |
from PIL import Image
|
11 |
import os
|
12 |
import logging
|
|
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger = logging.getLogger(__name__)
|
@@ -17,24 +17,33 @@ logger = logging.getLogger(__name__)
|
|
17 |
class MultimodalProcessor:
|
18 |
def __init__(self):
|
19 |
self.load_models()
|
|
|
20 |
|
21 |
def load_models(self):
|
22 |
-
"""Charge les modèles
|
23 |
try:
|
24 |
logger.info("Chargement des modèles...")
|
|
|
25 |
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
26 |
self.blip_model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
|
|
|
|
27 |
self.audio_transcriber = pipeline("automatic-speech-recognition",
|
28 |
-
model="openai/whisper-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
logger.info("Modèles chargés avec succès")
|
32 |
except Exception as e:
|
33 |
logger.error(f"Erreur lors du chargement des modèles: {str(e)}")
|
34 |
raise
|
35 |
|
36 |
def analyze_image(self, image):
|
37 |
-
"""Analyse une image
|
38 |
try:
|
39 |
if image is None:
|
40 |
return ""
|
@@ -42,8 +51,7 @@ class MultimodalProcessor:
|
|
42 |
questions = [
|
43 |
"What is in the picture?",
|
44 |
"What are the main colors?",
|
45 |
-
"What is the setting or background?"
|
46 |
-
"What is happening in the image?",
|
47 |
]
|
48 |
|
49 |
responses = {}
|
@@ -56,8 +64,7 @@ class MultimodalProcessor:
|
|
56 |
description = (
|
57 |
f"This image shows {responses['What is in the picture?']}. "
|
58 |
f"The main colors are {responses['What are the main colors?']}. "
|
59 |
-
f"The setting is {responses['What is the setting or background?']}.
|
60 |
-
f"In the scene, {responses['What is happening in the image?']}"
|
61 |
)
|
62 |
|
63 |
return description
|
@@ -66,7 +73,7 @@ class MultimodalProcessor:
|
|
66 |
return "Erreur lors de l'analyse de l'image."
|
67 |
|
68 |
def transcribe_audio(self, audio_path):
|
69 |
-
"""Transcrit un fichier audio"""
|
70 |
try:
|
71 |
if audio_path is None:
|
72 |
return ""
|
@@ -75,48 +82,61 @@ class MultimodalProcessor:
|
|
75 |
logger.error(f"Erreur lors de la transcription audio: {str(e)}")
|
76 |
return "Erreur lors de la transcription audio."
|
77 |
|
78 |
-
def
|
79 |
-
"""Génère
|
80 |
try:
|
81 |
if not prompt:
|
82 |
-
return
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
except Exception as e:
|
88 |
-
logger.error(f"Erreur lors de la génération de
|
89 |
-
return
|
90 |
|
91 |
def process_inputs(self, image, audio, text):
|
92 |
"""Traite les entrées multimodales"""
|
93 |
try:
|
94 |
-
|
95 |
-
image_description = self.analyze_image(image) if image is not None else ""
|
96 |
|
97 |
-
#
|
98 |
-
|
|
|
|
|
|
|
99 |
|
100 |
-
#
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
if text:
|
107 |
-
|
108 |
|
109 |
-
#
|
110 |
-
if
|
111 |
-
final_prompt =
|
112 |
else:
|
113 |
-
final_prompt = "
|
|
|
|
|
|
|
114 |
|
115 |
-
return final_prompt
|
116 |
|
117 |
except Exception as e:
|
118 |
logger.error(f"Erreur lors du traitement des entrées: {str(e)}")
|
119 |
-
return "Une erreur est survenue lors du traitement des entrées."
|
120 |
|
121 |
def create_interface():
|
122 |
"""Crée l'interface Gradio"""
|
@@ -130,16 +150,17 @@ def create_interface():
|
|
130 |
gr.Textbox(label="Entrez du texte additionnel")
|
131 |
],
|
132 |
outputs=[
|
133 |
-
gr.
|
|
|
134 |
],
|
135 |
-
title="
|
136 |
description="""
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
"""
|
144 |
)
|
145 |
|
|
|
3 |
from transformers import (
|
4 |
BlipProcessor,
|
5 |
BlipForQuestionAnswering,
|
6 |
+
pipeline
|
|
|
|
|
7 |
)
|
8 |
+
from modelscope.pipelines import pipeline as ms_pipeline
|
9 |
from PIL import Image
|
10 |
import os
|
11 |
import logging
|
12 |
+
import tempfile
|
13 |
|
14 |
logging.basicConfig(level=logging.INFO)
|
15 |
logger = logging.getLogger(__name__)
|
|
|
17 |
class MultimodalProcessor:
|
18 |
def __init__(self):
|
19 |
self.load_models()
|
20 |
+
self.temp_dir = tempfile.mkdtemp()
|
21 |
|
22 |
def load_models(self):
|
23 |
+
"""Charge les modèles"""
|
24 |
try:
|
25 |
logger.info("Chargement des modèles...")
|
26 |
+
# BLIP pour l'analyse d'image
|
27 |
self.blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
28 |
self.blip_model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
29 |
+
|
30 |
+
# Whisper pour la transcription audio
|
31 |
self.audio_transcriber = pipeline("automatic-speech-recognition",
|
32 |
+
model="openai/whisper-base")
|
33 |
+
|
34 |
+
# ModelScope pour la génération vidéo
|
35 |
+
self.video_pipeline = ms_pipeline(
|
36 |
+
'text-to-video-synthesis',
|
37 |
+
model='damo/text-to-video-synthesis'
|
38 |
+
)
|
39 |
+
|
40 |
logger.info("Modèles chargés avec succès")
|
41 |
except Exception as e:
|
42 |
logger.error(f"Erreur lors du chargement des modèles: {str(e)}")
|
43 |
raise
|
44 |
|
45 |
def analyze_image(self, image):
|
46 |
+
"""Analyse une image avec BLIP"""
|
47 |
try:
|
48 |
if image is None:
|
49 |
return ""
|
|
|
51 |
questions = [
|
52 |
"What is in the picture?",
|
53 |
"What are the main colors?",
|
54 |
+
"What is the setting or background?"
|
|
|
55 |
]
|
56 |
|
57 |
responses = {}
|
|
|
64 |
description = (
|
65 |
f"This image shows {responses['What is in the picture?']}. "
|
66 |
f"The main colors are {responses['What are the main colors?']}. "
|
67 |
+
f"The setting is {responses['What is the setting or background?']}."
|
|
|
68 |
)
|
69 |
|
70 |
return description
|
|
|
73 |
return "Erreur lors de l'analyse de l'image."
|
74 |
|
75 |
def transcribe_audio(self, audio_path):
|
76 |
+
"""Transcrit un fichier audio avec Whisper"""
|
77 |
try:
|
78 |
if audio_path is None:
|
79 |
return ""
|
|
|
82 |
logger.error(f"Erreur lors de la transcription audio: {str(e)}")
|
83 |
return "Erreur lors de la transcription audio."
|
84 |
|
85 |
+
def generate_video(self, prompt):
|
86 |
+
"""Génère une vidéo avec ModelScope"""
|
87 |
try:
|
88 |
if not prompt:
|
89 |
+
return None
|
90 |
+
|
91 |
+
output_path = os.path.join(self.temp_dir, "output.mp4")
|
92 |
+
result = self.video_pipeline({
|
93 |
+
'text': prompt,
|
94 |
+
'output_path': output_path
|
95 |
+
})
|
96 |
+
|
97 |
+
if os.path.exists(output_path):
|
98 |
+
return output_path
|
99 |
+
raise Exception("La vidéo n'a pas été générée correctement")
|
100 |
+
|
101 |
except Exception as e:
|
102 |
+
logger.error(f"Erreur lors de la génération de vidéo: {str(e)}")
|
103 |
+
return None
|
104 |
|
105 |
def process_inputs(self, image, audio, text):
|
106 |
"""Traite les entrées multimodales"""
|
107 |
try:
|
108 |
+
combined_parts = []
|
|
|
109 |
|
110 |
+
# Analyse de l'image si présente
|
111 |
+
if image is not None:
|
112 |
+
image_desc = self.analyze_image(image)
|
113 |
+
if image_desc:
|
114 |
+
combined_parts.append(f"Scene: {image_desc}")
|
115 |
|
116 |
+
# Transcription audio si présent
|
117 |
+
if audio is not None:
|
118 |
+
audio_text = self.transcribe_audio(audio)
|
119 |
+
if audio_text:
|
120 |
+
combined_parts.append(f"Audio narration: {audio_text}")
|
121 |
+
|
122 |
+
# Ajout du texte si présent
|
123 |
if text:
|
124 |
+
combined_parts.append(f"Additional context: {text}")
|
125 |
|
126 |
+
# Création du prompt final
|
127 |
+
if combined_parts:
|
128 |
+
final_prompt = " ".join(combined_parts)
|
129 |
else:
|
130 |
+
final_prompt = "Empty scene with neutral background"
|
131 |
+
|
132 |
+
# Génération de la vidéo
|
133 |
+
output_video = self.generate_video(final_prompt)
|
134 |
|
135 |
+
return output_video, final_prompt
|
136 |
|
137 |
except Exception as e:
|
138 |
logger.error(f"Erreur lors du traitement des entrées: {str(e)}")
|
139 |
+
return None, "Une erreur est survenue lors du traitement des entrées."
|
140 |
|
141 |
def create_interface():
|
142 |
"""Crée l'interface Gradio"""
|
|
|
150 |
gr.Textbox(label="Entrez du texte additionnel")
|
151 |
],
|
152 |
outputs=[
|
153 |
+
gr.Video(label="Vidéo générée"),
|
154 |
+
gr.Textbox(label="Description utilisée")
|
155 |
],
|
156 |
+
title="Générateur de Vidéo Multimodal",
|
157 |
description="""
|
158 |
+
Téléchargez une image, un fichier audio et/ou ajoutez du texte.
|
159 |
+
L'application va:
|
160 |
+
1. Analyser l'image pour en extraire une description
|
161 |
+
2. Transcrire l'audio en texte
|
162 |
+
3. Combiner ces éléments avec votre texte
|
163 |
+
4. Générer une vidéo basée sur la description combinée
|
164 |
"""
|
165 |
)
|
166 |
|