Echo-Search / app.py
gefiwek187's picture
Update app.py
ce71ba5 verified
import streamlit as st
import json
from duckduckgo_search import DDGS
from typing import List, Dict
st.set_page_config(page_title="Echo Search Demo", page_icon="πŸ”")
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
try:
results = list(DDGS().text(query, max_results=max_results))
if not results:
st.warning(f"No results found for query: {query}")
return results
except Exception as e:
st.error(f"An error occurred during web search: {str(e)}")
return [{"error": f"An error occurred during web search: {str(e)}"}]
def summarize_web_results(query: str, search_results: List[Dict[str, str]]) -> str:
try:
search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
sources = [result['href'] for result in search_results if 'href' in result]
prompt = f"""You are a highly intelligent & expert analyst. Your job is to skillfully articulate the web search results about '{query}'.
Create a comprehensive summary focusing on the following points:
1. Provide an introduction to the topic.
2. Include key facts, relevant statistics, and expert opinions if available.
3. Ensure the summary is well-structured with an introduction, main body, and conclusion.
4. Do not include any links or citations within the main text.
Use the following search results to create your summary:
{search_context}
Summary:"""
summary = DDGS().chat(prompt, model="gpt-4o-mini")
# Append sources to the summary
summary_with_sources = summary + "\n\nSources:\n" + "\n".join(f"{idx + 1}. {link}" for idx, link in enumerate(sources))
return summary_with_sources
except Exception as e:
return f"An error occurred during summarization: {str(e)}"
st.title("Echo Search Demo πŸ”")
st.markdown("""
Welcome to Echo Search Demo! This application allows you to search the web and get a summarized result of your query.
Here's how it works:
1. Enter your search query in the text box below.
2. Click the 'Search' button or press Enter.
3. The app will search the web and generate a comprehensive summary of the results.
4. The summary will include key facts, statistics, and expert opinions when available.
5. Sources will be listed at the end of the summary.
Give it a try and explore the power of web search and AI summarization!
""")
query = st.text_input("Enter your search query:")
if st.button("Search") or query:
if query:
with st.spinner("Searching and summarizing..."):
search_results = get_web_search_results(query)
if not search_results or "error" in search_results[0]:
st.error("No results found or an error occurred during the search.")
else:
summary = summarize_web_results(query, search_results)
st.subheader("Web Search Summary")
st.write(summary)
else:
st.warning("Please enter a search query.")
st.markdown("---")
st.markdown("Powered by Echo 1.5")