Spaces:
Sleeping
Sleeping
streamlit app now has all 4 choices and displays selection after the vote was submitted.
Browse files- app.py +66 -59
- brave_ai.py +1 -0
- helpers.py +20 -67
app.py
CHANGED
@@ -1,44 +1,22 @@
|
|
1 |
import streamlit as st
|
2 |
import random
|
3 |
-
from helpers import query_you_com, query_tavily, query_perplexity
|
4 |
-
from mongod_db import MongoDBHandler
|
5 |
-
from swarms.utils.loguru_logger import logger
|
6 |
import time
|
7 |
|
8 |
-
mongo = MongoDBHandler()
|
9 |
-
|
10 |
# Set Streamlit to wide mode
|
11 |
st.set_page_config(layout="wide")
|
12 |
|
13 |
-
|
14 |
# Define the function to process the question
|
15 |
def ProcessQuestion(question):
|
16 |
-
# Randomly select two out of the
|
17 |
-
functions = [query_you_com, query_tavily, query_perplexity]
|
18 |
selected_functions = random.sample(functions, 2)
|
19 |
|
20 |
# Get answers from the selected functions
|
21 |
answer_a = selected_functions[0](question)
|
22 |
answer_b = selected_functions[1](question)
|
23 |
-
|
24 |
-
# Log into mongodb
|
25 |
-
try:
|
26 |
-
logger.info(f"Logging question: {question}")
|
27 |
-
mongo.add(
|
28 |
-
{
|
29 |
-
"question": question,
|
30 |
-
"answer_a": answer_a,
|
31 |
-
"answer_b": answer_b,
|
32 |
-
"selected_functions": [f.__name__ for f in selected_functions],
|
33 |
-
"query_time": time.time(),
|
34 |
-
}
|
35 |
-
)
|
36 |
-
logger.info("Successfully logged into mongodb")
|
37 |
-
except Exception as e:
|
38 |
-
logger.error(f"Error logging into mongodb: {e}")
|
39 |
-
|
40 |
-
return answer_a, answer_b
|
41 |
|
|
|
42 |
|
43 |
# Initialize session state if not already done
|
44 |
if "results_displayed" not in st.session_state:
|
@@ -49,34 +27,38 @@ if "answer_b" not in st.session_state:
|
|
49 |
st.session_state["answer_b"] = ""
|
50 |
if "question" not in st.session_state:
|
51 |
st.session_state["question"] = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Streamlit app layout
|
54 |
st.title("Search Engine Agent Comparison")
|
55 |
|
56 |
-
#
|
57 |
-
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
question = st.text_area(
|
62 |
-
"Enter your question here (max 1000 characters):", max_chars=1000
|
63 |
-
)
|
64 |
-
|
65 |
-
with control_col:
|
66 |
-
# Submit button
|
67 |
-
submit_button = st.button("Submit")
|
68 |
|
69 |
if submit_button:
|
70 |
if question:
|
71 |
if len(question) <= 1000:
|
72 |
# Process the question and get answers
|
73 |
-
answer_a, answer_b = ProcessQuestion(question)
|
74 |
|
75 |
# Save answers and state to session state
|
76 |
st.session_state["answer_a"] = answer_a
|
77 |
st.session_state["answer_b"] = answer_b
|
78 |
st.session_state["question"] = question
|
|
|
|
|
79 |
st.session_state["results_displayed"] = True
|
|
|
80 |
else:
|
81 |
st.error(
|
82 |
"Your question exceeds the 1,000 character limit. Please shorten your question."
|
@@ -86,34 +68,59 @@ if submit_button:
|
|
86 |
|
87 |
# Display results if available in session state
|
88 |
if st.session_state["results_displayed"]:
|
89 |
-
|
90 |
-
with col1:
|
91 |
-
st.write("### Output A")
|
92 |
-
st.write(st.session_state["answer_a"])
|
93 |
-
a_feedback_grid = st.columns(1)
|
94 |
-
with col2:
|
95 |
-
st.write("### Output B")
|
96 |
-
st.write(st.session_state["answer_b"])
|
97 |
-
b_feedback_grid = st.columns(2)
|
98 |
-
|
99 |
-
# Create a placeholder for the feedback div
|
100 |
-
feedback_placeholder = st.empty()
|
101 |
|
102 |
def display_feedback(message):
|
103 |
-
|
104 |
f'<div style="position: fixed; bottom: 10px; left: 10px; background-color: #f0f0f0; padding: 10px; border-radius: 5px;">{message}</div>',
|
105 |
unsafe_allow_html=True,
|
106 |
)
|
107 |
|
108 |
-
with
|
109 |
-
if st.button("A is better 🥇"):
|
110 |
-
display_feedback("You selected: A is better")
|
111 |
-
with b_feedback_grid[0]:
|
112 |
-
if st.button("B is better 💪"):
|
113 |
-
display_feedback("You selected: B is better")
|
114 |
-
with a_feedback_grid[0]:
|
115 |
if st.button("It's a Tie 🤝"):
|
|
|
116 |
display_feedback("You selected: It's a Tie")
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
if st.button("Both are bad 👎"):
|
|
|
119 |
display_feedback("You selected: Both are bad")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import random
|
3 |
+
from helpers import query_you_com, query_tavily, query_perplexity, query_brave
|
|
|
|
|
4 |
import time
|
5 |
|
|
|
|
|
6 |
# Set Streamlit to wide mode
|
7 |
st.set_page_config(layout="wide")
|
8 |
|
|
|
9 |
# Define the function to process the question
|
10 |
def ProcessQuestion(question):
|
11 |
+
# Randomly select two out of the four functions
|
12 |
+
functions = [query_you_com, query_tavily, query_perplexity, query_brave]
|
13 |
selected_functions = random.sample(functions, 2)
|
14 |
|
15 |
# Get answers from the selected functions
|
16 |
answer_a = selected_functions[0](question)
|
17 |
answer_b = selected_functions[1](question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
return answer_a, answer_b, selected_functions
|
20 |
|
21 |
# Initialize session state if not already done
|
22 |
if "results_displayed" not in st.session_state:
|
|
|
27 |
st.session_state["answer_b"] = ""
|
28 |
if "question" not in st.session_state:
|
29 |
st.session_state["question"] = ""
|
30 |
+
if "source_a" not in st.session_state:
|
31 |
+
st.session_state["source_a"] = ""
|
32 |
+
if "source_b" not in st.session_state:
|
33 |
+
st.session_state["source_b"] = ""
|
34 |
+
if "winner" not in st.session_state:
|
35 |
+
st.session_state["winner"] = ""
|
36 |
|
37 |
# Streamlit app layout
|
38 |
st.title("Search Engine Agent Comparison")
|
39 |
|
40 |
+
# Text box for user input with character limit
|
41 |
+
question = st.text_area(
|
42 |
+
"Enter your question here (max 1000 characters):", max_chars=1000
|
43 |
+
)
|
44 |
|
45 |
+
# Submit button
|
46 |
+
submit_button = st.button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
if submit_button:
|
49 |
if question:
|
50 |
if len(question) <= 1000:
|
51 |
# Process the question and get answers
|
52 |
+
answer_a, answer_b, selected_functions = ProcessQuestion(question)
|
53 |
|
54 |
# Save answers and state to session state
|
55 |
st.session_state["answer_a"] = answer_a
|
56 |
st.session_state["answer_b"] = answer_b
|
57 |
st.session_state["question"] = question
|
58 |
+
st.session_state["source_a"] = selected_functions[0].__name__
|
59 |
+
st.session_state["source_b"] = selected_functions[1].__name__
|
60 |
st.session_state["results_displayed"] = True
|
61 |
+
st.session_state["winner"] = ""
|
62 |
else:
|
63 |
st.error(
|
64 |
"Your question exceeds the 1,000 character limit. Please shorten your question."
|
|
|
68 |
|
69 |
# Display results if available in session state
|
70 |
if st.session_state["results_displayed"]:
|
71 |
+
button_col1, button_col2, button_col3, button_col4 = st.columns(4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
def display_feedback(message):
|
74 |
+
st.markdown(
|
75 |
f'<div style="position: fixed; bottom: 10px; left: 10px; background-color: #f0f0f0; padding: 10px; border-radius: 5px;">{message}</div>',
|
76 |
unsafe_allow_html=True,
|
77 |
)
|
78 |
|
79 |
+
with button_col1:
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
if st.button("It's a Tie 🤝"):
|
81 |
+
st.session_state["winner"] = "Tie"
|
82 |
display_feedback("You selected: It's a Tie")
|
83 |
+
|
84 |
+
with button_col2:
|
85 |
+
if st.button("A is better 💪"):
|
86 |
+
st.session_state["winner"] = "A"
|
87 |
+
display_feedback("You selected: A is better")
|
88 |
+
with button_col3:
|
89 |
+
if st.button("B is better 🥇"):
|
90 |
+
st.session_state["winner"] = "B"
|
91 |
+
display_feedback("You selected: B is better")
|
92 |
+
with button_col4:
|
93 |
if st.button("Both are bad 👎"):
|
94 |
+
st.session_state["winner"] = "Both are bad"
|
95 |
display_feedback("You selected: Both are bad")
|
96 |
+
|
97 |
+
col1, col2 = st.columns(2)
|
98 |
+
with col1:
|
99 |
+
if st.session_state["winner"]:
|
100 |
+
if st.session_state["winner"] == "A":
|
101 |
+
st.write(f"### ⭐ {st.session_state['source_a'].replace('query_', '').capitalize()} 🥇")
|
102 |
+
|
103 |
+
elif st.session_state["winner"] == "B":
|
104 |
+
st.write(f"### {st.session_state['source_a'].replace('query_', '').capitalize()} 🥈")
|
105 |
+
else:
|
106 |
+
st.write("### Result A")
|
107 |
+
st.write(st.session_state["answer_a"])
|
108 |
+
|
109 |
+
with col2:
|
110 |
+
if st.session_state["winner"]:
|
111 |
+
if st.session_state["winner"] == "B":
|
112 |
+
st.write(f"### ⭐ {st.session_state['source_b'].replace('query_', '').capitalize()} 🥇")
|
113 |
+
|
114 |
+
elif st.session_state["winner"] == "A":
|
115 |
+
st.write(f"### {st.session_state['source_b'].replace('query_', '').capitalize()} 🥈")
|
116 |
+
else:
|
117 |
+
st.write("### Result B")
|
118 |
+
st.write(st.session_state["answer_b"])
|
119 |
+
|
120 |
+
# Add information about human feedback
|
121 |
+
st.write("### Importance of Human Feedback")
|
122 |
+
st.write("""
|
123 |
+
Comparing search results from different engines is crucial for improving search technologies.
|
124 |
+
Your feedback helps us understand which search engines provide the most relevant results.
|
125 |
+
This approach is similar to LMSys Chatbot Arena where they compare the outputs of different open-source chatbots.
|
126 |
+
""")
|
brave_ai.py
CHANGED
@@ -39,6 +39,7 @@ class BraveAIWrapper(BaseModel):
|
|
39 |
"count": count,
|
40 |
"summary": True,
|
41 |
"safe_search": safe_search,
|
|
|
42 |
}
|
43 |
try:
|
44 |
response = requests.get(self.base_search_url, headers=self.headers, params=params)
|
|
|
39 |
"count": count,
|
40 |
"summary": True,
|
41 |
"safe_search": safe_search,
|
42 |
+
"extra_snippets": True,
|
43 |
}
|
44 |
try:
|
45 |
response = requests.get(self.base_search_url, headers=self.headers, params=params)
|
helpers.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import requests
|
2 |
from dotenv import load_dotenv
|
|
|
3 |
import os
|
4 |
|
5 |
# Load environment variables from .env file
|
@@ -9,8 +10,7 @@ load_dotenv()
|
|
9 |
YOU_COM_API_KEY = os.getenv("YOU_API_KEY")
|
10 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
11 |
PERPLEXITY_API_KEY = os.getenv("PPLX_API_KEY")
|
12 |
-
BRAVE_API_KEY = os.getenv("
|
13 |
-
|
14 |
|
15 |
def query_you_com(query):
|
16 |
headers = {"X-API-Key": YOU_COM_API_KEY}
|
@@ -76,71 +76,6 @@ def query_perplexity(query):
|
|
76 |
return f"Request failed with status code: {response.status_code}"
|
77 |
|
78 |
|
79 |
-
# def query_brave(query):
|
80 |
-
# headers = {"X-API-Key": BRAVE_API_KEY}
|
81 |
-
# params = {
|
82 |
-
# "q": query,
|
83 |
-
# "count": 1,
|
84 |
-
# "summary": True
|
85 |
-
# }
|
86 |
-
# response = requests.get("https://api.search.brave.com/res/v1/web/search", params=params, headers=headers)
|
87 |
-
# if response.status_code == 200:
|
88 |
-
# return response.json().get("summary", "No summary available.")
|
89 |
-
# else:
|
90 |
-
# return f"Request failed with status code: {response}"
|
91 |
-
|
92 |
-
|
93 |
-
# def brave_search_summarization(query):
|
94 |
-
# # Endpoint for web search with summary
|
95 |
-
# web_search_url = "https://api.search.brave.com/res/v1/web/search"
|
96 |
-
# summarizer_url = "https://api.search.brave.com/res/v1/summarizer/search"
|
97 |
-
|
98 |
-
# # Headers for the requests
|
99 |
-
# headers = {
|
100 |
-
# "Accept": "application/json",
|
101 |
-
# "Accept-Encoding": "gzip",
|
102 |
-
# "X-Subscription-Token": BRAVE_API_KEY
|
103 |
-
# }
|
104 |
-
|
105 |
-
# # Parameters for the initial web search request
|
106 |
-
# web_search_params = {
|
107 |
-
# "q": query,
|
108 |
-
# "summary": 1
|
109 |
-
# }
|
110 |
-
|
111 |
-
# # Make the initial request to the web search endpoint
|
112 |
-
# web_search_response = requests.get(web_search_url, headers=headers, params=web_search_params)
|
113 |
-
|
114 |
-
# # Check if the request was successful
|
115 |
-
# if web_search_response.status_code != 200:
|
116 |
-
# raise Exception(f"Web search request failed with status code {web_search_response.status_code}")
|
117 |
-
|
118 |
-
# web_search_data = web_search_response.json()
|
119 |
-
|
120 |
-
# # Extract the summarizer key from the response
|
121 |
-
# summarizer_key = web_search_data.get('summarizer', {}).get('key')
|
122 |
-
# if not summarizer_key:
|
123 |
-
# raise Exception("No summarizer key found in the web search response")
|
124 |
-
|
125 |
-
# # Parameters for the summarizer request
|
126 |
-
# summarizer_params = {
|
127 |
-
# "key": summarizer_key,
|
128 |
-
# "entity_info": 1
|
129 |
-
# }
|
130 |
-
|
131 |
-
# # Make the request to the summarizer endpoint
|
132 |
-
# summarizer_response = requests.get(summarizer_url, headers=headers, params=summarizer_params)
|
133 |
-
|
134 |
-
# # Check if the request was successful
|
135 |
-
# if summarizer_response.status_code != 200:
|
136 |
-
# raise Exception(f"Summarizer request failed with status code {summarizer_response.status_code}")
|
137 |
-
|
138 |
-
# summarizer_data = summarizer_response.json()
|
139 |
-
|
140 |
-
# # Return the summarized content
|
141 |
-
# return summarizer_data
|
142 |
-
|
143 |
-
|
144 |
def ProcessQuestion(question, model):
|
145 |
if model == "You.com":
|
146 |
return query_you_com(question)
|
@@ -152,3 +87,21 @@ def ProcessQuestion(question, model):
|
|
152 |
return query_brave(question)
|
153 |
else:
|
154 |
return "Model not supported"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
from dotenv import load_dotenv
|
3 |
+
from typing import Optional
|
4 |
import os
|
5 |
|
6 |
# Load environment variables from .env file
|
|
|
10 |
YOU_COM_API_KEY = os.getenv("YOU_API_KEY")
|
11 |
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
12 |
PERPLEXITY_API_KEY = os.getenv("PPLX_API_KEY")
|
13 |
+
BRAVE_API_KEY = os.getenv("BRAVE_AI_API_KEY")
|
|
|
14 |
|
15 |
def query_you_com(query):
|
16 |
headers = {"X-API-Key": YOU_COM_API_KEY}
|
|
|
76 |
return f"Request failed with status code: {response.status_code}"
|
77 |
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def ProcessQuestion(question, model):
|
80 |
if model == "You.com":
|
81 |
return query_you_com(question)
|
|
|
87 |
return query_brave(question)
|
88 |
else:
|
89 |
return "Model not supported"
|
90 |
+
|
91 |
+
|
92 |
+
from brave_ai import BraveAIWrapper
|
93 |
+
|
94 |
+
def query_brave(query: str) -> Optional[str]:
|
95 |
+
"""
|
96 |
+
Get a summary for the given query using BraveAIWrapper.
|
97 |
+
|
98 |
+
Args:
|
99 |
+
query (str): The search query.
|
100 |
+
api_key (str): The API key for Brave Search.
|
101 |
+
|
102 |
+
Returns:
|
103 |
+
Optional[str]: Summarized result or None if an error occurs.
|
104 |
+
"""
|
105 |
+
brave_ai = BraveAIWrapper(api_key=BRAVE_API_KEY)
|
106 |
+
summary = brave_ai.get_and_summarize(query)
|
107 |
+
return summary
|