drvikasgaur commited on
Commit
feedb11
·
verified ·
1 Parent(s): f474072

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -54
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 API
8
  def search_api(query):
9
  if not query.strip():
10
- return pd.DataFrame([{"Title": "Empty query. Please enter a topic.", "URL": ""}])
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 subtopic in topic["Topics"]:
28
- if "Text" in subtopic and "FirstURL" in subtopic:
29
- results.append({
30
- "Title": subtopic["Text"],
31
- "URL": subtopic["FirstURL"]
32
- })
33
-
34
  if not results:
35
  results.append({"Title": "No results found.", "URL": ""})
36
 
37
- return pd.DataFrame(results)
38
-
 
 
 
39
  except Exception as e:
40
- return pd.DataFrame([{"Title": "Error fetching results.", "URL": str(e)}])
41
 
42
- # Export to CSV
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 to DOCX
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 _, row in results.iterrows():
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
- # Gradio UI
63
  with gr.Blocks() as demo:
64
- gr.Markdown("## 🔍 DuckDuckGo Search & Export Tool")
65
  gr.Markdown("""
66
- Welcome to the DuckDuckGo Search & Export App!
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
- **🔹 Features:**
74
- - Fast search via DuckDuckGo Instant Answer API
75
- - Download results in CSV or DOCX format
76
- - Mobile & desktop friendly
77
-
78
- ⚠️ *Note: This tool fetches basic related links, not full web content.*
79
  """)
80
 
81
- query = gr.Textbox(label="Search Query", placeholder="Enter a topic to search...")
82
- search_btn = gr.Button("Search")
 
83
 
84
- result_state = gr.State(value=pd.DataFrame())
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 as CSV")
89
- export_docx_btn = gr.Button("Export as Word DOCX")
90
 
91
  csv_file = gr.File(label="Download CSV")
92
  docx_file = gr.File(label="Download DOCX")
93
 
94
- def run_search(q):
 
95
  df = search_api(q)
96
- return df, df
97
-
98
- search_btn.click(fn=run_search, inputs=query, outputs=[results_display, result_state])
99
- export_csv_btn.click(fn=export_csv, inputs=result_state, outputs=csv_file)
100
- export_docx_btn.click(fn=export_docx, inputs=result_state, outputs=docx_file)
101
-
102
- demo.launch()
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)