yyin0 commited on
Commit ·
374a607
1
Parent(s): d9e8c5a
code cleaned up
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
|
|
| 1 |
from datetime import datetime
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
-
import os
|
| 5 |
-
from typing import List, Dict, Any
|
| 6 |
|
| 7 |
from helper import current_year, invoke_search_api
|
| 8 |
|
|
@@ -16,7 +15,8 @@ st.title("SearchBot 🤖")
|
|
| 16 |
# Front-end: Sidebar
|
| 17 |
with st.sidebar:
|
| 18 |
with st.expander("Instruction Manual"):
|
| 19 |
-
st.markdown(
|
|
|
|
| 20 |
## SearchBot 🤖
|
| 21 |
This Streamlit app allows you to search anything.
|
| 22 |
|
|
@@ -31,13 +31,16 @@ with st.sidebar:
|
|
| 31 |
- **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/serpapi-demo) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
|
| 32 |
|
| 33 |
Enjoy chatting with SearchBot!
|
| 34 |
-
"""
|
|
|
|
| 35 |
|
| 36 |
# Example:
|
| 37 |
st.success("Example: Yiqiao Yin.")
|
| 38 |
st.success("Example: Weather in Westchester, NY today")
|
| 39 |
st.success("Example: Latest news about Tesla")
|
| 40 |
-
st.success(
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Input
|
| 43 |
src_key_word = st.selectbox(
|
|
@@ -52,18 +55,20 @@ with st.sidebar:
|
|
| 52 |
"NYTimes",
|
| 53 |
"Economist",
|
| 54 |
"WebMD",
|
| 55 |
-
"BetterHelp",
|
| 56 |
"Zocdoc",
|
| 57 |
"Clevelandclinic",
|
| 58 |
"Drugs.com",
|
| 59 |
"EMedicineHealth.com",
|
| 60 |
"Reddit",
|
| 61 |
-
)
|
| 62 |
)
|
| 63 |
src_key_word = "" if src_key_word == "Google" else src_key_word
|
| 64 |
-
num = st.number_input(
|
|
|
|
|
|
|
| 65 |
location = st.selectbox(
|
| 66 |
-
|
| 67 |
(
|
| 68 |
"New York, New York, United States",
|
| 69 |
"Los Angeles, California, United States",
|
|
@@ -104,8 +109,8 @@ with st.sidebar:
|
|
| 104 |
"Kansas City, Missouri, United States",
|
| 105 |
"Atlanta, Georgia, United States",
|
| 106 |
"Omaha, Nebraska, United States",
|
| 107 |
-
"Colorado Springs, Colorado, United States"
|
| 108 |
-
)
|
| 109 |
)
|
| 110 |
|
| 111 |
# Add a button to clear the session state
|
|
@@ -139,19 +144,27 @@ for message in st.session_state.messages:
|
|
| 139 |
st.markdown(message["content"])
|
| 140 |
|
| 141 |
# Front-end: Chat Panel - React to user input
|
| 142 |
-
if prompt := st.chat_input(
|
| 143 |
-
|
|
|
|
| 144 |
# Display user message in chat message container
|
| 145 |
st.chat_message("user").markdown(prompt)
|
| 146 |
|
| 147 |
# Add user message to chat history
|
| 148 |
-
st.session_state.messages.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 150 |
|
| 151 |
# API Call
|
| 152 |
-
query = src_key_word +
|
| 153 |
try:
|
| 154 |
-
md_data = invoke_search_api(
|
|
|
|
|
|
|
| 155 |
md_data = md_data["data"]
|
| 156 |
response = f"""
|
| 157 |
Please see search results below: \n{md_data}
|
|
@@ -165,4 +178,4 @@ if prompt := st.chat_input("😉 Ask any question or feel free to use the exampl
|
|
| 165 |
st.markdown(response, unsafe_allow_html=True)
|
| 166 |
|
| 167 |
# Add assistant response to chat history
|
| 168 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
| 1 |
+
import os
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from helper import current_year, invoke_search_api
|
| 7 |
|
|
|
|
| 15 |
# Front-end: Sidebar
|
| 16 |
with st.sidebar:
|
| 17 |
with st.expander("Instruction Manual"):
|
| 18 |
+
st.markdown(
|
| 19 |
+
"""
|
| 20 |
## SearchBot 🤖
|
| 21 |
This Streamlit app allows you to search anything.
|
| 22 |
|
|
|
|
| 31 |
- **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/serpapi-demo) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
|
| 32 |
|
| 33 |
Enjoy chatting with SearchBot!
|
| 34 |
+
"""
|
| 35 |
+
)
|
| 36 |
|
| 37 |
# Example:
|
| 38 |
st.success("Example: Yiqiao Yin.")
|
| 39 |
st.success("Example: Weather in Westchester, NY today")
|
| 40 |
st.success("Example: Latest news about Tesla")
|
| 41 |
+
st.success(
|
| 42 |
+
"Example: Taking medicine during pregnancy (you can select WebMD to filter only results from WebMD)"
|
| 43 |
+
)
|
| 44 |
|
| 45 |
# Input
|
| 46 |
src_key_word = st.selectbox(
|
|
|
|
| 55 |
"NYTimes",
|
| 56 |
"Economist",
|
| 57 |
"WebMD",
|
| 58 |
+
"BetterHelp",
|
| 59 |
"Zocdoc",
|
| 60 |
"Clevelandclinic",
|
| 61 |
"Drugs.com",
|
| 62 |
"EMedicineHealth.com",
|
| 63 |
"Reddit",
|
| 64 |
+
),
|
| 65 |
)
|
| 66 |
src_key_word = "" if src_key_word == "Google" else src_key_word
|
| 67 |
+
num = st.number_input(
|
| 68 |
+
"Insert a number", value=20, step=1, placeholder="Type a number..."
|
| 69 |
+
)
|
| 70 |
location = st.selectbox(
|
| 71 |
+
"Where would you like to search from?",
|
| 72 |
(
|
| 73 |
"New York, New York, United States",
|
| 74 |
"Los Angeles, California, United States",
|
|
|
|
| 109 |
"Kansas City, Missouri, United States",
|
| 110 |
"Atlanta, Georgia, United States",
|
| 111 |
"Omaha, Nebraska, United States",
|
| 112 |
+
"Colorado Springs, Colorado, United States",
|
| 113 |
+
),
|
| 114 |
)
|
| 115 |
|
| 116 |
# Add a button to clear the session state
|
|
|
|
| 144 |
st.markdown(message["content"])
|
| 145 |
|
| 146 |
# Front-end: Chat Panel - React to user input
|
| 147 |
+
if prompt := st.chat_input(
|
| 148 |
+
"😉 Ask any question or feel free to use the examples provided in the left sidebar."
|
| 149 |
+
):
|
| 150 |
# Display user message in chat message container
|
| 151 |
st.chat_message("user").markdown(prompt)
|
| 152 |
|
| 153 |
# Add user message to chat history
|
| 154 |
+
st.session_state.messages.append(
|
| 155 |
+
{
|
| 156 |
+
"role": "system",
|
| 157 |
+
"content": f"You are a helpful assistant. Year now is {current_year}",
|
| 158 |
+
}
|
| 159 |
+
)
|
| 160 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 161 |
|
| 162 |
# API Call
|
| 163 |
+
query = src_key_word + " " + prompt
|
| 164 |
try:
|
| 165 |
+
md_data = invoke_search_api(
|
| 166 |
+
api_key=API_KEY, query=query, location=location, num=num
|
| 167 |
+
)
|
| 168 |
md_data = md_data["data"]
|
| 169 |
response = f"""
|
| 170 |
Please see search results below: \n{md_data}
|
|
|
|
| 178 |
st.markdown(response, unsafe_allow_html=True)
|
| 179 |
|
| 180 |
# Add assistant response to chat history
|
| 181 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
helper.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
|
|
|
|
|
| 1 |
from datetime import datetime
|
|
|
|
| 2 |
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import os
|
| 5 |
-
from typing import List, Dict, Any
|
| 6 |
import requests
|
| 7 |
-
import
|
| 8 |
|
| 9 |
api_key = os.environ["API_KEY"]
|
| 10 |
|
| 11 |
|
| 12 |
def invoke_search_api(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
) -> Dict[str, any]:
|
| 18 |
"""
|
| 19 |
Invoke the Vitanexus SearchBot API to retrieve search results based on a query and location.
|
|
@@ -25,30 +25,26 @@ def invoke_search_api(
|
|
| 25 |
num (int, optional): The number of search results to retrieve. Default is 10.
|
| 26 |
|
| 27 |
Returns:
|
| 28 |
-
Dict[str, any]: A dictionary containing the JSON response from the API.
|
| 29 |
If an error occurs, returns a dictionary with error details.
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
# Construct the API endpoint URL with the provided API key
|
| 33 |
url = f"https://vitanexus-searchbot-v2.azurewebsites.net/api/http_trigger?code={api_key}"
|
| 34 |
-
|
| 35 |
# Create the JSON payload for the POST request
|
| 36 |
-
payload = {
|
| 37 |
-
|
| 38 |
-
"location": location,
|
| 39 |
-
"num": num
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
try:
|
| 43 |
# Send a POST request to the API endpoint with the JSON payload
|
| 44 |
response = requests.post(url, json=payload)
|
| 45 |
-
|
| 46 |
# Raise an exception for any HTTP error responses
|
| 47 |
response.raise_for_status()
|
| 48 |
-
|
| 49 |
# Return the parsed JSON response if the request is successful
|
| 50 |
return response.json()
|
| 51 |
-
|
| 52 |
except requests.exceptions.RequestException as e:
|
| 53 |
# Handle any request exceptions and return an error message
|
| 54 |
print(f"An error occurred: {e}")
|
|
@@ -60,7 +56,7 @@ def current_year() -> int:
|
|
| 60 |
"""
|
| 61 |
Returns the current year as an integer.
|
| 62 |
|
| 63 |
-
This function fetches the current date and time, then extracts and returns
|
| 64 |
the year from the current date.
|
| 65 |
|
| 66 |
Returns:
|
|
@@ -68,6 +64,6 @@ def current_year() -> int:
|
|
| 68 |
"""
|
| 69 |
# Get the current date and time
|
| 70 |
now: datetime = datetime.now()
|
| 71 |
-
|
| 72 |
# Extract and return the current year
|
| 73 |
return now.year
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
from datetime import datetime
|
| 4 |
+
from typing import Any, Dict, List
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
import requests
|
| 7 |
+
import streamlit as st
|
| 8 |
|
| 9 |
api_key = os.environ["API_KEY"]
|
| 10 |
|
| 11 |
|
| 12 |
def invoke_search_api(
|
| 13 |
+
api_key: str,
|
| 14 |
+
query: str,
|
| 15 |
+
location: str = "New York, New York, United States",
|
| 16 |
+
num: int = 10,
|
| 17 |
) -> Dict[str, any]:
|
| 18 |
"""
|
| 19 |
Invoke the Vitanexus SearchBot API to retrieve search results based on a query and location.
|
|
|
|
| 25 |
num (int, optional): The number of search results to retrieve. Default is 10.
|
| 26 |
|
| 27 |
Returns:
|
| 28 |
+
Dict[str, any]: A dictionary containing the JSON response from the API.
|
| 29 |
If an error occurs, returns a dictionary with error details.
|
| 30 |
"""
|
| 31 |
+
|
| 32 |
# Construct the API endpoint URL with the provided API key
|
| 33 |
url = f"https://vitanexus-searchbot-v2.azurewebsites.net/api/http_trigger?code={api_key}"
|
| 34 |
+
|
| 35 |
# Create the JSON payload for the POST request
|
| 36 |
+
payload = {"query": query, "location": location, "num": num}
|
| 37 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
# Send a POST request to the API endpoint with the JSON payload
|
| 40 |
response = requests.post(url, json=payload)
|
| 41 |
+
|
| 42 |
# Raise an exception for any HTTP error responses
|
| 43 |
response.raise_for_status()
|
| 44 |
+
|
| 45 |
# Return the parsed JSON response if the request is successful
|
| 46 |
return response.json()
|
| 47 |
+
|
| 48 |
except requests.exceptions.RequestException as e:
|
| 49 |
# Handle any request exceptions and return an error message
|
| 50 |
print(f"An error occurred: {e}")
|
|
|
|
| 56 |
"""
|
| 57 |
Returns the current year as an integer.
|
| 58 |
|
| 59 |
+
This function fetches the current date and time, then extracts and returns
|
| 60 |
the year from the current date.
|
| 61 |
|
| 62 |
Returns:
|
|
|
|
| 64 |
"""
|
| 65 |
# Get the current date and time
|
| 66 |
now: datetime = datetime.now()
|
| 67 |
+
|
| 68 |
# Extract and return the current year
|
| 69 |
return now.year
|