SHAMIL SHAHBAZ AWAN commited on
Commit
f1778c0
·
verified ·
1 Parent(s): 434b4e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -1,43 +1,55 @@
1
  import streamlit as st
2
  from tavily import TavilyClient
3
 
4
- # Step 1: Initialize Tavily Client with API key from Streamlit secrets
5
- TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"] # Retrieve API key securely from secrets
6
- tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
 
 
 
 
7
 
8
- # Step 2: Fetch Suggestions Using Tavily
9
- def fetch_tavily_suggestions(location, need):
10
- """
11
- Use Tavily API to fetch real-time suggestions based on location and need.
12
- """
13
- query = f"Provide {need.lower()} recommendations for rural areas near {location}. Focus on affordability and accessibility."
14
- try:
15
- # Execute the query using TavilyClient
16
- response = tavily_client.search(query)
17
- return response.get("text", "No recommendations available.") # Extract the 'text' response
18
- except Exception as e:
19
- return f"Error: {str(e)}"
20
 
21
- # Step 3: Build Streamlit UI
22
- st.title("Rural Connectivity Advisor")
23
- st.markdown("""
24
- Welcome to the **Rural Connectivity Advisor**!
25
- This app helps rural communities find:
26
- - Affordable internet solutions.
27
- - Signal boosting tips.
28
- - Offline tools for education, healthcare, and business.
29
- """)
30
 
31
- # Sidebar User Inputs
32
- st.sidebar.header("Input Your Details")
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
- # Fetch and Display Suggestions
37
- if st.sidebar.button("Get Suggestions"):
38
- if location:
39
- st.subheader(f"Suggestions for {needs} in {location}")
40
- suggestions = fetch_tavily_suggestions(location, needs)
41
- st.write(suggestions)
42
- else:
43
- st.warning("Please enter your location to get recommendations.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")