File size: 5,630 Bytes
e73c426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6a5ce6
e73c426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ba8f28
e73c426
 
a99cffc
e73c426
 
d6a5ce6
 
 
 
e73c426
d6a5ce6
 
 
e73c426
 
 
571b33f
d6a5ce6
 
 
e73c426
 
d6a5ce6
 
 
 
 
 
 
 
 
 
e73c426
d6a5ce6
e73c426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1354d41
e73c426
 
 
 
1
2
3
4
5
6
7
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import streamlit as st
import numpy as np
import tensorflow as tf
from PIL import Image 
import tempfile
import base64
import re
import json
import random_responses
import time
import matplotlib.pyplot as plt
import pathlib
from streamlit_extras.app_logo import add_logo
import zipfile


#Decoracion de la app
st.set_page_config(page_title="Imagen Classification", 
                   page_icon=":turtle:", 
                   layout="wide",
                   initial_sidebar_state="auto",
                   menu_items=None)

markdown = """
La app realiza clasificacion de imagenes de las siguientes especies:
- *Gopherus flavomarginatus*
- *Kinosternon flavescens*
- *Terrapene coahuila*
- *Trachemys scripta*
\n
El uso del Chatbot se desplega al clasificar tu imagen
- Algunos ejemplos de pregunta:
- Descripcion de *Gopherus flavomarginatus*
- Distribucion de *Trachemys scripta*
\n
:grey-background[*Developed  by Bruno Rodriguez*]
"""
st.sidebar.title("INFORMACION\nV.1.0")
st.sidebar.info(markdown)

logo = "./Clicker.jpg"
st.sidebar.image(logo)


st.markdown("<h1 style='text-align: center;'>ASK MY JORGE</h1>", unsafe_allow_html=True)

#Script main chatbot
def load_json(file):
    with open(file) as bot_responses:
        # print(f"Loaded '{file}' succesfully!")
        return json.load(bot_responses)

responses_data = load_json("./bot.json")

def get_responses(input_string):
    split_message = re.split(r'\s+|[,;?!.-]\s*', input_string.lower())
    score_list = []
    
    for response in responses_data:
        response_score = 0
        required_score = 0
        required_words = response["required_words"]
        
        if required_words:
            for word in split_message:
                if word in required_words:
                    required_score += 1
        
        if response_score == len(required_words):
            for word in split_message:
                if word in response["user_input"]:
                    response_score += 1 
        
        score_list.append(response_score)
        
        
    best_response = max(score_list)
    response_index = score_list.index(best_response)
        
    if input_string == "":
        return "Que quieres saber sobre las tortugas :)"
        
    if best_response != 0:
        return responses_data[response_index]["bot_response"]
        
    return random_responses.random_string()

#Tensorflow completo

##Clasificacion de imagen
#nombres de las tortugas
turtle_name = ['Gopherus flavomarginatus', 'Kinisternon flavescens', 'Terrapene coahuila', 'Trachemys scripta']

#carga del modelo
model = tf.keras.models.load_model('./turtle_model_V_1_8.keras')
###########################################

def classify_images(image_path):
      input_image = tf.keras.utils.load_img(image_path, target_size= (224, 224))
      input_image_array = tf.keras.utils.img_to_array(input_image)
      input_image_exp_dim = tf.expand_dims(input_image_array, 0)

      prediction = model.predict(input_image_exp_dim)
      result = tf.nn.softmax(prediction[0])
      outcome = f'Tu imagen es clasificada como: {turtle_name[np.argmax(result)]}'


# #     #Grafico de la distribucion de probabilidades
      class_turtle = ['Gopherus flavomarginatus', 
                      'Kinosternon flavescens', 
                      'Terrapene coahuila', 
                      'Trachemys scripta']
     
     
      fig, ax = plt.subplots(figsize=(3, 3))
      y_pos = np.arange(len(class_turtle))
      ax.barh(y_pos, prediction[0], align = 'center')
      ax.set_yticks(y_pos)
      ax.set_yticklabels(class_turtle)
      ax.invert_yaxis()
      ax.set_xlabel("Probality")
      ax.set_title("Turtle Classification")

      st.pyplot(fig)
         
      return outcome

#Carga de la imagen a clasificar
file = st.file_uploader("Porfavor carga una imagen", type = ["jpg","png"])

if file is not None:
    # Create a temporary file to save the uploaded image
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file.write(file.getbuffer())
        temp_file_path = temp_file.name
    
    image = Image.open(temp_file_path)
    st.image(image, width=200)

    # Classify the image using the temporary file path
    classification_result = classify_images(temp_file_path)
    st.markdown(classification_result)

#auto play de cancion    
#https://discuss.streamlit.io/t/how-to-play-an-audio-file-automatically-generated-using-text-to-speech-in-streamlit/33201/2   
    def autoplay_audio(file_path: str):
        with open(file_path, "rb") as f:
            data = f.read()
            b64 = base64.b64encode(data).decode()
            md = f"""
                 <audio controls autoplay="true">
                 <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
                 </audio>
                 """
            st.markdown(
                  md,
                 unsafe_allow_html=True,
            )

    #st.write("# Auto-playing Audio!")
    
    autoplay_audio("./Finish.mp3") 
    
#chatbot interface   
# Interfaz de Streamlit
    st.markdown('''### Chatbot de Tortugas 🐢 ''')
    st.markdown("""
    <style>
        .stTextInput input[aria-label="Tú:"] {
            background-color: #1db2cc;
            color: #000000;
        }
    </style>
    """, unsafe_allow_html=True)

    # Caja de texto para entrada del usuario
    user_input = st.text_input("Tú:", "")

    # Mostrar la respuesta del bot
    if user_input:
        bot_response = get_responses(user_input)
        st.text_area("BugNo:", bot_response, max_chars=None) 
    
else:
    #st.text("No has cargado imagen aún!")
    st.text("")