Spaces:
Sleeping
Sleeping
Echo-ai
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
from duckduckgo_search import DDGS
|
4 |
+
from typing import List, Dict
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Echo Search Demo", page_icon="🔍")
|
7 |
+
|
8 |
+
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
9 |
+
try:
|
10 |
+
results = list(DDGS().text(query, max_results=max_results))
|
11 |
+
if not results:
|
12 |
+
st.warning(f"No results found for query: {query}")
|
13 |
+
return results
|
14 |
+
except Exception as e:
|
15 |
+
st.error(f"An error occurred during web search: {str(e)}")
|
16 |
+
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
17 |
+
|
18 |
+
def summarize_web_results(query: str, search_results: List[Dict[str, str]]) -> str:
|
19 |
+
try:
|
20 |
+
search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
|
21 |
+
sources = [result['href'] for result in search_results if 'href' in result]
|
22 |
+
|
23 |
+
prompt = f"""You are a highly intelligent & expert analyst. Your job is to skillfully articulate the web search results about '{query}'.
|
24 |
+
Create a comprehensive summary focusing on the following points:
|
25 |
+
1. Provide an introduction to the topic.
|
26 |
+
2. Include key facts, relevant statistics, and expert opinions if available.
|
27 |
+
3. Ensure the summary is well-structured with an introduction, main body, and conclusion.
|
28 |
+
4. Do not include any links or citations within the main text.
|
29 |
+
|
30 |
+
Use the following search results to create your summary:
|
31 |
+
|
32 |
+
{search_context}
|
33 |
+
|
34 |
+
Summary:"""
|
35 |
+
|
36 |
+
summary = DDGS().chat(prompt, model="GPT-4o-mini")
|
37 |
+
|
38 |
+
# Append sources to the summary
|
39 |
+
summary_with_sources = summary + "\n\nSources:\n" + "\n".join(f"{idx + 1}. {link}" for idx, link in enumerate(sources))
|
40 |
+
return summary_with_sources
|
41 |
+
except Exception as e:
|
42 |
+
return f"An error occurred during summarization: {str(e)}"
|
43 |
+
|
44 |
+
st.title("Echo Search Demo 🔍")
|
45 |
+
|
46 |
+
st.markdown("""
|
47 |
+
Welcome to Echo Search Demo! This application allows you to search the web and get a summarized result of your query.
|
48 |
+
|
49 |
+
Here's how it works:
|
50 |
+
1. Enter your search query in the text box below.
|
51 |
+
2. Click the 'Search' button or press Enter.
|
52 |
+
3. The app will search the web and generate a comprehensive summary of the results.
|
53 |
+
4. The summary will include key facts, statistics, and expert opinions when available.
|
54 |
+
5. Sources will be listed at the end of the summary.
|
55 |
+
|
56 |
+
Give it a try and explore the power of web search and AI summarization!
|
57 |
+
""")
|
58 |
+
|
59 |
+
query = st.text_input("Enter your search query:")
|
60 |
+
|
61 |
+
if st.button("Search") or query:
|
62 |
+
if query:
|
63 |
+
with st.spinner("Searching and summarizing..."):
|
64 |
+
search_results = get_web_search_results(query)
|
65 |
+
|
66 |
+
if not search_results or "error" in search_results[0]:
|
67 |
+
st.error("No results found or an error occurred during the search.")
|
68 |
+
else:
|
69 |
+
summary = summarize_web_results(query, search_results)
|
70 |
+
|
71 |
+
st.subheader("Web Search Summary")
|
72 |
+
st.write(summary)
|
73 |
+
else:
|
74 |
+
st.warning("Please enter a search query.")
|
75 |
+
|
76 |
+
st.markdown("---")
|
77 |
+
st.markdown("Powered by Echo 1.5")
|