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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
load_dotenv()
|
| 4 |
+
import os
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import os
|
| 7 |
+
from googletrans import Translator
|
| 8 |
+
# import google.generativeai as genai # Commentez cette ligne si inutile pour text-to-speech
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from gtts import gTTS # Bibliothèque pour la synthèse vocale
|
| 11 |
+
import google.generativeai as genai
|
| 12 |
+
import requests
|
| 13 |
+
import base64
|
| 14 |
+
import io
|
| 15 |
+
import pandas as pd
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
st.set_page_config(layout="wide")
|
| 20 |
+
st.sidebar.image('logo.png',width=300)
|
| 21 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
| 22 |
+
def main():
|
| 23 |
+
|
| 24 |
+
menu =['Chatbot','Text-to-speech','Zero-shot-image-classification','Image-to-text']
|
| 25 |
+
choice = st.sidebar.selectbox('Select Menu',menu)
|
| 26 |
+
|
| 27 |
+
if choice == 'Chatbot':
|
| 28 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 29 |
+
|
| 30 |
+
model=genai.GenerativeModel('gemini-pro')
|
| 31 |
+
chat= model.start_chat(history=[])
|
| 32 |
+
st.title('Chatbot Intelligent')
|
| 33 |
+
st.write("<hr>", unsafe_allow_html=True)
|
| 34 |
+
def get_gemini_response(question):
|
| 35 |
+
response=chat.send_message(question,stream=True)
|
| 36 |
+
return response
|
| 37 |
+
|
| 38 |
+
# Initialize chat history
|
| 39 |
+
if "messages" not in st.session_state:
|
| 40 |
+
st.session_state.messages = []
|
| 41 |
+
|
| 42 |
+
# Display chat messages from history on app rerun
|
| 43 |
+
for message in st.session_state.messages:
|
| 44 |
+
with st.chat_message(message["role"]):
|
| 45 |
+
st.markdown(message["content"])
|
| 46 |
+
|
| 47 |
+
# React to user input
|
| 48 |
+
if prompt := st.chat_input("Quoi de neuf?"):
|
| 49 |
+
# Display user message in chat message container
|
| 50 |
+
st.chat_message("user").markdown(prompt)
|
| 51 |
+
# Add user message to chat history
|
| 52 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 53 |
+
|
| 54 |
+
response = get_gemini_response(prompt)
|
| 55 |
+
# Display assistant response in chat message container
|
| 56 |
+
with st.chat_message("assistant"):
|
| 57 |
+
for chunk in response:
|
| 58 |
+
st.write(chunk.text)
|
| 59 |
+
#st.markdown(response)
|
| 60 |
+
# Add assistant response to chat history
|
| 61 |
+
st.session_state.messages.append({"role": "assistant", "content":chunk.text })
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if choice == 'Text-to-speech':
|
| 65 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 66 |
+
|
| 67 |
+
st.header('Transformez votre Texte en Espagnol avec Audio')
|
| 68 |
+
st.write("<hr>", unsafe_allow_html=True)
|
| 69 |
+
def get_text_to_speech(texte):
|
| 70 |
+
# Traduction du texte en espagnol
|
| 71 |
+
texte_espagnol = traduire_texte(texte)
|
| 72 |
+
|
| 73 |
+
# Configuration de la langue et de la vitesse (optionnel)
|
| 74 |
+
tts = gTTS(text=texte_espagnol, lang='es', slow=False)
|
| 75 |
+
|
| 76 |
+
# Enregistrement du fichier audio
|
| 77 |
+
tts.save("output.mp3")
|
| 78 |
+
|
| 79 |
+
# Lire le fichier audio dans Streamlit (nécessite des bibliothèques supplémentaires)
|
| 80 |
+
st.audio("output.mp3")
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def traduire_texte(texte):
|
| 84 |
+
traducteur = Translator()
|
| 85 |
+
traduction = traducteur.translate(texte, dest="es")
|
| 86 |
+
return traduction.text
|
| 87 |
+
|
| 88 |
+
# CSS personnalisé pour le bouton avec un fond vert
|
| 89 |
+
custom_css = """
|
| 90 |
+
<style>
|
| 91 |
+
.stButton>button {
|
| 92 |
+
background-color: green;
|
| 93 |
+
color: white;
|
| 94 |
+
padding: 10px 30px; /* Ajuster la hauteur et la largeur du bouton */
|
| 95 |
+
font-size: 16px; /* Ajuster la taille de la police du texte */
|
| 96 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 97 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 98 |
+
margin-left: auto; /* Positionner le bouton à droite */
|
| 99 |
+
margin-right: 0; /* Positionner le bouton à droite */
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/* Style pour le survol du bouton */
|
| 103 |
+
.stButton>button:hover {
|
| 104 |
+
background-color: white;
|
| 105 |
+
color: green;
|
| 106 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 107 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 108 |
+
}
|
| 109 |
+
</style>
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# Affichage du CSS personnalisé
|
| 116 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
def input_text_setup():
|
| 119 |
+
text = st.text_area("Entrez le texte à convertir en parole :", height=200,placeholder="Veuillez entre votre text a traduire...")
|
| 120 |
+
submit_button = st.button("Soumettre") # Ajout du bouton de soumission
|
| 121 |
+
return text, submit_button
|
| 122 |
+
|
| 123 |
+
# Section pour la conversion text-to-speech
|
| 124 |
+
# Afficher le champ de texte et le bouton de soumission
|
| 125 |
+
text_to_convert, submit_button = input_text_setup()
|
| 126 |
+
|
| 127 |
+
if submit_button:
|
| 128 |
+
if text_to_convert:
|
| 129 |
+
texte_traduit = traduire_texte(text_to_convert)
|
| 130 |
+
st.success("Texte converti en parole espagnole avec sucess.")
|
| 131 |
+
get_text_to_speech(texte_traduit)
|
| 132 |
+
# Section pour l'analyse d'images (peut rester commentée si non utilisée)
|
| 133 |
+
else:
|
| 134 |
+
st.warning("Veuillez entrer du texte avant de soumettre.")
|
| 135 |
+
|
| 136 |
+
if choice == 'Zero-shot-image-classification':
|
| 137 |
+
st.title("CLIP Image Classification")
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
API_URL = "https://api-inference.huggingface.co/models/openai/clip-vit-large-patch14"
|
| 141 |
+
hugging_face_token = os.getenv('API_TOKEN')
|
| 142 |
+
def query(data):
|
| 143 |
+
img_bytes = io.BytesIO(data["image_bytes"])
|
| 144 |
+
payload = {
|
| 145 |
+
"parameters": data["parameters"],
|
| 146 |
+
"inputs": base64.b64encode(img_bytes.read()).decode("utf-8")
|
| 147 |
+
}
|
| 148 |
+
headers = {"Authorization": f"Bearer {hugging_face_token}"}
|
| 149 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 150 |
+
return response.json()
|
| 151 |
+
custom_css = """
|
| 152 |
+
<style>
|
| 153 |
+
.stButton>button {
|
| 154 |
+
background-color: green;
|
| 155 |
+
color: white;
|
| 156 |
+
padding: 10px 30px; /* Ajuster la hauteur et la largeur du bouton */
|
| 157 |
+
font-size: 16px; /* Ajuster la taille de la police du texte */
|
| 158 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 159 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 160 |
+
margin-left: auto; /* Positionner le bouton à droite */
|
| 161 |
+
margin-right: 0; /* Positionner le bouton à droite */
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
/* Stylisation des étiquettes prédites */
|
| 165 |
+
.st-emotion-cache-cnbvxy {
|
| 166 |
+
margin-top: 20px;/* Marge supérieure */
|
| 167 |
+
font-size: 100px !important; /* Taille de la police */
|
| 168 |
+
}
|
| 169 |
+
/* Style pour chaque étiquette prédite */
|
| 170 |
+
.predicted-label {
|
| 171 |
+
color: green; /* Texte vert */
|
| 172 |
+
margin-bottom: 5px; /* Marge inférieure */
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
/* Style pour le survol du bouton */
|
| 176 |
+
.stButton>button:hover {
|
| 177 |
+
background-color: white;
|
| 178 |
+
color: green;
|
| 179 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 180 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 181 |
+
}
|
| 182 |
+
</style>
|
| 183 |
+
"""
|
| 184 |
+
# Affichage du CSS personnalisé
|
| 185 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 186 |
+
uploaded_image = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
| 187 |
+
if uploaded_image is not None:
|
| 188 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
| 189 |
+
st.title("Entre les Labels (obligatoire)")
|
| 190 |
+
candidate_labels = st.text_input("Entrez les étiquettes séparées par des virgules",placeholder='Veullez entrer les classes separé par des virgules. ex: chat, chien, souris ...')
|
| 191 |
+
parameters = {"candidate_labels": candidate_labels.split(",")}
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if st.button("Classifier"):
|
| 195 |
+
if candidate_labels.strip() == "":
|
| 196 |
+
st.warning("Veuillez entrer au moins une étiquette. Au-delà de deux étiquettes, veuillez vous assurer de les séparer par des virgules et des espaces.")
|
| 197 |
+
else:
|
| 198 |
+
parameters = {"candidate_labels": candidate_labels.split(",")}
|
| 199 |
+
result = query({"image_bytes": uploaded_image.read(), "parameters": parameters})
|
| 200 |
+
if result:
|
| 201 |
+
# Début du tableau
|
| 202 |
+
st.title("Predicted Labels:")
|
| 203 |
+
# Création d'une liste pour stocker les labels et scores
|
| 204 |
+
labels = []
|
| 205 |
+
scores = []
|
| 206 |
+
for prediction in result:
|
| 207 |
+
label = prediction["label"]
|
| 208 |
+
score = prediction["score"]
|
| 209 |
+
labels.append(label)
|
| 210 |
+
scores.append(score)
|
| 211 |
+
# Création d'un DataFrame avec les labels et les scores
|
| 212 |
+
df = pd.DataFrame({
|
| 213 |
+
'Label': labels,
|
| 214 |
+
'Score': scores,
|
| 215 |
+
})
|
| 216 |
+
# Affichage du DataFrame avec une largeur ajustée
|
| 217 |
+
st.dataframe(df, width=600)
|
| 218 |
+
else:
|
| 219 |
+
st.write("No labels predicted.")
|
| 220 |
+
|
| 221 |
+
if choice == 'Image-to-text':
|
| 222 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 223 |
+
|
| 224 |
+
custom_css = """
|
| 225 |
+
<style>
|
| 226 |
+
.stButton>button {
|
| 227 |
+
background-color: green;
|
| 228 |
+
color: white;
|
| 229 |
+
padding: 10px 30px; /* Ajuster la hauteur et la largeur du bouton */
|
| 230 |
+
font-size: 16px; /* Ajuster la taille de la police du texte */
|
| 231 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 232 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 233 |
+
margin-left: auto; /* Positionner le bouton à droite */
|
| 234 |
+
margin-right: 0; /* Positionner le bouton à droite */
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
/* Style pour le survol du bouton */
|
| 239 |
+
.stButton>button:hover {
|
| 240 |
+
background-color: white;
|
| 241 |
+
color: green;
|
| 242 |
+
border: 2px solid green; /* Ajouter une bordure verte */
|
| 243 |
+
border-radius: 5px; /* Ajouter des coins arrondis à la bordure */
|
| 244 |
+
}
|
| 245 |
+
</style>
|
| 246 |
+
"""
|
| 247 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
| 248 |
+
def get_gemini_response(input_prompt,image):
|
| 249 |
+
model=genai.GenerativeModel('gemini-pro-vision')
|
| 250 |
+
response = model.generate_content([input_prompt,image[0]])
|
| 251 |
+
return response.text
|
| 252 |
+
def input_image_setup(uploaded_file):
|
| 253 |
+
if uploaded_file is not None:
|
| 254 |
+
bytes_data = uploaded_file.getvalue()
|
| 255 |
+
image_parts = [{'mime_type':uploaded_file.type,
|
| 256 |
+
'data':bytes_data}]
|
| 257 |
+
return image_parts
|
| 258 |
+
else:
|
| 259 |
+
raise FileNotFoundError('No File Uploaded')
|
| 260 |
+
|
| 261 |
+
st.header('Description d une image')
|
| 262 |
+
st.write("<hr>", unsafe_allow_html=True)
|
| 263 |
+
uploaded_file = st.file_uploader('Choose an image.',type=['jpg','jpeg','png'])
|
| 264 |
+
if uploaded_file is not None:
|
| 265 |
+
image=Image.open(uploaded_file)
|
| 266 |
+
st.image(image,caption="uploaded_file",use_column_width=True)
|
| 267 |
+
st.write("<hr>", unsafe_allow_html=True)
|
| 268 |
+
submit=st.button('Decrire')
|
| 269 |
+
input_prompt="""
|
| 270 |
+
Decrire l'image
|
| 271 |
+
"""
|
| 272 |
+
if submit:
|
| 273 |
+
if uploaded_file is not None:
|
| 274 |
+
image_data = input_image_setup(uploaded_file)
|
| 275 |
+
response = get_gemini_response(input_prompt,image_data)
|
| 276 |
+
st.subheader('La description de l\'image est : ')
|
| 277 |
+
st.markdown(
|
| 278 |
+
f"<div style='font-size: 18px; border-radius: 10px; border: 2px solid #777777;; padding: 20px; line-height: 1.7;'>{response}</div>",
|
| 279 |
+
unsafe_allow_html=True )
|
| 280 |
+
else:
|
| 281 |
+
st.error("Veuillez entrer une image en entrée")
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
if __name__ == '__main__':
|
| 285 |
+
main()
|
| 286 |
+
|
| 287 |
+
#https://huggingface.co/spaces/DJONG-WANG/projet_tutore
|