Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from api_logic import fetch_keyword_ideas, rank_and_filter_keywords, KeywordRequestModel | |
| # ----------------------------- | |
| # Display functions | |
| # ----------------------------- | |
| def display_keyword_results(data: dict): | |
| """ | |
| Display keyword ideas returned from fetch_keyword_ideas() | |
| Parameters | |
| ---------- | |
| data : dict | |
| { | |
| "results": [...], | |
| "aggregateMetricResults": {...} | |
| } | |
| """ | |
| # When no results are found | |
| if not data or not data.get("results"): | |
| st.warning("No keyword ideas found.") | |
| return | |
| results = data["results"] | |
| st.subheader("π Keyword Ideas Overview") | |
| # ------- 1 Display main metrics as a table ------- | |
| df_main = pd.DataFrame([ | |
| { | |
| "Keyword": r["keyword"], | |
| "Avg Monthly Searches": r["avg_monthly_searches"], | |
| "Competition": r["competition"], | |
| "Competition Index": r["competition_index"], | |
| "Low Bid (micros)": r["low_top_of_page_bid_micros"], | |
| "High Bid (micros)": r["high_top_of_page_bid_micros"], | |
| #"Avg CPC (micros)": r["average_cpc_micros"], | |
| ## Added Features | |
| "Log CPC": r["log_cpc"], | |
| "Normalized CPC": r["log_cpc_norm_0_100"], | |
| } | |
| for r in results | |
| ]) | |
| st.dataframe(df_main, use_container_width=True) | |
| # ------- 2 Monthly search volumes (inside expanders) ------- | |
| st.subheader("π Monthly Search Volumes") | |
| for r in results: | |
| with st.expander(f"{r['keyword']} - Monthly Trends"): | |
| mv = r.get("monthly_search_volumes", []) | |
| if mv: | |
| df_mv = pd.DataFrame(mv) | |
| df_mv = df_mv.rename(columns={ | |
| "month": "Month", | |
| "year": "Year", | |
| "monthly_searches": "Monthly Searches" | |
| }) | |
| st.dataframe(df_mv, use_container_width=True) | |
| else: | |
| st.write("No monthly search volume data available.") | |
| # ------- 3 Aggregate metrics (device-based searches) ------- | |
| st.subheader("π± Aggregate Metric Results") | |
| agg = data.get("aggregateMetricResults", {}) | |
| device_searches = agg.get("device_searches", []) | |
| if device_searches: | |
| df_device = pd.DataFrame(device_searches) | |
| st.dataframe(df_device, use_container_width=True) | |
| else: | |
| st.write("No aggregate device metrics.") | |
| # ----------------------------- | |
| # Streamlit UI | |
| # ----------------------------- | |
| def main(): | |
| st.title("π Google Ads Keyword Ideas ToolV007") | |
| with st.expander("π οΈ Keyword Planner Demo β Development Progress (click to view)"): | |
| st.markdown(""" | |
| 1. Obtain Google Ads API authentication token | |
| 2. Implement `fetch_keyword_ideas` in `api_logic.py` | |
| This function communicates with the Google Ads API and retrieves keyword ideas. | |
| 3. Implement methodology to sort data **β οΈ(currently in progress)β οΈ** requires 1 day more. | |
| - Methodology is taken from **["Rule Book For GKP With Code"]** | |
| 3.1 Volume Search Section | |
| 3.2 Competition Score Section | |
| 3.3 CPC Score Section **(Completed)** | |
| 3.4 Intent Section | |
| 3.5 Trend Section | |
| 4. Implement `app.py` | |
| Integrate the results into the Streamlit UI and deploy | |
| **Hugging Face Link will be shared soon ** | |
| """) | |
| st.markdown("Enter keyword(s) and your Google Ads settings to fetch keyword ideas.") | |
| # Input UI | |
| customer_id = st.text_input("Customer ID", value="") | |
| keywords = st.text_input("Keywords (comma separated)", value="") | |
| geo_target = st.text_input("Geo Target ID (optional)", value="2840") | |
| language = st.text_input("Language ID (optional)", value="1000") | |
| st.write("---") | |
| if st.button("π Get Keyword Ideas"): | |
| if not customer_id or not keywords: | |
| st.error("Please enter Customer ID and keywords.") | |
| return | |
| keyword_list = [k.strip() for k in keywords.split(",")] | |
| # Build KeywordRequestModel | |
| request_model = KeywordRequestModel( | |
| customer_id=customer_id, | |
| keywords=keyword_list, | |
| geo_target=geo_target if geo_target else None, | |
| language=language if language else None, | |
| ) | |
| with st.spinner("Fetching keyword ideas..."): | |
| response = fetch_keyword_ideas(request_model) | |
| # Filter top 20 items | |
| top20 = rank_and_filter_keywords(response, limit=20) | |
| # Build display-friendly response structure | |
| filtered_response = { | |
| "results": top20, | |
| "aggregateMetricResults": response.get("aggregateMetricResults") | |
| } | |
| st.success("β Completed") | |
| display_keyword_results(filtered_response) | |
| # Run application | |
| if __name__ == "__main__": | |
| main() | |