SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files123456456789101189101112131415161718191213141516171819202122232122232425262728292425262728293132333430313233343536373839404142434445464736373839404142434849505152535455import streamlit as stfrom tavily import TavilyClient
# Step 1: Initialize Tavily Client with API key from Streamlit secretsTAVILY_API_KEY = st.secrets["TAVILY_API_KEY"] # Retrieve API key securely from secretstavily_client = TavilyClient(api_key=TAVILY_API_KEY)# Step 1: Retrieve Tavily API Key from Hugging Face SecretsTAVILY_API_KEY = st.secrets["TAVILY_API_KEY"] # Access the API key securely from Hugging Face Secretsif TAVILY_API_KEY is None: st.error("API key is missing. Please add the TAVILY_API_KEY in Hugging Face Secrets.")else: # Initialize Tavily Client with the API key tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
# Step 2: Fetch Suggestions Using Tavilydef fetch_tavily_suggestions(location, need): """ Use Tavily API to fetch real-time suggestions based on location and need. """ query = f"Provide {need.lower()} recommendations for rural areas near {location}. Focus on affordability and accessibility." try: # Execute the query using TavilyClient response = tavily_client.search(query) return response.get("text", "No recommendations available.") # Extract the 'text' response except Exception as e: return f"Error: {str(e)}" # Step 2: Fetch Suggestions Using Tavily def fetch_tavily_suggestions(location, need): """ Use Tavily API to fetch real-time suggestions based on location and need. """ query = f"Provide affordable internet providers or satellite internet services for rural areas near {location}, especially for low-income families." try: # Execute the query using TavilyClient response = tavily_client.search(query) st.write(f"Raw API Response: {response}") # Debug: print the raw response for inspection
# Step 3: Build Streamlit UIst.title("Rural Connectivity Advisor")st.markdown("""Welcome to the **Rural Connectivity Advisor**! This app helps rural communities find:- Affordable internet solutions.- Signal boosting tips.- Offline tools for education, healthcare, and business.""") # Check if the 'text' key exists in the response and return it if "text" in response: return response["text"] else: return "No suggestions available in the response."
# Sidebar User Inputsst.sidebar.header("Input Your Details")location = st.sidebar.text_input("Enter Your Location (e.g., City, District)")needs = st.sidebar.selectbox("What do you need?", ["Internet Provider", "Signal Boosting Tips", "Offline Tools"]) except Exception as e: return f"Error: {str(e)}"
# Step 3: Build Streamlit UI st.title("Rural Connectivity Advisor") st.markdown(""" Welcome to the **Rural Connectivity Advisor**! This app helps rural communities find: - Affordable internet solutions. - Signal boosting tips. - Offline tools for education, healthcare, and business. """)
# Sidebar User Inputs st.sidebar.header("Input Your Details") location = st.sidebar.text_input("Enter Your Location (e.g., City, District)") needs = st.sidebar.selectbox("What do you need?", ["Internet Provider", "Signal Boosting Tips", "Offline Tools"])
# Fetch and Display Suggestionsif st.sidebar.button("Get Suggestions"): if location: st.subheader(f"Suggestions for {needs} in {location}") suggestions = fetch_tavily_suggestions(location, needs) st.write(suggestions) else: st.warning("Please enter your location to get recommendations.") # Fetch and Display Suggestions if st.sidebar.button("Get Suggestions"): if location: st.subheader(f"Suggestions for {needs} in {location}") suggestions = fetch_tavily_suggestions(location, needs) st.write(suggestions) else: st.warning("Please enter your location to get recommendations.")app.py+48-36