story illustrator and story video generator, AI web researcher fixes
This commit is contained in:
@@ -494,36 +494,89 @@ def do_web_research():
|
||||
with progress_col:
|
||||
progress_bar = st.progress(0)
|
||||
|
||||
def update_progress(message, progress=None, level="info"):
|
||||
"""Update progress bar and status display.
|
||||
|
||||
Args:
|
||||
message (str): The message to display
|
||||
progress (float, optional): Progress value between 0 and 100. Will be converted to 0.0-1.0
|
||||
level (str, optional): Message level (info, warning, error, success)
|
||||
"""
|
||||
if progress is not None:
|
||||
# Convert percentage to decimal (0.0-1.0)
|
||||
progress = float(progress) / 100.0
|
||||
# Ensure progress stays within bounds
|
||||
progress = max(0.0, min(1.0, progress))
|
||||
progress_bar.progress(progress)
|
||||
|
||||
if level == "error":
|
||||
status_display.error(f"🚫 {message}")
|
||||
elif level == "warning":
|
||||
status_display.warning(f"⚠️ {message}")
|
||||
elif level == "success":
|
||||
status_display.success(f"✨ {message}")
|
||||
else:
|
||||
status_display.info(f"🔄 {message}")
|
||||
logger.debug(f"Progress update [{level}]: {message}")
|
||||
|
||||
# Execute search with all parameters
|
||||
web_research_result = gpt_web_researcher(
|
||||
search_keywords=st.session_state.research_options["primary_keywords"],
|
||||
search_mode=st.session_state.research_options["search_mode"],
|
||||
related_keywords=st.session_state.research_options["related_keywords"],
|
||||
target_audience=st.session_state.research_options["target_audience"],
|
||||
content_type=st.session_state.research_options["content_type"],
|
||||
search_depth=st.session_state.research_options["search_depth"],
|
||||
geo_location=st.session_state.research_options["geo_location"],
|
||||
search_language=st.session_state.research_options["search_language"],
|
||||
num_results=st.session_state.research_options["num_results"],
|
||||
time_range=st.session_state.research_options["time_range"],
|
||||
include_domains=st.session_state.research_options["include_domains"],
|
||||
similar_url=st.session_state.research_options["similar_url"]
|
||||
)
|
||||
try:
|
||||
update_progress("Starting search...", 0.25)
|
||||
logger.info(f"Executing web research with mode: {st.session_state.research_options['search_mode']}")
|
||||
|
||||
# Create base parameters
|
||||
research_params = {
|
||||
"search_keywords": st.session_state.research_options["primary_keywords"],
|
||||
"search_mode": st.session_state.research_options["search_mode"],
|
||||
"related_keywords": st.session_state.research_options["related_keywords"],
|
||||
"target_audience": st.session_state.research_options["target_audience"],
|
||||
"content_type": st.session_state.research_options["content_type"],
|
||||
"search_depth": st.session_state.research_options["search_depth"],
|
||||
"geo_location": st.session_state.research_options["geo_location"],
|
||||
"search_language": st.session_state.research_options["search_language"],
|
||||
"num_results": st.session_state.research_options["num_results"],
|
||||
"time_range": st.session_state.research_options["time_range"],
|
||||
"include_domains": st.session_state.research_options["include_domains"],
|
||||
"similar_url": st.session_state.research_options["similar_url"]
|
||||
}
|
||||
|
||||
# Add UI-specific parameters
|
||||
research_params.update({
|
||||
"status_container": status_display,
|
||||
"update_progress": update_progress
|
||||
})
|
||||
|
||||
# For AI search mode, ensure search_keywords is passed correctly
|
||||
if st.session_state.research_options["search_mode"] == "ai":
|
||||
research_params["tavily_params"] = {
|
||||
"max_results": st.session_state.research_options["num_results"],
|
||||
"search_depth": "advanced" if st.session_state.research_options["search_depth"] > 2 else "basic",
|
||||
"time_range": st.session_state.research_options["time_range"],
|
||||
"include_domains": st.session_state.research_options["include_domains"].split(",") if st.session_state.research_options["include_domains"] else [""]
|
||||
}
|
||||
# Pass search_keywords as a positional argument
|
||||
research_params["tavily_search_keywords"] = st.session_state.research_options["primary_keywords"]
|
||||
|
||||
# Execute the research
|
||||
web_research_result = gpt_web_researcher(**research_params)
|
||||
|
||||
if web_research_result:
|
||||
status_display.success("✨ Research completed!")
|
||||
|
||||
# Display results in an organized way
|
||||
with st.expander("📊 Research Results", expanded=False):
|
||||
st.write(web_research_result)
|
||||
else:
|
||||
st.warning("No results found for your search")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Research failed: {str(e)}"
|
||||
logger.error(error_msg, exc_info=True)
|
||||
st.error(f"🚫 Research failed: {error_msg}")
|
||||
|
||||
if web_research_result:
|
||||
status_display.success("✨ Research completed!")
|
||||
|
||||
# Display results in an organized way
|
||||
with st.expander("📊 Research Results", expanded=False):
|
||||
st.write(web_research_result)
|
||||
else:
|
||||
st.warning("No results found for your search")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Research failed: {str(e)}"
|
||||
logger.error(error_msg, exc_info=True)
|
||||
st.error(f"🚫 Research failed: {error_msg}")
|
||||
|
||||
logger.error(f"Unexpected error in web research: {e}", exc_info=True)
|
||||
st.error("🚫 An unexpected error occurred. Please try again.")
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error in web research: {e}", exc_info=True)
|
||||
st.error("🚫 An unexpected error occurred. Please try again.")
|
||||
@@ -3,7 +3,22 @@ from lib.ai_web_researcher.metaphor_basic_neural_web_search import metaphor_find
|
||||
from datetime import datetime, timedelta
|
||||
import re
|
||||
import urllib.parse
|
||||
import logging
|
||||
|
||||
# Configure logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
# Create console handler if it doesn't exist
|
||||
if not logger.handlers:
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setLevel(logging.DEBUG)
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
console_handler.setFormatter(formatter)
|
||||
logger.addHandler(console_handler)
|
||||
|
||||
def is_valid_url(url):
|
||||
"""
|
||||
@@ -17,15 +32,20 @@ def is_valid_url(url):
|
||||
"""
|
||||
try:
|
||||
result = urllib.parse.urlparse(url)
|
||||
return all([result.scheme, result.netloc])
|
||||
except:
|
||||
is_valid = all([result.scheme, result.netloc])
|
||||
logger.debug(f"URL validation for {url}: {is_valid}")
|
||||
return is_valid
|
||||
except Exception as e:
|
||||
logger.error(f"URL validation error for {url}: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
def competitor_analysis():
|
||||
logger.info("Starting competitor analysis")
|
||||
|
||||
# Initialize session state for progress bar visibility
|
||||
if 'show_progress' not in st.session_state:
|
||||
st.session_state.show_progress = True
|
||||
logger.debug("Initialized show_progress session state")
|
||||
|
||||
st.title("Competitor Analysis")
|
||||
st.markdown("""**Use Cases:**
|
||||
@@ -44,6 +64,7 @@ def competitor_analysis():
|
||||
# Validate URL
|
||||
url_valid = is_valid_url(similar_url) if similar_url else False
|
||||
if similar_url and not url_valid:
|
||||
logger.warning(f"Invalid URL provided: {similar_url}")
|
||||
st.error("⚠️ Please enter a valid URL including http:// or https://")
|
||||
|
||||
# Usecase selection with improved help
|
||||
@@ -52,6 +73,7 @@ def competitor_analysis():
|
||||
["similar companies", "listicles", "Top tools", "alternative-to", "similar products", "similar websites"],
|
||||
help="Choose the type of analysis you want to perform"
|
||||
)
|
||||
logger.debug(f"Selected usecase: {usecase}")
|
||||
|
||||
# Default summary query based on usecase
|
||||
default_summary_queries = {
|
||||
@@ -65,6 +87,7 @@ def competitor_analysis():
|
||||
|
||||
# Advanced options using a modal dialog
|
||||
show_advanced = st.checkbox("Show Advanced Options", help="Configure additional search parameters")
|
||||
logger.debug(f"Advanced options shown: {show_advanced}")
|
||||
|
||||
# Initialize default values
|
||||
num_results = 5
|
||||
@@ -96,6 +119,7 @@ def competitor_analysis():
|
||||
|
||||
# Advanced options section
|
||||
if show_advanced:
|
||||
logger.debug("Processing advanced options")
|
||||
st.markdown("### 🔧 Advanced Search Options")
|
||||
|
||||
# Summary query with improved help in a card
|
||||
@@ -192,6 +216,9 @@ def competitor_analysis():
|
||||
if st.button("Analyze", disabled=not url_valid if similar_url else False):
|
||||
if similar_url and url_valid:
|
||||
try:
|
||||
logger.info(f"Starting analysis for URL: {similar_url}")
|
||||
logger.debug(f"Analysis parameters - Usecase: {usecase}, Num Results: {num_results}, Time Range: {time_range}")
|
||||
|
||||
# Create a progress container
|
||||
progress_container = st.empty()
|
||||
status_container = st.empty()
|
||||
@@ -201,11 +228,8 @@ def competitor_analysis():
|
||||
status_container.info(f"Starting analysis for the URL: {similar_url}")
|
||||
|
||||
# Create a progress bar
|
||||
progress_bar = progress_container.progress(0)
|
||||
|
||||
# Update progress and status
|
||||
progress_bar.progress(10)
|
||||
status_container.info("Initializing search parameters...")
|
||||
progress_bar = progress_container.progress(0.1)
|
||||
logger.debug("Initialized progress bar and status containers")
|
||||
|
||||
# Calculate date range based on selection
|
||||
start_date = None
|
||||
@@ -219,6 +243,7 @@ def competitor_analysis():
|
||||
start_date = end_date - timedelta(days=30)
|
||||
elif time_range == "Past Year":
|
||||
start_date = end_date - timedelta(days=365)
|
||||
logger.debug(f"Date range: {start_date} to {end_date}")
|
||||
|
||||
# Format dates for API if they exist
|
||||
start_published_date = start_date.strftime("%Y-%m-%dT%H:%M:%S.000Z") if start_date else None
|
||||
@@ -228,41 +253,48 @@ def competitor_analysis():
|
||||
summary_query_param = None
|
||||
if summary_query:
|
||||
summary_query_param = {"query": summary_query}
|
||||
logger.debug(f"Summary query: {summary_query}")
|
||||
|
||||
# Update progress
|
||||
progress_bar.progress(20)
|
||||
progress_bar.progress(0.2)
|
||||
status_container.info("Searching for similar content...")
|
||||
logger.info("Initiating similar content search")
|
||||
|
||||
# Call the metaphor_find_similar function with all parameters
|
||||
with st.spinner("Performing competitor analysis..."):
|
||||
# Update progress
|
||||
progress_bar.progress(30)
|
||||
status_container.info("Finding similar content...")
|
||||
|
||||
# Call the API
|
||||
df, search_response = metaphor_find_similar(
|
||||
similar_url=similar_url,
|
||||
usecase=usecase,
|
||||
num_results=num_results,
|
||||
start_published_date=start_published_date,
|
||||
end_published_date=end_published_date,
|
||||
include_domains=include_domains,
|
||||
exclude_domains=exclude_domains,
|
||||
include_text=include_text,
|
||||
exclude_text=exclude_text,
|
||||
summary_query=summary_query_param
|
||||
)
|
||||
|
||||
# Update progress
|
||||
progress_bar.progress(70)
|
||||
status_container.info("Processing and analyzing results...")
|
||||
|
||||
# Update progress to complete
|
||||
progress_bar.progress(100)
|
||||
status_container.success("Analysis completed successfully!")
|
||||
logger.debug("Calling metaphor_find_similar API")
|
||||
try:
|
||||
df, search_response = metaphor_find_similar(
|
||||
similar_url=similar_url,
|
||||
usecase=usecase,
|
||||
num_results=num_results,
|
||||
start_published_date=start_published_date,
|
||||
end_published_date=end_published_date,
|
||||
include_domains=include_domains,
|
||||
exclude_domains=exclude_domains,
|
||||
include_text=include_text,
|
||||
exclude_text=exclude_text,
|
||||
summary_query=summary_query_param
|
||||
)
|
||||
logger.info(f"API call successful. Found {len(df) if not df.empty else 0} results")
|
||||
|
||||
# Update progress
|
||||
progress_bar.progress(0.7)
|
||||
status_container.info("Processing and analyzing results...")
|
||||
logger.debug("Processing search results")
|
||||
|
||||
# Update progress to complete
|
||||
progress_bar.progress(1.0)
|
||||
status_container.success("Analysis completed successfully!")
|
||||
logger.info("Analysis completed successfully")
|
||||
|
||||
except Exception as api_error:
|
||||
logger.error(f"API call failed: {str(api_error)}", exc_info=True)
|
||||
raise
|
||||
|
||||
# Display results using data editor
|
||||
# Display results
|
||||
if not df.empty:
|
||||
logger.debug(f"Displaying {len(df)} results")
|
||||
st.subheader("📊 Competitor Analysis Results")
|
||||
|
||||
# Add a download button for the results
|
||||
@@ -331,8 +363,12 @@ def competitor_analysis():
|
||||
with st.expander("View Raw Data"):
|
||||
st.json(search_response)
|
||||
else:
|
||||
logger.warning("No results found for the given URL and parameters")
|
||||
st.warning("No results found for the given URL and parameters.")
|
||||
|
||||
except Exception as err:
|
||||
logger.error(f"Analysis failed: {str(err)}", exc_info=True)
|
||||
st.error(f"✖ 🚫 Failed to do similar search.\nError: {err}")
|
||||
else:
|
||||
logger.warning("Analysis attempted without valid URL")
|
||||
st.error("Please enter a valid URL.")
|
||||
Reference in New Issue
Block a user