Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,104 +3,284 @@ from streamlit_option_menu import option_menu
|
|
3 |
from dataclasses import dataclass
|
4 |
|
5 |
import google.generativeai as genai
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Configuration de la page
|
9 |
st.set_page_config(page_title="Application Avancée", layout="wide")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
# Contenu de la navbar avec des icônes appropriées
|
13 |
-
selected = option_menu(
|
14 |
-
menu_title=None, # Aucun titre pour le menu
|
15 |
-
options=[
|
16 |
-
"Chatbot intelligent",
|
17 |
-
"Text to speech",
|
18 |
-
"Zero shot image classification",
|
19 |
-
"Image to text",
|
20 |
-
],
|
21 |
-
icons=["robot", "volume-up", "camera", "file-text"], # Icônes pour chaque option
|
22 |
-
menu_icon="cast", # Icône du menu
|
23 |
-
default_index=0, # Option par défaut
|
24 |
-
orientation="horizontal",
|
25 |
-
) # Orientation du menu
|
26 |
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# Affichage du
|
32 |
-
|
33 |
-
"""
|
34 |
-
<div class='content'>
|
35 |
-
<h1>Chatbot intelligent</h1>
|
36 |
-
<p>Bienvenue dans la section Chatbot intelligent.</p>
|
37 |
-
</div>
|
38 |
-
""",
|
39 |
-
unsafe_allow_html=True,
|
40 |
-
)
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
class Message:
|
48 |
-
actor: str
|
49 |
-
payload: str
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
]
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
-
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
st.session_state[MESSAGES].append(Message(actor=USER, payload=prompt))
|
68 |
-
st.chat_message(USER).write(prompt)
|
69 |
-
generate = model.generate_content(prompt)
|
70 |
-
response: str = generate.text
|
71 |
-
st.session_state[MESSAGES].append(Message(actor=ASSISTANT, payload=response))
|
72 |
-
st.chat_message(ASSISTANT).write(response)
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
elif selected == "Text to speech":
|
76 |
-
st.markdown(
|
77 |
-
"""
|
78 |
-
<div class='content'>
|
79 |
-
<h1>Text to speech</h1>
|
80 |
-
<p>Explorez notre fonctionnalité de conversion de texte en parole.</p>
|
81 |
-
</div>
|
82 |
-
""",
|
83 |
-
unsafe_allow_html=True,
|
84 |
-
)
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
98 |
st.markdown(
|
99 |
"""
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
""",
|
105 |
unsafe_allow_html=True,
|
106 |
)
|
|
|
3 |
from dataclasses import dataclass
|
4 |
|
5 |
import google.generativeai as genai
|
6 |
+
from gtts import gTTS
|
7 |
+
from io import BytesIO
|
8 |
|
9 |
+
from langdetect import detect
|
10 |
+
import pycountry
|
11 |
+
from googletrans import Translator, LANGUAGES
|
12 |
+
import torchaudio
|
13 |
+
|
14 |
+
from transformers import VitsModel, AutoTokenizer, pipeline
|
15 |
+
import torch
|
16 |
+
|
17 |
+
import scipy.io.wavfile
|
18 |
+
import numpy as np
|
19 |
+
|
20 |
+
import base64
|
21 |
+
|
22 |
+
from PIL import Image
|
23 |
+
|
24 |
+
|
25 |
+
model = VitsModel.from_pretrained("facebook/mms-tts-spa")
|
26 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-spa")
|
27 |
+
|
28 |
+
|
29 |
+
model_name_zero_shot_classification = "openai/clip-vit-large-patch14-336"
|
30 |
+
classifier_zero_shot_classification = pipeline(
|
31 |
+
"zero-shot-image-classification", model=model_name_zero_shot_classification
|
32 |
+
)
|
33 |
|
34 |
# Configuration de la page
|
35 |
st.set_page_config(page_title="Application Avancée", layout="wide")
|
36 |
|
37 |
+
# Options Menu
|
38 |
+
with st.sidebar:
|
39 |
+
menu = option_menu(
|
40 |
+
"Onyx AI",
|
41 |
+
["Acceuil", "A propos"],
|
42 |
+
icons=["play-btn", "info-circle"],
|
43 |
+
menu_icon="intersect",
|
44 |
+
default_index=0,
|
45 |
+
)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Intro Page
|
49 |
+
if menu == "Acceuil":
|
50 |
+
# Header
|
51 |
|
52 |
+
# Contenu de la navbar avec des icônes appropriées
|
53 |
+
selected = option_menu(
|
54 |
+
menu_title=None, # Aucun titre pour le menu
|
55 |
+
options=[
|
56 |
+
"Chatbot",
|
57 |
+
"Text to speech",
|
58 |
+
"Classification",
|
59 |
+
"Image to text",
|
60 |
+
],
|
61 |
+
icons=[
|
62 |
+
"robot",
|
63 |
+
"volume-up",
|
64 |
+
"camera",
|
65 |
+
"file-text",
|
66 |
+
], # Icônes pour chaque option
|
67 |
+
menu_icon="cast", # Icône du menu
|
68 |
+
default_index=0, # Option par défaut
|
69 |
+
orientation="horizontal",
|
70 |
+
) # Orientation du menu
|
71 |
|
72 |
+
# Affichage du contenu basé sur le choix de la navbar avec st.markdown pour une meilleure intégration
|
73 |
+
if selected == "Chatbot":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Affichage du titre et du message de bienvenue
|
76 |
+
st.markdown(
|
77 |
+
"""
|
78 |
+
<div class='content'>
|
79 |
+
<h1>Chatbot intelligent</h1>
|
80 |
+
<p>Bienvenue dans la section Chatbot intelligent.</p>
|
81 |
+
</div>
|
82 |
+
""",
|
83 |
+
unsafe_allow_html=True,
|
84 |
+
)
|
85 |
|
86 |
+
genai.configure(api_key="AIzaSyDQ9e-O5oWqo-toEZN19y4zzp9A61oZpTU")
|
87 |
|
88 |
+
model = genai.GenerativeModel("gemini-pro")
|
|
|
|
|
|
|
89 |
|
90 |
+
@dataclass
|
91 |
+
class Message:
|
92 |
+
actor: str
|
93 |
+
payload: str
|
94 |
|
95 |
+
USER = "user"
|
96 |
+
ASSISTANT = "ai"
|
97 |
+
MESSAGES = "messages"
|
|
|
98 |
|
99 |
+
if MESSAGES not in st.session_state:
|
100 |
+
st.session_state[MESSAGES] = [
|
101 |
+
Message(
|
102 |
+
actor=ASSISTANT, payload="Salut! Comment puis-je vous aider ? 😎"
|
103 |
+
)
|
104 |
+
]
|
105 |
|
106 |
+
msg: Message
|
107 |
+
for msg in st.session_state[MESSAGES]:
|
108 |
+
st.chat_message(msg.actor).write(msg.payload)
|
109 |
|
110 |
+
prompt: str = st.chat_input("Enter a prompt here")
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
if prompt:
|
113 |
+
st.session_state[MESSAGES].append(Message(actor=USER, payload=prompt))
|
114 |
+
st.chat_message(USER).write(prompt)
|
115 |
+
generate = model.generate_content(prompt)
|
116 |
+
response: str = generate.text
|
117 |
+
st.session_state[MESSAGES].append(
|
118 |
+
Message(actor=ASSISTANT, payload=response)
|
119 |
+
)
|
120 |
+
st.chat_message(ASSISTANT).write(response)
|
121 |
|
122 |
+
elif selected == "Text to speech":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
+
# Affichage du titre et du message de bienvenue
|
125 |
+
st.markdown(
|
126 |
+
"""
|
127 |
+
<div class='content'>
|
128 |
+
<h1>Text to speech</h1>
|
129 |
+
<p>Bienvenue dans la section Text to speech en espagnol.</p>
|
130 |
+
</div>
|
131 |
+
""",
|
132 |
+
unsafe_allow_html=True,
|
133 |
+
)
|
134 |
+
|
135 |
+
@dataclass
|
136 |
+
class Message:
|
137 |
+
actor: str
|
138 |
+
payload: str
|
139 |
+
|
140 |
+
USER = "user"
|
141 |
+
ASSISTANT = "ai"
|
142 |
+
MESSAGES = "messages_audio"
|
143 |
+
|
144 |
+
if MESSAGES not in st.session_state:
|
145 |
+
st.session_state[MESSAGES] = [
|
146 |
+
Message(
|
147 |
+
actor=ASSISTANT,
|
148 |
+
payload="Salut! Quelle phrase (peut importe la langue) souhaitez-vous prononcer en espagnol ? 😎",
|
149 |
+
)
|
150 |
+
]
|
151 |
+
|
152 |
+
# msg: Message
|
153 |
+
for msg in st.session_state[MESSAGES]:
|
154 |
+
st.chat_message(msg.actor).write(msg.payload)
|
155 |
+
|
156 |
+
prompt: str = st.chat_input("Entrez le texte à prononcer :")
|
157 |
+
|
158 |
+
translator = Translator()
|
159 |
+
|
160 |
+
if prompt:
|
161 |
+
st.session_state[MESSAGES].append(Message(actor=USER, payload=prompt))
|
162 |
+
st.chat_message(USER).write(prompt)
|
163 |
+
|
164 |
+
lang = pycountry.languages.get(alpha_2=detect(prompt))
|
165 |
+
|
166 |
+
lang_name = lang.name if lang else "Langue inconnue"
|
167 |
+
|
168 |
+
translated = translator.translate(prompt, dest="es")
|
169 |
+
|
170 |
+
st.chat_message(ASSISTANT).write(
|
171 |
+
f"Langue détecté {lang_name} \n \n {translated.text} \n \n Génération de l'audio ..."
|
172 |
+
)
|
173 |
+
|
174 |
+
inputs = tokenizer(translated.text, return_tensors="pt")
|
175 |
+
|
176 |
+
# Créer un buffer en mémoire
|
177 |
+
buffer = BytesIO()
|
178 |
+
|
179 |
+
with torch.no_grad():
|
180 |
+
output = model(**inputs).waveform
|
181 |
+
|
182 |
+
# Sauvegarder l'audio généré dans le buffer
|
183 |
+
torchaudio.save(
|
184 |
+
buffer,
|
185 |
+
src=output,
|
186 |
+
sample_rate=model.config.sampling_rate,
|
187 |
+
format="wav", # Spécifiez explicitement le format si nécessaire
|
188 |
+
)
|
189 |
+
|
190 |
+
buffer.seek(0)
|
191 |
+
|
192 |
+
st.chat_message(ASSISTANT).audio(data=buffer, format="audio/wav")
|
193 |
+
|
194 |
+
elif selected == "Classification":
|
195 |
+
st.markdown(
|
196 |
+
"""
|
197 |
+
<div class='content'>
|
198 |
+
<h1>Zero shot image classification</h1>
|
199 |
+
<p>Découvrez la classification d'images avec zéro exemple.</p>
|
200 |
+
</div>
|
201 |
+
""",
|
202 |
+
unsafe_allow_html=True,
|
203 |
+
)
|
204 |
+
|
205 |
+
@dataclass
|
206 |
+
class Message:
|
207 |
+
actor: str
|
208 |
+
payload: str
|
209 |
+
|
210 |
+
USER = "user"
|
211 |
+
ASSISTANT = "ai"
|
212 |
+
|
213 |
+
def classify_image(image, labels):
|
214 |
+
image_to_classify = image
|
215 |
+
|
216 |
+
labels = labels.split(",")
|
217 |
+
|
218 |
+
labels_for_classification = labels
|
219 |
+
scores = classifier_zero_shot_classification(
|
220 |
+
image_to_classify, candidate_labels=labels_for_classification
|
221 |
+
)
|
222 |
+
|
223 |
+
return f"The highest score is {scores[0]['score']:.3f} for the label {scores[0]['label']}"
|
224 |
+
|
225 |
+
# Interface utilisateur pour l'entrée de l'image et du libellé
|
226 |
+
uploaded_image = st.file_uploader(
|
227 |
+
"Upload an image", type=["jpg", "png", "jpeg"]
|
228 |
+
)
|
229 |
+
|
230 |
+
label = st.text_input(placeholder="apple, banana, car, dog, elephant")
|
231 |
+
|
232 |
+
# Bouton pour envoyer l'image et le libellé
|
233 |
+
if st.button("Send"):
|
234 |
+
if uploaded_image is not None and label:
|
235 |
+
# Lire et stocker l'image temporairement
|
236 |
+
image = Image.open(uploaded_image)
|
237 |
+
|
238 |
+
st.chat_message(USER).image(image)
|
239 |
+
|
240 |
+
st.chat_message(ASSISTANT).text(classify_image(image, label))
|
241 |
+
|
242 |
+
# # Appeler la fonction add_message pour afficher l'image, le libellé et la réponse
|
243 |
+
# add_message(image, label)
|
244 |
+
|
245 |
+
else:
|
246 |
+
st.write("Please upload an image and enter a label.")
|
247 |
+
|
248 |
+
elif selected == "Image to text":
|
249 |
+
st.markdown(
|
250 |
+
"""
|
251 |
+
<div class='content'>
|
252 |
+
<h1>Image to text</h1>
|
253 |
+
<p>Convertissez des images en texte grâce à notre outil.</p>
|
254 |
+
</div>
|
255 |
+
""",
|
256 |
+
unsafe_allow_html=True,
|
257 |
+
)
|
258 |
+
|
259 |
+
|
260 |
+
elif menu == "A propos":
|
261 |
|
262 |
+
# Lorsque "About" est sélectionné, afficher le contenu À propos dans le corps principal
|
263 |
st.markdown(
|
264 |
"""
|
265 |
+
# À propos de cette application
|
266 |
+
|
267 |
+
Cette application est conçue pour démontrer diverses fonctionnalités intéressantes telles que le chatbot intelligent, la conversion de texte en parole, la classification d'images sans exemples et la conversion d'images en texte. Elle est construite en utilisant Streamlit, une bibliothèque incroyable pour créer des applications web en Python facilement et rapidement.
|
268 |
+
|
269 |
+
## Créateur
|
270 |
+
|
271 |
+
- [James Olongo](https://jamesolongo.vercel.app/)
|
272 |
+
|
273 |
+
## Technologies utilisées
|
274 |
+
|
275 |
+
- Streamlit
|
276 |
+
- PyTorch
|
277 |
+
- Transformers
|
278 |
+
- torchaudio
|
279 |
+
- gTTS
|
280 |
+
- Langdetect
|
281 |
+
- Googletrans
|
282 |
+
- Pycountry
|
283 |
+
- Google Generative AI
|
284 |
""",
|
285 |
unsafe_allow_html=True,
|
286 |
)
|