audio added
Browse files- app.py +15 -17
- helper.py +17 -0
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from datetime import datetime
|
|
| 3 |
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
from helper import ChatBot, current_year, invoke_search_api
|
| 7 |
|
| 8 |
# API KEY
|
| 9 |
API_KEY = os.environ["API_KEY"]
|
|
@@ -75,7 +75,6 @@ with st.sidebar:
|
|
| 75 |
(
|
| 76 |
"New York, New York, United States",
|
| 77 |
"Los Angeles, California, United States",
|
| 78 |
-
"Seattle, Washington, United States",
|
| 79 |
"Chicago, Illinois, United States",
|
| 80 |
"Houston, Texas, United States",
|
| 81 |
"Phoenix, Arizona, United States",
|
|
@@ -91,6 +90,7 @@ with st.sidebar:
|
|
| 91 |
"Charlotte, North Carolina, United States",
|
| 92 |
"San Francisco, California, United States",
|
| 93 |
"Indianapolis, Indiana, United States",
|
|
|
|
| 94 |
"Denver, Colorado, United States",
|
| 95 |
"Washington, D.C., United States",
|
| 96 |
"Boston, Massachusetts, United States",
|
|
@@ -115,7 +115,6 @@ with st.sidebar:
|
|
| 115 |
"Colorado Springs, Colorado, United States",
|
| 116 |
),
|
| 117 |
)
|
| 118 |
-
do_not_use_internet = st.checkbox("Do not user internet. Chat only.")
|
| 119 |
|
| 120 |
# Add a button to clear the session state
|
| 121 |
if st.button("Clear Session"):
|
|
@@ -166,20 +165,14 @@ if prompt := st.chat_input(
|
|
| 166 |
# API Call
|
| 167 |
query = src_key_word + " " + prompt
|
| 168 |
try:
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
)
|
| 178 |
-
md_data = md_data["data"]
|
| 179 |
-
response = f"""
|
| 180 |
-
Please see search results below: \n{md_data}
|
| 181 |
-
"""
|
| 182 |
-
ref_table_string = response
|
| 183 |
|
| 184 |
# API Call
|
| 185 |
bot = ChatBot()
|
|
@@ -196,9 +189,14 @@ if prompt := st.chat_input(
|
|
| 196 |
st.warning(f"Failed to fetch data: {e}")
|
| 197 |
response = "We are fixing the API right now. Please check back later."
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Display assistant response in chat message container
|
| 200 |
with st.chat_message("assistant"):
|
| 201 |
st.markdown(response, unsafe_allow_html=True)
|
|
|
|
| 202 |
with st.expander("See references:", expanded=False):
|
| 203 |
st.markdown(ref_table_string)
|
| 204 |
|
|
|
|
| 3 |
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
from helper import ChatBot, current_year, invoke_search_api, save_to_audio
|
| 7 |
|
| 8 |
# API KEY
|
| 9 |
API_KEY = os.environ["API_KEY"]
|
|
|
|
| 75 |
(
|
| 76 |
"New York, New York, United States",
|
| 77 |
"Los Angeles, California, United States",
|
|
|
|
| 78 |
"Chicago, Illinois, United States",
|
| 79 |
"Houston, Texas, United States",
|
| 80 |
"Phoenix, Arizona, United States",
|
|
|
|
| 90 |
"Charlotte, North Carolina, United States",
|
| 91 |
"San Francisco, California, United States",
|
| 92 |
"Indianapolis, Indiana, United States",
|
| 93 |
+
"Seattle, Washington, United States",
|
| 94 |
"Denver, Colorado, United States",
|
| 95 |
"Washington, D.C., United States",
|
| 96 |
"Boston, Massachusetts, United States",
|
|
|
|
| 115 |
"Colorado Springs, Colorado, United States",
|
| 116 |
),
|
| 117 |
)
|
|
|
|
| 118 |
|
| 119 |
# Add a button to clear the session state
|
| 120 |
if st.button("Clear Session"):
|
|
|
|
| 165 |
# API Call
|
| 166 |
query = src_key_word + " " + prompt
|
| 167 |
try:
|
| 168 |
+
md_data = invoke_search_api(
|
| 169 |
+
api_key=API_KEY, query=query, location=location, num=num
|
| 170 |
+
)
|
| 171 |
+
md_data = md_data["data"]
|
| 172 |
+
response = f"""
|
| 173 |
+
Please see search results below: \n{md_data}
|
| 174 |
+
"""
|
| 175 |
+
ref_table_string = response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# API Call
|
| 178 |
bot = ChatBot()
|
|
|
|
| 189 |
st.warning(f"Failed to fetch data: {e}")
|
| 190 |
response = "We are fixing the API right now. Please check back later."
|
| 191 |
|
| 192 |
+
# Save audio
|
| 193 |
+
if response:
|
| 194 |
+
save_to_audio(response)
|
| 195 |
+
|
| 196 |
# Display assistant response in chat message container
|
| 197 |
with st.chat_message("assistant"):
|
| 198 |
st.markdown(response, unsafe_allow_html=True)
|
| 199 |
+
st.audio("output.mp3", format="audio/mpeg", loop=True)
|
| 200 |
with st.expander("See references:", expanded=False):
|
| 201 |
st.markdown(ref_table_string)
|
| 202 |
|
helper.py
CHANGED
|
@@ -6,6 +6,7 @@ from typing import Dict, List
|
|
| 6 |
import requests
|
| 7 |
import streamlit as st
|
| 8 |
from openai import OpenAI
|
|
|
|
| 9 |
|
| 10 |
api_key = os.environ["API_KEY"]
|
| 11 |
|
|
@@ -69,6 +70,22 @@ def current_year() -> int:
|
|
| 69 |
# Extract and return the current year
|
| 70 |
return now.year
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
class ChatBot:
|
| 74 |
"""
|
|
|
|
| 6 |
import requests
|
| 7 |
import streamlit as st
|
| 8 |
from openai import OpenAI
|
| 9 |
+
from gtts import gTTS
|
| 10 |
|
| 11 |
api_key = os.environ["API_KEY"]
|
| 12 |
|
|
|
|
| 70 |
# Extract and return the current year
|
| 71 |
return now.year
|
| 72 |
|
| 73 |
+
# Save audio
|
| 74 |
+
def save_to_audio(text: str) -> None:
|
| 75 |
+
"""
|
| 76 |
+
Converts the given text to an audio file.
|
| 77 |
+
|
| 78 |
+
This function uses Google Text-to-Speech (gTTS) to convert the provided
|
| 79 |
+
text into speech and saves it as an MP3 file named 'output.mp3'.
|
| 80 |
+
|
| 81 |
+
Parameters:
|
| 82 |
+
text (str): The text to be converted to speech.
|
| 83 |
+
"""
|
| 84 |
+
# Create a gTTS object with the specified text and language ('en' for English)
|
| 85 |
+
tts = gTTS(text=text, lang='en') # 'en' for English, change as needed
|
| 86 |
+
|
| 87 |
+
# Save the generated speech to an audio file named "output.mp3"
|
| 88 |
+
tts.save("output.mp3")
|
| 89 |
|
| 90 |
class ChatBot:
|
| 91 |
"""
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
google-search-results
|
| 2 |
openai
|
| 3 |
streamlit
|
| 4 |
-
serpapi
|
|
|
|
|
|
| 1 |
google-search-results
|
| 2 |
openai
|
| 3 |
streamlit
|
| 4 |
+
serpapi
|
| 5 |
+
gTTS
|