SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,55 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from tavily import TavilyClient
|
| 3 |
|
| 4 |
-
# Step 1:
|
| 5 |
-
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"] #
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Step 2: Fetch Suggestions Using Tavily
|
| 9 |
-
def fetch_tavily_suggestions(location, need):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return f"Error: {str(e)}"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
- Affordable internet solutions.
|
| 27 |
-
- Signal boosting tips.
|
| 28 |
-
- Offline tools for education, healthcare, and business.
|
| 29 |
-
""")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
location = st.sidebar.text_input("Enter Your Location (e.g., City, District)")
|
| 34 |
-
needs = st.sidebar.selectbox("What do you need?", ["Internet Provider", "Signal Boosting Tips", "Offline Tools"])
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from tavily import TavilyClient
|
| 3 |
|
| 4 |
+
# Step 1: Retrieve Tavily API Key from Hugging Face Secrets
|
| 5 |
+
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"] # Access the API key securely from Hugging Face Secrets
|
| 6 |
+
if TAVILY_API_KEY is None:
|
| 7 |
+
st.error("API key is missing. Please add the TAVILY_API_KEY in Hugging Face Secrets.")
|
| 8 |
+
else:
|
| 9 |
+
# Initialize Tavily Client with the API key
|
| 10 |
+
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 11 |
|
| 12 |
+
# Step 2: Fetch Suggestions Using Tavily
|
| 13 |
+
def fetch_tavily_suggestions(location, need):
|
| 14 |
+
"""
|
| 15 |
+
Use Tavily API to fetch real-time suggestions based on location and need.
|
| 16 |
+
"""
|
| 17 |
+
query = f"Provide affordable internet providers or satellite internet services for rural areas near {location}, especially for low-income families."
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
# Execute the query using TavilyClient
|
| 21 |
+
response = tavily_client.search(query)
|
| 22 |
+
st.write(f"Raw API Response: {response}") # Debug: print the raw response for inspection
|
|
|
|
| 23 |
|
| 24 |
+
# Check if the 'text' key exists in the response and return it
|
| 25 |
+
if "text" in response:
|
| 26 |
+
return response["text"]
|
| 27 |
+
else:
|
| 28 |
+
return "No suggestions available in the response."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Step 3: Build Streamlit UI
|
| 34 |
+
st.title("Rural Connectivity Advisor")
|
| 35 |
+
st.markdown("""
|
| 36 |
+
Welcome to the **Rural Connectivity Advisor**!
|
| 37 |
+
This app helps rural communities find:
|
| 38 |
+
- Affordable internet solutions.
|
| 39 |
+
- Signal boosting tips.
|
| 40 |
+
- Offline tools for education, healthcare, and business.
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
# Sidebar User Inputs
|
| 44 |
+
st.sidebar.header("Input Your Details")
|
| 45 |
+
location = st.sidebar.text_input("Enter Your Location (e.g., City, District)")
|
| 46 |
+
needs = st.sidebar.selectbox("What do you need?", ["Internet Provider", "Signal Boosting Tips", "Offline Tools"])
|
| 47 |
+
|
| 48 |
+
# Fetch and Display Suggestions
|
| 49 |
+
if st.sidebar.button("Get Suggestions"):
|
| 50 |
+
if location:
|
| 51 |
+
st.subheader(f"Suggestions for {needs} in {location}")
|
| 52 |
+
suggestions = fetch_tavily_suggestions(location, needs)
|
| 53 |
+
st.write(suggestions)
|
| 54 |
+
else:
|
| 55 |
+
st.warning("Please enter your location to get recommendations.")
|