Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,12 @@ import pandas as pd
|
|
3 |
import requests
|
4 |
import tempfile
|
5 |
from docx import Document
|
|
|
6 |
|
7 |
-
# DuckDuckGo search
|
8 |
def search_api(query):
|
9 |
if not query.strip():
|
10 |
-
return pd.DataFrame([{"
|
11 |
|
12 |
url = f"https://api.duckduckgo.com/?q={query}&format=json&no_redirect=1"
|
13 |
try:
|
@@ -16,89 +17,90 @@ def search_api(query):
|
|
16 |
data = response.json()
|
17 |
related_topics = data.get("RelatedTopics", [])
|
18 |
results = []
|
19 |
-
|
20 |
for topic in related_topics:
|
21 |
if "Text" in topic and "FirstURL" in topic:
|
22 |
-
results.append({
|
23 |
-
"Title": topic["Text"],
|
24 |
-
"URL": topic["FirstURL"]
|
25 |
-
})
|
26 |
elif "Topics" in topic:
|
27 |
-
for
|
28 |
-
if "Text" in
|
29 |
-
results.append({
|
30 |
-
|
31 |
-
"URL": subtopic["FirstURL"]
|
32 |
-
})
|
33 |
-
|
34 |
if not results:
|
35 |
results.append({"Title": "No results found.", "URL": ""})
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
except Exception as e:
|
40 |
-
return pd.DataFrame([{"Title": "Error fetching results.", "URL": str(e)}])
|
41 |
|
42 |
-
# Export
|
43 |
-
def export_csv(results):
|
44 |
if results is None or results.empty:
|
45 |
return None
|
|
|
|
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline='', encoding="utf-8") as tmp:
|
47 |
results.to_csv(tmp.name, index=False)
|
48 |
return tmp.name
|
49 |
|
50 |
-
# Export
|
51 |
-
def export_docx(results):
|
52 |
if results is None or results.empty:
|
53 |
return None
|
|
|
|
|
54 |
doc = Document()
|
55 |
-
doc.add_heading("Search Results", 0)
|
56 |
-
for
|
57 |
-
doc.add_paragraph(f"{row['Title']}\n{row['URL']}")
|
58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
|
59 |
doc.save(tmp.name)
|
60 |
return tmp.name
|
61 |
|
62 |
-
#
|
63 |
with gr.Blocks() as demo:
|
64 |
-
gr.Markdown("##
|
65 |
gr.Markdown("""
|
66 |
-
|
67 |
-
|
68 |
-
**🔹 Instructions:**
|
69 |
-
1. Enter a **search query** in the textbox below.
|
70 |
-
2. Click **Search** to view relevant results from DuckDuckGo.
|
71 |
-
3. You can **download** the results as a **CSV file** or a **Word document**.
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
⚠️ *Note: This tool fetches basic related links, not full web content.*
|
79 |
""")
|
80 |
|
81 |
-
query = gr.Textbox(label="Search Query", placeholder="Enter
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
results_display = gr.Dataframe(headers=["Title", "URL"], label="Search Results", interactive=False)
|
86 |
|
87 |
with gr.Row():
|
88 |
-
export_csv_btn = gr.Button("Export
|
89 |
-
export_docx_btn = gr.Button("Export
|
90 |
|
91 |
csv_file = gr.File(label="Download CSV")
|
92 |
docx_file = gr.File(label="Download DOCX")
|
93 |
|
94 |
-
def
|
|
|
95 |
df = search_api(q)
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import requests
|
4 |
import tempfile
|
5 |
from docx import Document
|
6 |
+
from datetime import datetime
|
7 |
|
8 |
+
# DuckDuckGo API search
|
9 |
def search_api(query):
|
10 |
if not query.strip():
|
11 |
+
return pd.DataFrame([{"Rank": "", "Title": "Please enter a valid search query.", "URL": ""}])
|
12 |
|
13 |
url = f"https://api.duckduckgo.com/?q={query}&format=json&no_redirect=1"
|
14 |
try:
|
|
|
17 |
data = response.json()
|
18 |
related_topics = data.get("RelatedTopics", [])
|
19 |
results = []
|
20 |
+
|
21 |
for topic in related_topics:
|
22 |
if "Text" in topic and "FirstURL" in topic:
|
23 |
+
results.append({"Title": topic["Text"], "URL": topic["FirstURL"]})
|
|
|
|
|
|
|
24 |
elif "Topics" in topic:
|
25 |
+
for sub in topic["Topics"]:
|
26 |
+
if "Text" in sub and "FirstURL" in sub:
|
27 |
+
results.append({"Title": sub["Text"], "URL": sub["FirstURL"]})
|
28 |
+
|
|
|
|
|
|
|
29 |
if not results:
|
30 |
results.append({"Title": "No results found.", "URL": ""})
|
31 |
|
32 |
+
# Add ranking
|
33 |
+
for i, r in enumerate(results):
|
34 |
+
r["Rank"] = i + 1
|
35 |
+
|
36 |
+
return pd.DataFrame(results)[["Rank", "Title", "URL"]]
|
37 |
except Exception as e:
|
38 |
+
return pd.DataFrame([{"Rank": "", "Title": "Error fetching results.", "URL": str(e)}])
|
39 |
|
40 |
+
# Export CSV with timestamp
|
41 |
+
def export_csv(results, query):
|
42 |
if results is None or results.empty:
|
43 |
return None
|
44 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
45 |
+
fname = f"{query.replace(' ', '_')}_results_{timestamp}.csv"
|
46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline='', encoding="utf-8") as tmp:
|
47 |
results.to_csv(tmp.name, index=False)
|
48 |
return tmp.name
|
49 |
|
50 |
+
# Export DOCX with timestamp
|
51 |
+
def export_docx(results, query):
|
52 |
if results is None or results.empty:
|
53 |
return None
|
54 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
55 |
+
fname = f"{query.replace(' ', '_')}_results_{timestamp}.docx"
|
56 |
doc = Document()
|
57 |
+
doc.add_heading(f"Search Results for: {query}", 0)
|
58 |
+
for i, row in results.iterrows():
|
59 |
+
doc.add_paragraph(f"{row['Rank']}. {row['Title']}\n{row['URL']}")
|
60 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp:
|
61 |
doc.save(tmp.name)
|
62 |
return tmp.name
|
63 |
|
64 |
+
# UI
|
65 |
with gr.Blocks() as demo:
|
66 |
+
gr.Markdown("## 🔎 Smart Search & Export App")
|
67 |
gr.Markdown("""
|
68 |
+
This app lets you search DuckDuckGo and export the results as a **CSV or Word DOCX**.
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
**Instructions:**
|
71 |
+
1. Type a search query (e.g., *climate change*).
|
72 |
+
2. Click **Search** to get top DuckDuckGo topics.
|
73 |
+
3. Review the list and click to open links.
|
74 |
+
4. Export results for offline use.
|
|
|
75 |
""")
|
76 |
|
77 |
+
query = gr.Textbox(label="Search Query", placeholder="Enter topic...")
|
78 |
+
status = gr.Markdown("")
|
79 |
+
search_btn = gr.Button("🔍 Search Now")
|
80 |
|
81 |
+
results_display = gr.Dataframe(headers=["Rank", "Title", "URL"], label="Search Results", interactive=False)
|
|
|
82 |
|
83 |
with gr.Row():
|
84 |
+
export_csv_btn = gr.Button("⬇️ Export CSV")
|
85 |
+
export_docx_btn = gr.Button("⬇️ Export DOCX")
|
86 |
|
87 |
csv_file = gr.File(label="Download CSV")
|
88 |
docx_file = gr.File(label="Download DOCX")
|
89 |
|
90 |
+
def handle_search(q):
|
91 |
+
status_msg = f"Searching DuckDuckGo for **{q}**..."
|
92 |
df = search_api(q)
|
93 |
+
if "Error" in df.iloc[0]["Title"]:
|
94 |
+
status_msg = f"❌ Error: {df.iloc[0]['URL']}"
|
95 |
+
elif "Please enter" in df.iloc[0]["Title"]:
|
96 |
+
status_msg = "⚠️ Please enter a search term."
|
97 |
+
else:
|
98 |
+
status_msg = f"✅ Showing top {len(df)} results for: **{q}**"
|
99 |
+
return df, status_msg
|
100 |
+
|
101 |
+
# Actions
|
102 |
+
search_btn.click(fn=handle_search, inputs=query, outputs=[results_display, status])
|
103 |
+
export_csv_btn.click(fn=export_csv, inputs=[results_display, query], outputs=csv_file)
|
104 |
+
export_docx_btn.click(fn=export_docx, inputs=[results_display, query], outputs=docx_file)
|
105 |
+
|
106 |
+
demo.launch(share=True)
|