Spaces:
Running
Running
File size: 2,179 Bytes
5d3056e 21eada5 4fef53c bf857f3 21eada5 4fef53c 9460907 4fef53c 21eada5 4fef53c 21eada5 4fef53c 21eada5 4fef53c 21eada5 4fef53c 21eada5 4fef53c bf857f3 4fef53c bf857f3 9460907 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import gradio as gr
import pandas as pd
import requests
import os
from datetime import datetime
from docx import Document
def search_api(query):
url = f"https://api.duckduckgo.com/?q={query}&format=json&no_redirect=1"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
related_topics = data.get("RelatedTopics", [])
results = []
for topic in related_topics:
if "Text" in topic and "FirstURL" in topic:
results.append({
"Title": topic["Text"],
"URL": topic["FirstURL"]
})
return pd.DataFrame(results)
else:
return pd.DataFrame([{"Title": "Error", "URL": "Failed to fetch results"}])
# Save results
def export_csv(df):
file_path = "search_results.csv"
df.to_csv(file_path, index=False)
return file_path
def export_docx(df):
doc = Document()
doc.add_heading("Search Results", 0)
for _, row in df.iterrows():
doc.add_paragraph(f"{row['Title']}\n{row['URL']}")
file_path = "search_results.docx"
doc.save(file_path)
return file_path
# Set up Gradio Blocks
with gr.Blocks() as demo:
gr.Markdown("## 🔎 Simple Search with Export Options")
query_input = gr.Textbox(label="Search Query", placeholder="Enter a topic...")
search_button = gr.Button("Search")
results_df = gr.Dataframe(label="Search Results", interactive=False)
with gr.Row():
export_csv_button = gr.Button("Export as CSV")
export_docx_button = gr.Button("Export as Word DOCX")
csv_output = gr.File(label="Download CSV")
docx_output = gr.File(label="Download DOCX")
# Logic
def handle_search(q):
df = search_api(q)
return df
def handle_csv_export(df):
return export_csv(df)
def handle_docx_export(df):
return export_docx(df)
search_button.click(handle_search, inputs=query_input, outputs=results_df)
export_csv_button.click(handle_csv_export, inputs=results_df, outputs=csv_output)
export_docx_button.click(handle_docx_export, inputs=results_df, outputs=docx_output)
demo.launch()
|