Fixing Errors - WIP - Making improvements, content workflows

This commit is contained in:
ajaysi
2024-09-13 19:41:48 +05:30
parent ca8618a6a4
commit 52753901f1
11 changed files with 159 additions and 26 deletions

View File

@@ -4,7 +4,6 @@ import sys
import re
import json
from pathlib import Path
import streamlit as st
from datetime import datetime, timedelta
from pathlib import Path
from loguru import logger
@@ -93,7 +92,6 @@ def save_in_file(table_content):
try:
# Save the content to the file
with open(file_path, "a+", encoding="utf-8") as file:
st.write(table_content)
file.write(table_content)
file.write("\n" * 3) # Add three newlines at the end
logger.info(f"Search content saved to {file_path}")

View File

@@ -49,9 +49,9 @@ logger.add(
)
from .common_utils import save_in_file, cfg_search_param
from tenacity import retry, stop_after_attempt, wait_random_exponential
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def google_search(query):
"""
@@ -75,10 +75,12 @@ def google_search(query):
try:
logger.info("Trying Google search with Serper.dev: https://serper.dev/api-key")
search_result = perform_serperdev_google_search(query)
process_search_results(search_result)
return(search_result)
if search_result:
process_search_results(search_result)
return(search_result)
except Exception as err:
logger.error(f"Failed to do Google search with serper.dev: {err}")
logger.error(f"Failed Google search with serper.dev: {err}")
return None
# # Retry with BROWSERLESS API

View File

@@ -62,10 +62,12 @@ def do_google_serp_search(search_keywords):
try:
logger.info(f"Doing Google search for: {search_keywords}\n")
g_results = google_search(search_keywords)
g_titles = extract_info(g_results, 'titles')
return(g_results, g_titles)
if g_results:
g_titles = extract_info(g_results, 'titles')
return(g_results, g_titles)
except Exception as err:
logger.error(f"Failed to do Google Serpapi research: {err}")
logger.error(f"Failed to do Google SERP research: {err}")
return None
# Not failing, as tavily would do same and then GPT-V to search.

View File

@@ -36,7 +36,7 @@ from tabulate import tabulate
# Load environment variables from .env file
load_dotenv(Path('../../.env'))
from rich import print
import streamlit as st
# Configure logger
logger.remove()
logger.add(sys.stdout,
@@ -95,11 +95,37 @@ def get_tavilyai_results(keywords, max_results=5):
max_results=max_results)
print_result_table(tavily_search_result)
streamlit_display_results(tavily_search_result)
return(tavily_search_result)
except Exception as err:
logger.error(f"Failed to do Tavily Research: {err}")
def streamlit_display_results(output_data):
"""Display Tavily AI search results in Streamlit UI."""
# Prepare data for display
table_data = []
for item in output_data.get("results", []):
title = item.get("title", "")
snippet = item.get("content", "")
link = item.get("url", "")
table_data.append([title, snippet, link])
# Display the table in Streamlit
st.table(table_data)
# Display the 'answer' in Streamlit
answer = output_data.get("answer", "No answer available")
st.write(f"**The answer to your search query:** {answer}")
# Display follow-up questions if available
follow_up_questions = output_data.get("follow_up_questions", [])
if follow_up_questions:
st.write(f"**Follow-up questions for the query:** {output_data.get('query')}")
st.write(", ".join(follow_up_questions))
def print_result_table(output_data):
""" Pretty print the tavily AI search result. """
# Prepare data for tabulate