ashok2216 commited on
Commit
feaeab3
1 Parent(s): 6c168ef

Upload 20 files

Browse files
.devcontainer/devcontainer.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "Python 3",
3
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
4
+ "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
5
+ "customizations": {
6
+ "codespaces": {
7
+ "openFiles": [
8
+ "README.md",
9
+ "app.py"
10
+ ]
11
+ },
12
+ "vscode": {
13
+ "settings": {},
14
+ "extensions": [
15
+ "ms-python.python",
16
+ "ms-python.vscode-pylance"
17
+ ]
18
+ }
19
+ },
20
+ "updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
21
+ "postAttachCommand": {
22
+ "server": "streamlit run app.py --server.enableCORS false --server.enableXsrfProtection false"
23
+ },
24
+ "portsAttributes": {
25
+ "8501": {
26
+ "label": "Application",
27
+ "onAutoForward": "openPreview"
28
+ }
29
+ },
30
+ "forwardPorts": [
31
+ 8501
32
+ ]
33
+ }
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ BirdSpecies_Classification.ipynb filter=lfs diff=lfs merge=lfs -text
.github/workflows/python-app.yml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This workflow will install Python dependencies, run tests and lint with a single version of Python
2
+ # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
3
+
4
+ name: Python application in Streamlit
5
+ on:
6
+ push:
7
+ branches: [ "main" ]
8
+ pull_request:
9
+ branches: [ "main" ]
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ build:
16
+
17
+ runs-on: ubuntu-latest
18
+
19
+ steps:
20
+ - uses: actions/checkout@v4
21
+ - name: Set up Python 3.10
22
+ uses: actions/setup-python@v3
23
+ with:
24
+ python-version: "3.10"
25
+ - name: Install dependencies
26
+ run: |
27
+ python -m pip install --upgrade pip
28
+ if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
29
+ # - name: Run Streamlit app
30
+ # run: streamlit run app.py
31
+ - name: Deploy
32
+ # Only run this step if the branch is main
33
+ if: github.ref == 'refs/heads/main'
34
+ env:
35
+ deploy_url: ${{ secrets.RENDER_DEPLOY_HOOK_URL }}
36
+ run: |
37
+ curl "$deploy_url"
BirdSpecies_Classification.ipynb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3ba76fa455187b5856a3d5315ffc815b3e27feedc3f7c23761d17b71945ad23
3
+ size 22378841
__pycache__/audio_analysis.cpython-39.pyc ADDED
Binary file (1.53 kB). View file
 
__pycache__/audio_processing.cpython-39.pyc ADDED
Binary file (1.09 kB). View file
 
app.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Copyright 2024 Ashok Kumar
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.'''
14
+
15
+
16
+
17
+ import os
18
+ import numpy as np
19
+ import warnings
20
+ import librosa
21
+ import streamlit as st
22
+ import tempfile
23
+ import json
24
+ from PIL import Image
25
+ import pandas as pd
26
+ from joblib import dump, load
27
+ import wikipedia
28
+ import requests
29
+ # import wikipediaapi
30
+ from sklearn.preprocessing import LabelEncoder
31
+ from tensorflow.keras.models import load_model
32
+ # import soundfile as sf
33
+ from audio_analysis import audio_signals
34
+ from audio_processing import extract_features
35
+ import os
36
+ from dotenv import load_dotenv
37
+ import json
38
+ import streamlit as st
39
+ from huggingface_hub import InferenceApi, login, InferenceClient
40
+
41
+
42
+ st.set_page_config(
43
+ page_title="BirdSense",
44
+ page_icon=":bird:",
45
+ initial_sidebar_state="expanded",
46
+ menu_items={
47
+ 'Get Help': 'https://ashok2216-myportfolio-github-io.vercel.app/#contact',
48
+ 'Report a bug': "https://ashok2216-myportfolio-github-io.vercel.app/#contact",
49
+ 'About': "https://ashok2216-myportfolio-github-io.vercel.app/"
50
+ }
51
+ )
52
+
53
+ # Get the Hugging Face token from environment variables
54
+ load_dotenv()
55
+ hf_token = os.getenv("HF_TOKEN")
56
+ if hf_token is None:
57
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
58
+ # Authenticate with Hugging Face
59
+ login(hf_token)
60
+
61
+ # Model information and links
62
+ model_links = {
63
+ "Zephyr-7B": "HuggingFaceH4/zephyr-7b-beta"
64
+ }
65
+ model_info = {
66
+ "Zephyr-7B": {
67
+ 'description': """Zephyr 7B is a Huggingface model, fine-tuned for helpful and instructive interactions.""",
68
+ 'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'
69
+ }
70
+ }
71
+
72
+ # Inference API Initialization
73
+ client = InferenceClient('HuggingFaceH4/zephyr-7b-beta')
74
+
75
+ # Reset conversation button
76
+ def reset_conversation():
77
+ return [
78
+ {"role": "system", "content": "You are a knowledgeable and empathetic ornithologist assistant providing accurate and relevant information based on user input."}
79
+ ]
80
+
81
+ # Initialize conversation and chat history
82
+ messages = reset_conversation()
83
+
84
+ # Display chat history
85
+ # for message in messages:
86
+ # with st.chat_message(message["role"]):
87
+ # print(message["content"])
88
+
89
+ def respond(message, history, max_tokens, temperature, top_p):
90
+ # Prepare the list of messages for the chat completion
91
+ messages = [{"role": "system", "content": history[0]["content"]}]
92
+
93
+ for val in history:
94
+ if val["role"] == "user":
95
+ messages.append({"role": "user", "content": val["content"]})
96
+ elif val["role"] == "assistant":
97
+ messages.append({"role": "assistant", "content": val["content"]})
98
+
99
+ messages.append({"role": "user", "content": message})
100
+
101
+ # Generate response
102
+ response = ""
103
+ response_container = st.empty() # Placeholder to update the response text dynamically
104
+
105
+ for message in client.chat_completion(
106
+ messages,
107
+ max_tokens=max_tokens,
108
+ stream=True,
109
+ temperature=temperature,
110
+ top_p=top_p,
111
+ ):
112
+ token = message.choices[0].delta.content
113
+ response += token
114
+ # response_container.text(response) # Stream the response
115
+
116
+ return response
117
+ # Load environment variables
118
+ load_dotenv()
119
+ hf_token = os.getenv("HF_TOKEN")
120
+ if hf_token is None:
121
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
122
+ # Authenticate with Hugging Face
123
+ login(hf_token)
124
+
125
+ image = Image.open('logo.PNG')
126
+ st.image(
127
+ image, width=250
128
+ )
129
+ st.subheader('Bird Species Classification')
130
+ # st.markdown('Sound of 114 Bird Species :bird: :penguin: :hatched_chick:')
131
+ st.header('', divider='rainbow')
132
+
133
+ @st.cache_data
134
+ def loaded_model(model_path):
135
+ return load_model(model_path)
136
+
137
+ @st.cache_data
138
+ def predict_class(audio_path, model):
139
+ extracted_feature = extract_features(audio_path)
140
+ extracted_feature = extracted_feature.reshape(1, 1, extracted_feature.shape[0])
141
+ prediction = model.predict(extracted_feature)
142
+ predicted_class_index = np.argmax(prediction)
143
+ print('HI',predicted_class_index)
144
+ # predicted_class_label = label_encoder.inverse_transform([predicted_class_index])[0]
145
+ return predicted_class_index
146
+
147
+
148
+ audio_file = st.file_uploader("Upload an Audio file", type=["mp3", "wav", "ogg"], accept_multiple_files=False)
149
+ # Load the model
150
+ model_path = 'bird_audio_classification_model.h5'
151
+ model = loaded_model(model_path)
152
+
153
+ class_file = open('classes.json', 'r').read()
154
+ labels_list = json.loads(class_file)
155
+
156
+ st.markdown('Download the Sample Audio here :point_down:')
157
+ st.page_link("https://dibird.com/", label="DiBird.com", icon="🐦")
158
+ st.subheader('Scientific Name of 114 Birds Species :bird:')
159
+
160
+ with st.container(height=300):
161
+ st.markdown(list(labels_list.values()))
162
+ # birds = pd.DataFrame(class_file)
163
+ # st.table(birds)
164
+ st.header('', divider='rainbow')
165
+
166
+ if audio_file is not None:
167
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
168
+ tmp_file.write(audio_file.read())
169
+
170
+ file_path = tmp_file.name
171
+ audio_data, sampling_rate = librosa.load(file_path)
172
+ st.audio(audio_data, sample_rate=sampling_rate)
173
+ st.success("Audio file successfully uploaded and stored temporally.")
174
+ audio_signals(file_path)
175
+ # Predict the class
176
+ y_predict = predict_class(file_path, model)
177
+ # Display predicted class
178
+ if str(y_predict) in labels_list.keys():
179
+ pred = labels_list[str(y_predict)][:-6]
180
+ st.subheader(f'Predicted Class: :rainbow[{pred}]')
181
+ st.image(wikipedia.page(pred).images[0], caption=labels_list[str(y_predict)][:-6], width=200)
182
+ st.markdown(wikipedia.summary(pred))
183
+
184
+ user_input = f"Explain about {pred} bird"
185
+ # Generate and display assistant response
186
+ if user_input:
187
+ response = respond(user_input, messages, max_tokens = 500, temperature = 0.70, top_p = 0.95)
188
+ st.markdown(response)
189
+ messages.append({"role": "assistant", "content": response})
190
+ st.page_link(wikipedia.page(pred).url, label="Explore more in Wikipedia.com", icon="🌎")
191
+ # st.session_state.messages.append({"role": "assistant", "content": response})
192
+ else:
193
+ st.write('Class not Found')
194
+ else:
195
+ st.markdown('File not Found!')
audio_analysis.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Copyright 2024 Ashok Kumar
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.'''
14
+
15
+
16
+ import os
17
+ import matplotlib.pyplot as plt
18
+ plt.style.use('dark_background')
19
+ import seaborn as sns
20
+ import numpy as np
21
+ import librosa
22
+ from IPython.display import Audio
23
+ # import soundfile as sf
24
+ import streamlit as st
25
+
26
+
27
+ def audio_waveframe(file_path):
28
+ # Load the audio file
29
+ audio_data, sampling_rate = librosa.load(file_path)
30
+ # Calculate the duration of the audio file
31
+ duration = len(audio_data) / sampling_rate
32
+ # Create a time array for plotting
33
+ time = np.arange(0, duration, 1/sampling_rate)
34
+ # Plot the waveform
35
+ plt.figure(figsize=(30, 10))
36
+ plt.plot(time, audio_data, color='blue')
37
+ plt.title('Audio Waveform')
38
+ plt.xlabel('Time (s)')
39
+ plt.ylabel('Amplitude')
40
+ # plt.savefig('audio_waveframe.png')
41
+ plot = st.pyplot(plt)
42
+
43
+ return plot
44
+
45
+
46
+ # def spectrogram(file_path):
47
+ # # Compute the short-time Fourier transform (STFT)
48
+ # n_fft = 500 # Number of FFT points 2048
49
+ # hop_length = 1 # Hop length for STFT 512
50
+ # audio_data, sampling_rate = librosa.load(file_path)
51
+ # stft = librosa.stft(audio_data, n_fft=n_fft, hop_length=hop_length)
52
+ # # Convert the magnitude spectrogram to decibels (log scale)
53
+ # spectrogram = librosa.amplitude_to_db(np.abs(stft))
54
+ # # Plot the spectrogram
55
+ # plt.figure(figsize=(30, 10))
56
+ # # librosa.display.specshow(spectrogram, sr=sampling_rate, hop_length=hop_length, x_axis='time', y_axis='linear')
57
+ # librosa.display.specshow(spectrogram, sr=sampling_rate, hop_length=hop_length)
58
+ # plt.colorbar(format='%+2.0f dB')
59
+ # plt.title('Spectrogram')
60
+ # plt.xlabel('Time (s)')
61
+ # plt.ylabel('Frequency (Hz)')
62
+ # plt.tight_layout()
63
+ # # plt.savefig('spectrogram.png')
64
+ # plot = st.pyplot(plt)
65
+
66
+ # return plot
67
+
68
+
69
+ def spectrogram(file_path):
70
+
71
+ y, sr = librosa.load(file_path)
72
+ # Compute the spectrogram
73
+ D = librosa.stft(y)
74
+ # Convert magnitude spectrogram to decibels
75
+ DB = librosa.amplitude_to_db(abs(D))
76
+ # Plot the spectrogram
77
+ plt.figure(figsize=(20, 5))
78
+ librosa.display.specshow(DB, sr=sr, x_axis='time', y_axis='hz')
79
+ plt.colorbar(format='%+2.0f dB')
80
+ plt.title('Spectrogram')
81
+ plt.xlabel('Time (s)')
82
+ plt.ylabel('Frequency (Hz)')
83
+ plt.tight_layout()
84
+ plot = st.pyplot(plt)
85
+
86
+ return plot
87
+
88
+
89
+
90
+ def audio_signals(file_path):
91
+ aw = audio_waveframe(file_path)
92
+ spg = spectrogram(file_path)
93
+
94
+ return aw, spg
audio_processing.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Copyright 2024 Ashok Kumar
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.'''
14
+
15
+
16
+ import os
17
+ import matplotlib.pyplot as plt
18
+ plt.style.use('dark_background')
19
+ import seaborn as sns
20
+ import numpy as np
21
+ import librosa
22
+ from IPython.display import Audio
23
+ import pandas as pd
24
+
25
+
26
+ # Function to extract features from audio file
27
+ def extract_features(file_path):
28
+ # Load audio file
29
+ audio, sample_rate = librosa.load(file_path)
30
+ # Extract features using Mel-Frequency Cepstral Coefficients (MFCC)
31
+ mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
32
+ # Flatten the features into a 1D array
33
+ flattened_features = np.mean(mfccs.T, axis=0)
34
+
35
+ return flattened_features
36
+
37
+ # Function to load dataset and extract features
38
+ def load_data_and_extract_features(data_dir):
39
+ labels = []
40
+ features = []
41
+ # Loop through each audio file in the dataset directory
42
+ for filename in os.listdir(data_dir):
43
+ if filename.endswith('.wav'):
44
+ file_path = os.path.join(data_dir, filename)
45
+ # Extract label from filename
46
+ label = filename.split('-')[0]
47
+ labels.append(label)
48
+ # Extract features from audio file
49
+ feature = extract_features(file_path)
50
+ features.append(feature)
51
+
52
+ return np.array(features), np.array(labels)
audio_record.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''Copyright 2024 Ashok Kumar
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.'''
14
+
15
+
16
+
17
+ import pyaudio
18
+ import wave
19
+
20
+ def record_audio(output_filename, duration=5, sample_rate=44100, channels=2, chunk=1024, format=pyaudio.paInt16):
21
+ audio = pyaudio.PyAudio()
22
+
23
+ # Open recording stream
24
+ stream = audio.open(format=format,
25
+ channels=channels,
26
+ rate=sample_rate,
27
+ input=True,
28
+ frames_per_buffer=chunk)
29
+
30
+ print("Recording...")
31
+
32
+ frames = []
33
+
34
+ # Record for the specified duration
35
+ for i in range(0, int(sample_rate / chunk * duration)):
36
+ data = stream.read(chunk)
37
+ frames.append(data)
38
+
39
+ print("Finished recording.")
40
+
41
+ # Stop and close the stream
42
+ stream.stop_stream()
43
+ stream.close()
44
+ audio.terminate()
45
+
46
+ # Save the recorded audio to a WAV file
47
+ with wave.open(output_filename, 'wb') as wf:
48
+ wf.setnchannels(channels)
49
+ wf.setsampwidth(audio.get_sample_size(format))
50
+ wf.setframerate(sample_rate)
51
+ wf.writeframes(b''.join(frames))
52
+
53
+ # output_filename = "recorded_audio.wav"
54
+ # record_audio(output_filename, duration=5)
55
+ # print(f"Audio recorded and saved as {output_filename}")
56
+
57
+ # Example usage
58
+ # output_filename = "recorded_audio.wav"
59
+ # record_audio(output_filename, duration=5)
60
+ # print(f"Audio recorded and saved as {output_filename}")
base/llm.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ import json
4
+ import streamlit as st
5
+ from huggingface_hub import InferenceApi, login, InferenceClient
6
+
7
+ # Get the Hugging Face token from environment variables
8
+ load_dotenv()
9
+ hf_token = os.getenv("HF_TOKEN")
10
+ if hf_token is None:
11
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
12
+ # Authenticate with Hugging Face
13
+ login(hf_token)
14
+
15
+ # Model information and links
16
+ model_links = {
17
+ "Zephyr-7B": "HuggingFaceH4/zephyr-7b-beta"
18
+ }
19
+ model_info = {
20
+ "Zephyr-7B": {
21
+ 'description': """Zephyr 7B is a Huggingface model, fine-tuned for helpful and instructive interactions.""",
22
+ 'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'
23
+ }
24
+ }
25
+
26
+ # Inference API Initialization
27
+ client = InferenceClient('HuggingFaceH4/zephyr-7b-beta')
28
+
29
+ # Reset conversation button
30
+ def reset_conversation():
31
+ return [
32
+ {"role": "system", "content": "You are a knowledgeable and empathetic ornithologist assistant providing accurate and relevant information based on user input."}
33
+ ]
34
+
35
+ # Initialize conversation and chat history
36
+ messages = reset_conversation()
37
+
38
+ # Display chat history
39
+ for message in messages:
40
+ with st.chat_message(message["role"]):
41
+ st.markdown(message["content"])
42
+
43
+ def respond(message, history, max_tokens, temperature, top_p):
44
+ # Prepare the list of messages for the chat completion
45
+ messages = [{"role": "system", "content": history[0]["content"]}]
46
+
47
+ for val in history:
48
+ if val["role"] == "user":
49
+ messages.append({"role": "user", "content": val["content"]})
50
+ elif val["role"] == "assistant":
51
+ messages.append({"role": "assistant", "content": val["content"]})
52
+
53
+ messages.append({"role": "user", "content": message})
54
+
55
+ # Generate response
56
+ response = ""
57
+ response_container = st.empty() # Placeholder to update the response text dynamically
58
+
59
+ for message in client.chat_completion(
60
+ messages,
61
+ max_tokens=max_tokens,
62
+ stream=True,
63
+ temperature=temperature,
64
+ top_p=top_p,
65
+ ):
66
+ token = message.choices[0].delta.content
67
+ response += token
68
+ # response_container.text(response) # Stream the response
69
+
70
+ return response
base/main.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import warnings
4
+ import librosa
5
+ import streamlit as st
6
+ import tempfile
7
+ import json
8
+ from PIL import Image
9
+ import pandas as pd
10
+ from joblib import dump, load
11
+ import wikipedia
12
+ import requests
13
+ # import wikipediaapi
14
+ from sklearn.preprocessing import LabelEncoder
15
+ from tensorflow.keras.models import load_model
16
+ # import soundfile as sf
17
+ from audio_analysis import audio_signals
18
+ from audio_processing import extract_features
19
+ import os
20
+ from dotenv import load_dotenv
21
+ import json
22
+ import streamlit as st
23
+ from huggingface_hub import InferenceApi, login, InferenceClient
24
+
25
+
26
+ st.set_page_config(
27
+ page_title="BirdSense",
28
+ page_icon=":bird:",
29
+ initial_sidebar_state="expanded",
30
+ menu_items={
31
+ 'Get Help': 'https://ashok2216-myportfolio-github-io.vercel.app/#contact',
32
+ 'Report a bug': "https://ashok2216-myportfolio-github-io.vercel.app/#contact",
33
+ 'About': "https://ashok2216-myportfolio-github-io.vercel.app/"
34
+ }
35
+ )
36
+
37
+ # Get the Hugging Face token from environment variables
38
+ load_dotenv()
39
+ hf_token = os.getenv("HF_TOKEN")
40
+ if hf_token is None:
41
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
42
+ # Authenticate with Hugging Face
43
+ login(hf_token)
44
+
45
+ # Model information and links
46
+ model_links = {
47
+ "Zephyr-7B": "HuggingFaceH4/zephyr-7b-beta"
48
+ }
49
+ model_info = {
50
+ "Zephyr-7B": {
51
+ 'description': """Zephyr 7B is a Huggingface model, fine-tuned for helpful and instructive interactions.""",
52
+ 'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'
53
+ }
54
+ }
55
+
56
+ # Inference API Initialization
57
+ client = InferenceClient('HuggingFaceH4/zephyr-7b-beta')
58
+
59
+ # Reset conversation button
60
+ def reset_conversation():
61
+ return [
62
+ {"role": "system", "content": "You are a knowledgeable and empathetic ornithologist assistant providing accurate and relevant information based on user input."}
63
+ ]
64
+
65
+ # Initialize conversation and chat history
66
+ messages = reset_conversation()
67
+
68
+ # Display chat history
69
+ for message in messages:
70
+ with st.chat_message(message["role"]):
71
+ st.markdown(message["content"])
72
+
73
+ def respond(message, history, max_tokens, temperature, top_p):
74
+ # Prepare the list of messages for the chat completion
75
+ messages = [{"role": "system", "content": history[0]["content"]}]
76
+
77
+ for val in history:
78
+ if val["role"] == "user":
79
+ messages.append({"role": "user", "content": val["content"]})
80
+ elif val["role"] == "assistant":
81
+ messages.append({"role": "assistant", "content": val["content"]})
82
+
83
+ messages.append({"role": "user", "content": message})
84
+
85
+ # Generate response
86
+ response = ""
87
+ response_container = st.empty() # Placeholder to update the response text dynamically
88
+
89
+ for message in client.chat_completion(
90
+ messages,
91
+ max_tokens=max_tokens,
92
+ stream=True,
93
+ temperature=temperature,
94
+ top_p=top_p,
95
+ ):
96
+ token = message.choices[0].delta.content
97
+ response += token
98
+ # response_container.text(response) # Stream the response
99
+
100
+ return response
101
+ # Load environment variables
102
+ load_dotenv()
103
+ hf_token = os.getenv("HF_TOKEN")
104
+ if hf_token is None:
105
+ raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")
106
+ # Authenticate with Hugging Face
107
+ login(hf_token)
108
+
109
+ image = Image.open('logo.PNG')
110
+ st.image(
111
+ image, width=250
112
+ )
113
+ st.subheader('Bird Species Classification')
114
+ # st.markdown('Sound of 114 Bird Species :bird: :penguin: :hatched_chick:')
115
+ st.header('', divider='rainbow')
116
+
117
+ @st.cache_data
118
+ def loaded_model(model_path):
119
+ return load_model(model_path)
120
+
121
+ @st.cache_data
122
+ def predict_class(audio_path, model):
123
+ extracted_feature = extract_features(audio_path)
124
+ extracted_feature = extracted_feature.reshape(1, 1, extracted_feature.shape[0])
125
+ prediction = model.predict(extracted_feature)
126
+ predicted_class_index = np.argmax(prediction)
127
+ print('HI',predicted_class_index)
128
+ # predicted_class_label = label_encoder.inverse_transform([predicted_class_index])[0]
129
+ return predicted_class_index
130
+
131
+
132
+ audio_file = st.file_uploader("Upload an Audio file", type=["mp3", "wav", "ogg"], accept_multiple_files=False)
133
+ # Load the model
134
+ model_path = 'bird_audio_classification_model.h5'
135
+ model = loaded_model(model_path)
136
+
137
+ class_file = open('classes.json', 'r').read()
138
+ labels_list = json.loads(class_file)
139
+
140
+ st.markdown('Download the Sample Audio here :point_down:')
141
+ st.page_link("https://dibird.com/", label="DiBird.com", icon="🐦")
142
+ st.subheader('Scientific Name of 114 Birds Species :bird:')
143
+
144
+ with st.container(height=300):
145
+ st.markdown(list(labels_list.values()))
146
+ # birds = pd.DataFrame(class_file)
147
+ # st.table(birds)
148
+ st.header('', divider='rainbow')
149
+
150
+ if audio_file is not None:
151
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
152
+ tmp_file.write(audio_file.read())
153
+ st.success("Audio file successfully uploaded and stored temporally.")
154
+ file_path = tmp_file.name
155
+ audio_data, sampling_rate = librosa.load(file_path)
156
+ st.audio(audio_data, sample_rate=sampling_rate)
157
+ audio_signals(file_path)
158
+ # Predict the class
159
+ y_predict = predict_class(file_path, model)
160
+ # Display predicted class
161
+ if str(y_predict) in labels_list.keys():
162
+ pred = labels_list[str(y_predict)][:-6]
163
+ st.subheader(f'Predicted Class: :rainbow[{pred}]')
164
+ st.image(wikipedia.page(pred).images[0], caption=labels_list[str(y_predict)][:-6], width=200)
165
+ st.markdown(wikipedia.summary(pred))
166
+
167
+ user_input = f"Explain about {pred} bird"
168
+ # Generate and display assistant response
169
+ if user_input:
170
+ response = respond(user_input, messages, max_tokens = 500, temperature = 0.70, top_p = 0.95)
171
+ st.markdown(response)
172
+ messages.append({"role": "assistant", "content": response})
173
+ st.page_link(wikipedia.page(pred).url, label="Explore more in Wikipedia.com", icon="🌎")
174
+ # st.session_state.messages.append({"role": "assistant", "content": response})
175
+ else:
176
+ st.write('Class not Found')
177
+ else:
178
+ st.markdown('File not Found!')
179
+
bird_audio_classification_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:76253985cd575fc581063035f509e85f36b20b1efd950ed48a907f8892c592d2
3
+ size 6231152
classes.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"66": "Ornate Tinamou_sound", "59": "Moluccan Megapode_sound", "76": "Red-throated Piping Guan_sound", "51": "Little Chachalaca_sound", "102": "Undulated Tinamou_sound", "6": "Baudo Guan_sound", "110": "White-crested Guan_sound", "33": "Dusky Megapode_sound", "12": "Black-capped Tinamou_sound", "8": "Berlepschs Tinamou_sound", "50": "Lesser Rhea_sound", "83": "Scaled Chachalaca_sound", "32": "Darwins Nothura_sound", "58": "Micronesian Megapode_sound", "113": "Yellow-legged Tinamou_sound", "84": "Slaty-breasted Tinamou_sound", "7": "Bearded Guan_sound", "96": "Tataupa Tinamou_sound", "45": "Grey-legged Tinamou_sound", "86": "Solitary Tinamou_sound", "13": "Black-fronted Piping Guan_sound", "14": "Blue-throated Piping Guan_sound", "97": "Tawny-breasted Tinamou_sound", "70": "Plain Chachalaca_sound", "71": "Puna Tinamou_sound", "82": "Rusty-margined Guan_sound", "23": "Chestnut-winged Chachalaca_sound", "88": "Southern Brown Kiwi_sound", "104": "Variegated Tinamou_sound", "28": "Colombian Chachalaca_sound", "111": "White-throated Tinamou_sound", "74": "Red-faced Guan_sound", "63": "Northern Cassowary_sound", "57": "Melanesian Megapode_sound", "101": "Trinidad Piping Guan_sound", "49": "Lesser Nothura_sound", "80": "Rufous-vented Chachalaca_sound", "112": "White-winged Guan_sound", "77": "Red-winged Tinamou_sound", "79": "Rufous-headed Chachalaca_sound", "20": "Chaco Chachalaca_sound", "37": "East Brazilian Chachalaca_sound", "64": "Okarito Kiwi_sound", "99": "Thicket Tinamou_sound", "69": "Philippine Megapode_sound", "103": "Vanuatu Megapode_sound", "39": "Emu_sound", "100": "Tongan Megapode_sound", "89": "Southern Cassowary_sound", "19": "Cauca Guan_sound", "94": "Taczanowskis Tinamou_sound", "40": "Great Spotted Kiwi_sound", "109": "White-browed Guan_sound", "56": "Marail Guan_sound", "60": "New Guinea Scrubfowl_sound", "4": "Barred Tinamou_sound", "30": "Crested Guan_sound", "106": "West Mexican Chachalaca_sound", "47": "Hooded Tinamou_sound", "48": "Huayco Tinamou_sound", "98": "Tepui Tinamou_sound", "44": "Grey-headed Chachalaca_sound", "78": "Rufous-bellied Chachalaca_sound", "15": "Brazilian Tinamou_sound", "61": "Nicobar Megapode_sound", "22": "Chestnut-headed Chachalaca_sound", "24": "Chilean Tinamou_sound", "26": "Cinereous Tinamou_sound", "38": "Elegant Crested Tinamou_sound", "27": "Collared Brushturkey_sound", "10": "Black Tinamou_sound", "65": "Orange-footed Scrubfowl_sound", "0": "Andean Guan_sound", "53": "Little Tinamou_sound", "3": "Band-tailed Guan_sound", "54": "Maleo_sound", "29": "Common Ostrich_sound", "107": "White-bellied Chachalaca_sound", "31": "Curve-billed Tinamou_sound", "43": "Grey Tinamou_sound", "5": "Bartletts Tinamou_sound", "95": "Tanimbar Megapode_sound", "87": "Somali Ostrich_sound", "21": "Chestnut-bellied Guan_sound", "16": "Brown Tinamou_sound", "35": "Dwarf Cassowary_sound", "11": "Black-billed Brushturkey_sound", "68": "Patagonian Tinamou_sound", "17": "Brushland Tinamou_sound", "18": "Buff-browed Chachalaca_sound", "42": "Greater Rhea_sound", "93": "Sula Megapode_sound", "91": "Spixs Guan_sound", "81": "Rusty Tinamou_sound", "75": "Red-legged Tinamou_sound", "67": "Pale-browed Tinamou_sound", "2": "Australian Brushturkey_sound", "1": "Andean Tinamou_sound", "55": "Malleefowl_sound", "36": "Dwarf Tinamou_sound", "46": "Highland Tinamou_sound", "85": "Small-billed Tinamou_sound", "25": "Choco Tinamou_sound", "9": "Biak Scrubfowl_sound", "34": "Dusky-legged Guan_sound", "62": "North Island Brown Kiwi_sound", "73": "Red-billed Brushturkey_sound", "105": "Wattled Brushturkey_sound", "108": "White-bellied Nothura_sound", "92": "Spotted Nothura_sound", "52": "Little Spotted Kiwi_sound", "72": "Quebracho Crested Tinamou_sound", "41": "Great Tinamou_sound", "90": "Speckled Chachalaca_sound"}
extracted_features.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:234f3c08bc2031bac5be26d7e65549357a4a304ddb14739f26fb1508144b562d
3
+ size 1728928
logo.PNG ADDED
requirements.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit==1.37.0
2
+ librosa==0.10.1
3
+ joblib==1.3.2
4
+ ipython==8.17.2
5
+ matplotlib==3.7.2
6
+ matplotlib-inline==0.1.6
7
+ scikit-learn==1.5.0
8
+ numpy==1.25.2
9
+ seaborn==0.12.2
10
+ pandas==2.2.0
11
+ tensorflow==2.17.0
12
+ wikipedia==1.4.0
13
+ huggingface-hub==0.26.2
14
+ Wikipedia-API==0.7.1
15
+ python-dotenv==1.0.1
test_data/261514.mp3 ADDED
Binary file (267 kB). View file
 
test_data/276343.mp3 ADDED
Binary file (378 kB). View file
 
test_data/335768.mp3 ADDED
Binary file (208 kB). View file
 
test_data/XC117965 - Spix's Guan - Penelope jacquacu.mp3 ADDED
Binary file (864 kB). View file