MMQP / google.py
gskdsrikrishna's picture
Update google.py
bf88000 verified
import streamlit as st
import requests
import speech_recognition as sr
import pandas as pd
import altair as alt
from PIL import Image
from io import BytesIO
# Ensure set_page_config is the first Streamlit command
st.set_page_config(page_title="Google Search App", layout="wide")
# Function to perform Google Search
def google_search(api_key, cse_id, query, num_results=10):
url = "https://www.googleapis.com/customsearch/v1"
params = {'key': api_key, 'cx': cse_id, 'q': query, 'num': num_results}
response = requests.get(url, params=params)
return response.json()
# Initialize search history and data storage for analytics
if 'search_history' not in st.session_state:
st.session_state.search_history = []
if 'search_data' not in st.session_state:
st.session_state.search_data = pd.DataFrame(columns=["Query", "Source", "Timestamp"])
def main():
st.title("Enhanced Google Search Application")
# User inputs for API key, CSE ID, and search query
api_key = st.secrets["GOOGLE_API_KEY"] # Use Streamlit secrets for security
cse_id = st.secrets["CSE_ID"]
query = st.text_input("Enter your search query", "", key='query_input')
# Voice search feature
if st.button("Use Voice Search"):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Listening...")
audio = recognizer.listen(source)
try:
query = recognizer.recognize_google(audio)
st.write(f"You said: {query}")
if api_key and cse_id and query:
results = google_search(api_key, cse_id, query)
update_search_history(query, "Voice")
display_results(results)
except sr.UnknownValueError:
st.error("Could not understand audio.")
except sr.RequestError:
st.error("Could not request results from Google.")
# Trigger search when clicking the search button
if st.button("Search") and query:
if api_key and cse_id:
results = google_search(api_key, cse_id, query)
update_search_history(query, "Text")
display_results(results)
else:
st.error("Please enter API Key, CSE ID, and a search query.")
# Show search history
if st.button("Show Search History"):
if st.session_state.search_history:
st.write("Search History:")
for h in st.session_state.search_history:
st.write(h)
else:
st.write("No search history found.")
# Clear search history
if st.button("Clear Search History"):
st.session_state.search_history.clear()
st.session_state.search_data = pd.DataFrame(columns=["Query", "Source", "Timestamp"])
st.success("Search history cleared.")
# Interactive Analytics Dashboard
st.subheader("Search Analytics")
if not st.session_state.search_data.empty:
search_trends = alt.Chart(st.session_state.search_data).mark_line().encode(
x='Timestamp:T',
y='count():Q',
color='Source:N',
tooltip=['Query:N', 'count():Q', 'Source:N']
).properties(width=600, height=300)
st.altair_chart(search_trends, use_container_width=True)
# Most popular queries
st.write("**Top Search Queries**")
top_queries = (
st.session_state.search_data['Query']
.value_counts()
.head(5)
.reset_index()
.rename(columns={'index': 'Query', 'Query': 'Count'})
)
st.write(top_queries)
def display_results(results):
if results and 'items' in results:
for i, item in enumerate(results['items']):
st.write(f"**{i + 1}. {item['title']}**")
st.write(f"[Link]({item['link']})")
st.write(f"{item['snippet']}\n")
# Display image if available
if 'pagemap' in item and 'cse_image' in item['pagemap']:
image_data = item['pagemap']['cse_image'][0]
image_url = image_data.get('src')
if image_url:
try:
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
st.image(img, width=100)
except Exception:
st.write("**Image could not be loaded.**")
else:
st.write("**Image source not available.**")
else:
st.write("No image available for this result.")
else:
st.write("No results found.")
def update_search_history(query, source):
st.session_state.search_history.append(query)
new_data = pd.DataFrame({
"Query": [query],
"Source": [source],
"Timestamp": [pd.Timestamp.now()]
})
st.session_state.search_data = pd.concat([st.session_state.search_data, new_data], ignore_index=True)
if __name__ == "__main__":
main()