|
import os |
|
import json |
|
import asyncio |
|
from datetime import datetime |
|
import gradio as gr |
|
import httpx |
|
|
|
from websearch import PerplexityClient, parse_perplexity_response |
|
|
|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
perplexity_client = PerplexityClient( |
|
api_key=os.environ["PERPLEXITY_AUTH_TOKEN"] |
|
) |
|
|
|
async def query_api(query: str, website: str, after_date: str) -> tuple: |
|
if website: |
|
query = query + f" \"site:{website}\"" |
|
if after_date: |
|
query = query + f" \"after:{after_date}\"" |
|
print("query: ", query) |
|
try: |
|
|
|
response = await perplexity_client.generate_response(query) |
|
|
|
|
|
parsed_response = parse_perplexity_response(response) |
|
|
|
response_data = json.dumps(parsed_response) |
|
|
|
|
|
response_json = json.loads(response_data) |
|
|
|
|
|
content = response_json.get("content", "") |
|
citations = response_json.get("citations", []) |
|
|
|
|
|
beautified_content = f"# Search Results\n\n{content}" |
|
|
|
|
|
beautified_citations = "# Citations/Sources\n\n" |
|
for i, citation in enumerate(citations, start=1): |
|
beautified_citations += f"{i}. [{citation}]({citation})\n" |
|
|
|
|
|
yield beautified_content, beautified_citations |
|
except httpx.TimeoutException: |
|
yield "# Request Timeout\n\nRequest timed out. Please try again later.", "" |
|
except httpx.HTTPStatusError as e: |
|
yield f"# HTTP Error\n\nHTTP error occurred: {e}", "" |
|
except Exception as e: |
|
yield f"# Error\n\nAn error occurred: {e}", "" |
|
|
|
|
|
with gr.Blocks(css=".gradio-container { background-color: #f5f5f5; padding: 20px; border-radius: 10px; }", theme=gr.themes.Citrus()) as demo: |
|
gr.Markdown("# Web Search Application") |
|
|
|
with gr.Row(): |
|
with gr.Column( |
|
render=True, |
|
show_progress=True |
|
): |
|
query = gr.Textbox( |
|
label="Enter your query", |
|
placeholder="Type your search query here...", |
|
lines=2, |
|
max_lines=20, |
|
value="", |
|
elem_id="query-input" |
|
) |
|
website = gr.Textbox( |
|
label="Single website search", |
|
placeholder="website url", |
|
lines=1, |
|
max_lines=20, |
|
value="", |
|
elem_id="query-input" |
|
) |
|
after_date = gr.Textbox( |
|
label="After date", |
|
placeholder="date format: YYYY-MM-DD", |
|
lines=1, |
|
max_lines=2, |
|
value="", |
|
elem_id="query-input" |
|
) |
|
submit_button = gr.Button("Search") |
|
|
|
with gr.Column( |
|
render=True, |
|
show_progress=True |
|
): |
|
output_content = gr.Markdown( |
|
label="Response Content", |
|
value="", |
|
elem_id="response-content", |
|
height="600px", |
|
visible=True, |
|
show_label=True |
|
|
|
) |
|
output_citations = gr.Markdown( |
|
label="Citations", |
|
value="", |
|
elem_id="response-citations", |
|
height="200px", |
|
visible=True, |
|
show_label=True |
|
) |
|
|
|
|
|
submit_button.click(query_api, inputs=[query, website, after_date], outputs=[output_content, output_citations]) |
|
|
|
gr.Markdown("Powered by FastAPI and Gradio") |
|
|
|
|
|
demo.launch() |