forked from jgravelle/Groqqle
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGroqqle.py
170 lines (146 loc) · 4.77 KB
/
Groqqle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import streamlit as st
import json
from PIL import Image
import base64
from agents.Web_Agent import Web_Agent
import os
from dotenv import load_dotenv
import sys
from flask import Flask, request, jsonify
load_dotenv()
def get_groq_api_key():
api_key = os.getenv('GROQ_API_KEY')
if not api_key:
if 'groq_api_key' not in st.session_state:
st.warning("Groq API Key not found. Please enter your API key below:")
api_key = st.text_input("Groq API Key", type="password")
if api_key:
st.session_state.groq_api_key = api_key
else:
api_key = st.session_state.groq_api_key
else:
st.session_state.groq_api_key = api_key
return api_key
def main():
st.set_page_config(page_title="Groqqle", layout="wide")
st.markdown("""
<style>
.stApp {
max-width: 100%;
}
.main {
padding-top: 50px;
padding-left: 20%;
padding-right: 20%;
}
.stButton>button {
background-color: #f8f9fa;
border: 1px solid #f8f9fa;
border-radius: 4px;
color: #3c4043;
font-family: arial,sans-serif;
font-size: 14px;
margin: 11px 4px;
padding: 0 16px;
line-height: 27px;
height: 36px;
min-width: 120px;
text-align: center;
cursor: pointer;
user-select: none;
white-space: nowrap;
width: 100%;
}
.search-container {
max-width: 600px;
margin: 0 auto;
}
.search-results {
max-width: 600px;
margin: 0 auto;
text-align: left;
padding: 0;
}
.search-result {
margin-bottom: 20px;
}
.search-result-title {
font-size: 18px;
color: #1a0dab;
text-decoration: none;
}
.search-result-url {
font-size: 14px;
color: #006621;
}
.search-result-description {
font-size: 14px;
color: #545454;
}
</style>
""", unsafe_allow_html=True)
api_key = get_groq_api_key()
if not api_key:
st.error("Please provide a valid Groq API Key to use the application.")
return
agent = Web_Agent(api_key)
st.markdown('<div class="search-container">', unsafe_allow_html=True)
st.image("images/logo.png", width=272)
query = st.text_input("Search query", key="search_bar", on_change=perform_search, label_visibility="collapsed")
col1, col2, col3 = st.columns([2,1,2])
with col1:
if st.button("Groqqle Search", key="search_button"):
perform_search()
with col3:
json_results = st.checkbox("JSON Results", value=False, key="json_results")
st.markdown('</div>', unsafe_allow_html=True)
if st.session_state.get('search_results'):
display_results(st.session_state.search_results, json_results)
def perform_search():
query = st.session_state.search_bar
if query and 'groq_api_key' in st.session_state:
with st.spinner('Searching...'):
results = Web_Agent(st.session_state.groq_api_key).process_request(query)
st.session_state.search_results = results
def display_results(results, json_format=False):
if results:
st.markdown("---")
st.markdown('<div class="search-container">', unsafe_allow_html=True)
st.markdown("### Search Results")
st.markdown('</div>', unsafe_allow_html=True)
if json_format:
st.json(results)
else:
st.markdown('<div class="search-results">', unsafe_allow_html=True)
for result in results:
st.markdown(f"""
<div class="search-result">
<a href="{result['url']}" class="search-result-title" target="_blank">{result['title']}</a>
<div class="search-result-url">{result['url']}</div>
<div class="search-result-description">{result['description']}</div>
</div>
""", unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
else:
st.markdown("No results found.")
def create_api_app():
app = Flask(__name__)
@app.route('/search', methods=['POST'])
def api_search():
data = request.json
query = data.get('query')
if not query:
return jsonify({"error": "No query provided"}), 400
api_key = os.getenv('GROQ_API_KEY')
if not api_key:
return jsonify({"error": "Groq API Key not set"}), 500
agent = Web_Agent(api_key)
results = agent.process_request(query)
return jsonify(results)
return app
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'api':
api_app = create_api_app()
api_app.run(debug=True, port=5000)
else:
main()