Spaces:
Build error
Build error
Nafise
commited on
Commit
·
71ede40
1
Parent(s):
fc5820c
Streamlit
Browse files- app.py +111 -0
- requirements.txt +14 -0
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import speech_recognition as sr
|
4 |
+
import pyttsx3
|
5 |
+
|
6 |
+
|
7 |
+
st.title("Music Chatbot")
|
8 |
+
|
9 |
+
# Function to send user input to backend and receive response
|
10 |
+
def get_bot_response(user_input):
|
11 |
+
response = requests.post("http://127.0.0.1:8000/get_response", json={"dialog": [user_input]})
|
12 |
+
if response.ok:
|
13 |
+
return response.json()
|
14 |
+
return None
|
15 |
+
|
16 |
+
# Function to perform speech-to-text conversion
|
17 |
+
def transcribe_audio():
|
18 |
+
recognizer = sr.Recognizer()
|
19 |
+
engine = pyttsx3.init()
|
20 |
+
engine.setProperty('rate', 150)
|
21 |
+
|
22 |
+
with sr.Microphone() as mic:
|
23 |
+
recognizer.adjust_for_ambient_noise(mic)
|
24 |
+
# st.write("Recording...")
|
25 |
+
audio = recognizer.listen(mic)
|
26 |
+
|
27 |
+
try:
|
28 |
+
text = recognizer.recognize_google(audio)
|
29 |
+
st.write(f"Transcription: {text}")
|
30 |
+
print(f"Text: {text}")
|
31 |
+
return text
|
32 |
+
except sr.UnknownValueError:
|
33 |
+
st.write("Could not understand audio")
|
34 |
+
return None
|
35 |
+
except sr.RequestError as e:
|
36 |
+
st.write(f"Error: {e}")
|
37 |
+
return None
|
38 |
+
|
39 |
+
# Sidebar for user input options
|
40 |
+
input_option = st.sidebar.selectbox("Select input method:", ("Text", "Audio"))
|
41 |
+
|
42 |
+
if input_option == "Text":
|
43 |
+
# Initialize chat messages if not already initialized
|
44 |
+
if "chat_messages" not in st.session_state:
|
45 |
+
st.session_state.chat_messages = []
|
46 |
+
|
47 |
+
# Input field for user text input
|
48 |
+
user_input = st.sidebar.text_input("You:", "")
|
49 |
+
|
50 |
+
# Button to send user message
|
51 |
+
if st.sidebar.button("Send"):
|
52 |
+
# Get user input
|
53 |
+
user_message = {'actor': 'user', 'payload': user_input, 'timestamp': len(st.session_state.chat_messages)}
|
54 |
+
st.session_state.chat_messages.append(user_message)
|
55 |
+
|
56 |
+
# Get bot response
|
57 |
+
bot_response = get_bot_response(user_input)
|
58 |
+
|
59 |
+
if bot_response:
|
60 |
+
# Append bot response to chat history
|
61 |
+
bot_message = {'actor': 'bot', 'payload': bot_response['generated_response'], 'timestamp': len(st.session_state.chat_messages)}
|
62 |
+
st.session_state.chat_messages.append(bot_message)
|
63 |
+
|
64 |
+
# Display chat history
|
65 |
+
for message in st.session_state.chat_messages:
|
66 |
+
if message['actor'] == 'user':
|
67 |
+
st.text_input("You:", value=message['payload'], key=message['timestamp'], disabled=True)
|
68 |
+
elif message['actor'] == 'bot':
|
69 |
+
st.text_area("Bot:", value=message['payload'], key=message['timestamp'], disabled=True)
|
70 |
+
|
71 |
+
# songs recommendations
|
72 |
+
st.write("Music Recommendations:")
|
73 |
+
for rec in bot_response["recommendations"]:
|
74 |
+
st.write(f"- {rec['name']} by {rec['artist']['name']}")
|
75 |
+
listen_url = rec['url']
|
76 |
+
st.markdown(f"[Listen]({listen_url})")
|
77 |
+
|
78 |
+
elif input_option == "Audio":
|
79 |
+
# Button to record audio
|
80 |
+
recording = st.sidebar.button("Record")
|
81 |
+
|
82 |
+
if recording:
|
83 |
+
with st.spinner("Recording..."):
|
84 |
+
stop_recording = st.sidebar.button("Cancel")
|
85 |
+
if stop_recording:
|
86 |
+
st.write("Recording stopped.")
|
87 |
+
else:
|
88 |
+
transcribed_text = transcribe_audio()
|
89 |
+
if transcribed_text:
|
90 |
+
bot_response = get_bot_response(transcribed_text)
|
91 |
+
if bot_response:
|
92 |
+
# Append bot response to chat history
|
93 |
+
bot_message = {'actor': 'bot', 'payload': bot_response['generated_response'], 'timestamp': len(st.session_state.chat_messages)}
|
94 |
+
st.session_state.chat_messages.append(bot_message)
|
95 |
+
|
96 |
+
# Display chat history
|
97 |
+
for message in st.session_state.chat_messages:
|
98 |
+
if message['actor'] == 'user':
|
99 |
+
st.text_input("You:", value=message['payload'], key=message['timestamp'], disabled=True)
|
100 |
+
elif message['actor'] == 'bot':
|
101 |
+
st.text_area("Bot:", value=message['payload'], key=message['timestamp'], disabled=True)
|
102 |
+
|
103 |
+
# Display music recommendations
|
104 |
+
st.write("Music Recommendations:")
|
105 |
+
for rec in bot_response["recommendations"]:
|
106 |
+
st.write(f"- {rec['name']} by {rec['artist']['name']}")
|
107 |
+
listen_url = rec['url']
|
108 |
+
st.markdown(f"[Listen]({listen_url})")
|
109 |
+
|
110 |
+
else:
|
111 |
+
st.write("Click 'Record' to start recording.")
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers[sentencepiece]
|
2 |
+
flask~=2.2.2
|
3 |
+
flask-restful
|
4 |
+
flask-cors
|
5 |
+
streamlit
|
6 |
+
requests
|
7 |
+
torch
|
8 |
+
torchvision
|
9 |
+
torchaudio
|
10 |
+
pyttsx3
|
11 |
+
pyaudio
|
12 |
+
speechrecognition
|
13 |
+
setuptools
|
14 |
+
influxdb-client
|