Spaces:
Running
Running
Update google.py
Browse files
google.py
CHANGED
@@ -1,136 +1,136 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import speech_recognition as sr
|
6 |
-
import pandas as pd
|
7 |
-
import altair as alt
|
8 |
-
from PIL import Image
|
9 |
-
from io import BytesIO
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
st.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
st.write("
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
st.
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
.
|
93 |
-
.
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
st.write(f"{item['
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
st.session_state.search_history.append(query)
|
128 |
-
new_data = pd.DataFrame({
|
129 |
-
"Query": [query],
|
130 |
-
"Source": [source],
|
131 |
-
"Timestamp": [pd.Timestamp.now()]
|
132 |
-
})
|
133 |
-
st.session_state.search_data = pd.concat([st.session_state.search_data, new_data], ignore_index=True)
|
134 |
-
|
135 |
-
if __name__ == "__main__":
|
136 |
-
main()
|
|
|
1 |
+
api_key = "AIzaSyBvnTpjwspsYBMlHN4nMEvybEmZL8mwAQ4"
|
2 |
+
cse_id = "464947c4e602c4ee8"
|
3 |
+
import streamlit as st
|
4 |
+
import requests
|
5 |
+
import speech_recognition as sr
|
6 |
+
import pandas as pd
|
7 |
+
import altair as alt
|
8 |
+
from PIL import Image
|
9 |
+
from io import BytesIO
|
10 |
+
|
11 |
+
# Ensure set_page_config is the first Streamlit command
|
12 |
+
st.set_page_config(page_title="Google Search App", layout="wide")
|
13 |
+
|
14 |
+
# Function to perform Google Search
|
15 |
+
def google_search(api_key, cse_id, query, num_results=10):
|
16 |
+
url = "https://www.googleapis.com/customsearch/v1"
|
17 |
+
params = {'key': api_key, 'cx': cse_id, 'q': query, 'num': num_results}
|
18 |
+
response = requests.get(url, params=params)
|
19 |
+
return response.json()
|
20 |
+
|
21 |
+
# Initialize search history and data storage for analytics
|
22 |
+
if 'search_history' not in st.session_state:
|
23 |
+
st.session_state.search_history = []
|
24 |
+
if 'search_data' not in st.session_state:
|
25 |
+
st.session_state.search_data = pd.DataFrame(columns=["Query", "Source", "Timestamp"])
|
26 |
+
|
27 |
+
def main():
|
28 |
+
st.title("Enhanced Google Search Application")
|
29 |
+
|
30 |
+
# User inputs for API key, CSE ID, and search query
|
31 |
+
api_key = st.secrets["GOOGLE_API_KEY"] # Use Streamlit secrets for security
|
32 |
+
cse_id = st.secrets["CSE_ID"]
|
33 |
+
|
34 |
+
query = st.text_input("Enter your search query", "", key='query_input')
|
35 |
+
|
36 |
+
# Voice search feature
|
37 |
+
if st.button("Use Voice Search"):
|
38 |
+
recognizer = sr.Recognizer()
|
39 |
+
with sr.Microphone() as source:
|
40 |
+
st.write("Listening...")
|
41 |
+
audio = recognizer.listen(source)
|
42 |
+
try:
|
43 |
+
query = recognizer.recognize_google(audio)
|
44 |
+
st.write(f"You said: {query}")
|
45 |
+
if api_key and cse_id and query:
|
46 |
+
results = google_search(api_key, cse_id, query)
|
47 |
+
update_search_history(query, "Voice")
|
48 |
+
display_results(results)
|
49 |
+
except sr.UnknownValueError:
|
50 |
+
st.error("Could not understand audio.")
|
51 |
+
except sr.RequestError:
|
52 |
+
st.error("Could not request results from Google.")
|
53 |
+
|
54 |
+
# Trigger search when clicking the search button
|
55 |
+
if st.button("Search") and query:
|
56 |
+
if api_key and cse_id:
|
57 |
+
results = google_search(api_key, cse_id, query)
|
58 |
+
update_search_history(query, "Text")
|
59 |
+
display_results(results)
|
60 |
+
else:
|
61 |
+
st.error("Please enter API Key, CSE ID, and a search query.")
|
62 |
+
|
63 |
+
# Show search history
|
64 |
+
if st.button("Show Search History"):
|
65 |
+
if st.session_state.search_history:
|
66 |
+
st.write("Search History:")
|
67 |
+
for h in st.session_state.search_history:
|
68 |
+
st.write(h)
|
69 |
+
else:
|
70 |
+
st.write("No search history found.")
|
71 |
+
|
72 |
+
# Clear search history
|
73 |
+
if st.button("Clear Search History"):
|
74 |
+
st.session_state.search_history.clear()
|
75 |
+
st.session_state.search_data = pd.DataFrame(columns=["Query", "Source", "Timestamp"])
|
76 |
+
st.success("Search history cleared.")
|
77 |
+
|
78 |
+
# Interactive Analytics Dashboard
|
79 |
+
st.subheader("Search Analytics")
|
80 |
+
if not st.session_state.search_data.empty:
|
81 |
+
search_trends = alt.Chart(st.session_state.search_data).mark_line().encode(
|
82 |
+
x='Timestamp:T',
|
83 |
+
y='count():Q',
|
84 |
+
color='Source:N',
|
85 |
+
tooltip=['Query:N', 'count():Q', 'Source:N']
|
86 |
+
).properties(width=600, height=300)
|
87 |
+
st.altair_chart(search_trends, use_container_width=True)
|
88 |
+
|
89 |
+
# Most popular queries
|
90 |
+
st.write("**Top Search Queries**")
|
91 |
+
top_queries = (
|
92 |
+
st.session_state.search_data['Query']
|
93 |
+
.value_counts()
|
94 |
+
.head(5)
|
95 |
+
.reset_index()
|
96 |
+
.rename(columns={'index': 'Query', 'Query': 'Count'})
|
97 |
+
)
|
98 |
+
st.write(top_queries)
|
99 |
+
|
100 |
+
def display_results(results):
|
101 |
+
if results and 'items' in results:
|
102 |
+
for i, item in enumerate(results['items']):
|
103 |
+
st.write(f"**{i + 1}. {item['title']}**")
|
104 |
+
st.write(f"[Link]({item['link']})")
|
105 |
+
st.write(f"{item['snippet']}\n")
|
106 |
+
|
107 |
+
# Display image if available
|
108 |
+
if 'pagemap' in item and 'cse_image' in item['pagemap']:
|
109 |
+
image_data = item['pagemap']['cse_image'][0]
|
110 |
+
image_url = image_data.get('src')
|
111 |
+
|
112 |
+
if image_url:
|
113 |
+
try:
|
114 |
+
response = requests.get(image_url)
|
115 |
+
img = Image.open(BytesIO(response.content))
|
116 |
+
st.image(img, width=100)
|
117 |
+
except Exception:
|
118 |
+
st.write("**Image could not be loaded.**")
|
119 |
+
else:
|
120 |
+
st.write("**Image source not available.**")
|
121 |
+
else:
|
122 |
+
st.write("No image available for this result.")
|
123 |
+
else:
|
124 |
+
st.write("No results found.")
|
125 |
+
|
126 |
+
def update_search_history(query, source):
|
127 |
+
st.session_state.search_history.append(query)
|
128 |
+
new_data = pd.DataFrame({
|
129 |
+
"Query": [query],
|
130 |
+
"Source": [source],
|
131 |
+
"Timestamp": [pd.Timestamp.now()]
|
132 |
+
})
|
133 |
+
st.session_state.search_data = pd.concat([st.session_state.search_data, new_data], ignore_index=True)
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
main()
|