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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
from serpapi import GoogleSearch
|
| 6 |
+
from typing import List, Dict, Any
|
| 7 |
+
|
| 8 |
+
from helper import search_serpapi, convert_to_md_table, current_year
|
| 9 |
+
|
| 10 |
+
# API KEY
|
| 11 |
+
SERPAPI_API_KEY = os.environ["SERPAPI_API_KEY"]
|
| 12 |
+
|
| 13 |
+
# Front-end
|
| 14 |
+
st.set_page_config(layout="wide")
|
| 15 |
+
st.title("SearchBot 🤖")
|
| 16 |
+
|
| 17 |
+
# Front-end: Sidebar
|
| 18 |
+
with st.sidebar:
|
| 19 |
+
with st.expander("Instruction Manual"):
|
| 20 |
+
st.markdown("""
|
| 21 |
+
## SearchBot 🤖
|
| 22 |
+
This Streamlit app allows you to search anything.
|
| 23 |
+
|
| 24 |
+
### How to Use:
|
| 25 |
+
1. **Source**: Select your favorite source, i.e. default is Google.
|
| 26 |
+
1. **Num**: Select number of output you want to display, i.e. default is 20.
|
| 27 |
+
1. **Location**: Select your location so content such as weather can be customized, i.e. default is New York.
|
| 28 |
+
2. **Response**: The app will display a response table.
|
| 29 |
+
3. **Chat History**: Previous conversations will be shown on the app and can be cleared using "Clear Session" button.
|
| 30 |
+
|
| 31 |
+
### Credits:
|
| 32 |
+
- **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/)
|
| 33 |
+
|
| 34 |
+
Enjoy chatting with SearchBot!
|
| 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("Example: Taking medicine during pregnancy (you can select WebMD to filter only results from WebMD)")
|
| 42 |
+
|
| 43 |
+
# Input
|
| 44 |
+
src_key_word = st.selectbox(
|
| 45 |
+
"Source of page you prefer?",
|
| 46 |
+
(
|
| 47 |
+
"Google",
|
| 48 |
+
"Yahoo",
|
| 49 |
+
"WebMD",
|
| 50 |
+
"BetterHelp",
|
| 51 |
+
"Zocdoc",
|
| 52 |
+
"Clevelandclinic",
|
| 53 |
+
"Drugs.com",
|
| 54 |
+
"EMedicineHealth.com",
|
| 55 |
+
"Reddit",
|
| 56 |
+
)
|
| 57 |
+
)
|
| 58 |
+
src_key_word = "" if src_key_word == "Google" else src_key_word
|
| 59 |
+
num = st.number_input("Insert a number", value=20, step=1, placeholder="Type a number...")
|
| 60 |
+
location = st.selectbox(
|
| 61 |
+
"Where would you like to search from?",
|
| 62 |
+
(
|
| 63 |
+
"New York, New York, United States",
|
| 64 |
+
"Los Angeles, California, United States",
|
| 65 |
+
"Chicago, Illinois, United States",
|
| 66 |
+
"Houston, Texas, United States",
|
| 67 |
+
"Phoenix, Arizona, United States",
|
| 68 |
+
"Philadelphia, Pennsylvania, United States",
|
| 69 |
+
"San Antonio, Texas, United States",
|
| 70 |
+
"San Diego, California, United States",
|
| 71 |
+
"Dallas, Texas, United States",
|
| 72 |
+
"San Jose, California, United States",
|
| 73 |
+
"Austin, Texas, United States",
|
| 74 |
+
"Jacksonville, Florida, United States",
|
| 75 |
+
"Fort Worth, Texas, United States",
|
| 76 |
+
"Columbus, Ohio, United States",
|
| 77 |
+
"Charlotte, North Carolina, United States",
|
| 78 |
+
"San Francisco, California, United States",
|
| 79 |
+
"Indianapolis, Indiana, United States",
|
| 80 |
+
"Seattle, Washington, United States",
|
| 81 |
+
"Denver, Colorado, United States",
|
| 82 |
+
"Washington, D.C., United States",
|
| 83 |
+
"Boston, Massachusetts, United States",
|
| 84 |
+
"El Paso, Texas, United States",
|
| 85 |
+
"Nashville, Tennessee, United States",
|
| 86 |
+
"Detroit, Michigan, United States",
|
| 87 |
+
"Oklahoma City, Oklahoma, United States",
|
| 88 |
+
"Portland, Oregon, United States",
|
| 89 |
+
"Las Vegas, Nevada, United States",
|
| 90 |
+
"Memphis, Tennessee, United States",
|
| 91 |
+
"Louisville, Kentucky, United States",
|
| 92 |
+
"Baltimore, Maryland, United States",
|
| 93 |
+
"Milwaukee, Wisconsin, United States",
|
| 94 |
+
"Albuquerque, New Mexico, United States",
|
| 95 |
+
"Tucson, Arizona, United States",
|
| 96 |
+
"Fresno, California, United States",
|
| 97 |
+
"Sacramento, California, United States",
|
| 98 |
+
"Mesa, Arizona, United States",
|
| 99 |
+
"Kansas City, Missouri, United States",
|
| 100 |
+
"Atlanta, Georgia, United States",
|
| 101 |
+
"Omaha, Nebraska, United States",
|
| 102 |
+
"Colorado Springs, Colorado, United States"
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Add a button to clear the session state
|
| 107 |
+
if st.button("Clear Session"):
|
| 108 |
+
st.session_state.messages = []
|
| 109 |
+
st.experimental_rerun()
|
| 110 |
+
|
| 111 |
+
# Credit:
|
| 112 |
+
current_year = current_year() # This will print the current year
|
| 113 |
+
st.markdown(
|
| 114 |
+
f"""
|
| 115 |
+
<h6 style='text-align: left;'>Copyright © 2010-{current_year} Present Yiqiao Yin</h6>
|
| 116 |
+
""",
|
| 117 |
+
unsafe_allow_html=True,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# Initialize chat history
|
| 122 |
+
if "messages" not in st.session_state:
|
| 123 |
+
st.session_state.messages = []
|
| 124 |
+
|
| 125 |
+
# Ensure messages are a list of dictionaries
|
| 126 |
+
if not isinstance(st.session_state.messages, list):
|
| 127 |
+
st.session_state.messages = []
|
| 128 |
+
if not all(isinstance(msg, dict) for msg in st.session_state.messages):
|
| 129 |
+
st.session_state.messages = []
|
| 130 |
+
|
| 131 |
+
# Display chat messages from history on app rerun
|
| 132 |
+
for message in st.session_state.messages:
|
| 133 |
+
with st.chat_message(message["role"]):
|
| 134 |
+
st.markdown(message["content"])
|
| 135 |
+
|
| 136 |
+
# Front-end: Chat Panel - React to user input
|
| 137 |
+
if prompt := st.chat_input("😉 Ask any question or feel free to use the examples provided in the left sidebar."):
|
| 138 |
+
|
| 139 |
+
# Display user message in chat message container
|
| 140 |
+
st.chat_message("user").markdown(prompt)
|
| 141 |
+
|
| 142 |
+
# Add user message to chat history
|
| 143 |
+
st.session_state.messages.append({"role": "system", "content": f"You are a helpful assistant. Year now is {current_year}"})
|
| 144 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 145 |
+
|
| 146 |
+
# API Call
|
| 147 |
+
query = src_key_word + ' ' + prompt
|
| 148 |
+
try:
|
| 149 |
+
results = search_serpapi(query, location, SERPAPI_API_KEY, num)
|
| 150 |
+
list_of_links = []
|
| 151 |
+
for result in results:
|
| 152 |
+
list_of_links.append({
|
| 153 |
+
"title": result['title'],
|
| 154 |
+
"link": result['link'],
|
| 155 |
+
"snippet": result['snippet']
|
| 156 |
+
})
|
| 157 |
+
md_data = convert_to_md_table(list_of_links)
|
| 158 |
+
response = f"""
|
| 159 |
+
Please see search results below: \n{md_data}
|
| 160 |
+
"""
|
| 161 |
+
except Exception as e:
|
| 162 |
+
st.warning(f"Failed to fetch data: {e}")
|
| 163 |
+
response = "We are fixing the API right now. Please check back later."
|
| 164 |
+
|
| 165 |
+
# Display assistant response in chat message container
|
| 166 |
+
with st.chat_message("assistant"):
|
| 167 |
+
st.markdown(response, unsafe_allow_html=True)
|
| 168 |
+
|
| 169 |
+
# Add assistant response to chat history
|
| 170 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|