SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,21 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from tavily import TavilyClient
|
|
|
|
| 3 |
|
| 4 |
-
# Step 1: Retrieve
|
| 5 |
-
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"]
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
else:
|
| 9 |
-
# Initialize
|
| 10 |
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
|
|
|
| 11 |
|
| 12 |
-
# Step
|
| 13 |
def fetch_tavily_suggestions(location, need):
|
| 14 |
"""
|
| 15 |
Use Tavily API to fetch real-time suggestions based on location and need.
|
|
@@ -19,18 +25,29 @@ else:
|
|
| 19 |
try:
|
| 20 |
# Execute the query using TavilyClient
|
| 21 |
response = tavily_client.search(query)
|
| 22 |
-
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
st.title("Rural Connectivity Advisor")
|
| 35 |
st.markdown("""
|
| 36 |
Welcome to the **Rural Connectivity Advisor**!
|
|
@@ -45,11 +62,17 @@ else:
|
|
| 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
|
| 49 |
if st.sidebar.button("Get Suggestions"):
|
| 50 |
if location:
|
| 51 |
st.subheader(f"Suggestions for {needs} in {location}")
|
| 52 |
-
|
| 53 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
st.warning("Please enter your location to get recommendations.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
from tavily import TavilyClient
|
| 5 |
+
from groq import Groq
|
| 6 |
|
| 7 |
+
# Step 1: Retrieve API Keys from Hugging Face Secrets
|
| 8 |
+
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"]
|
| 9 |
+
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
| 10 |
+
|
| 11 |
+
if TAVILY_API_KEY is None or GROQ_API_KEY is None:
|
| 12 |
+
st.error("API key(s) are missing. Please add the API keys in Hugging Face Secrets.")
|
| 13 |
else:
|
| 14 |
+
# Step 2: Initialize Clients for Tavily and Groq
|
| 15 |
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 16 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
+
# Step 3: Fetch Suggestions Using Tavily
|
| 19 |
def fetch_tavily_suggestions(location, need):
|
| 20 |
"""
|
| 21 |
Use Tavily API to fetch real-time suggestions based on location and need.
|
|
|
|
| 25 |
try:
|
| 26 |
# Execute the query using TavilyClient
|
| 27 |
response = tavily_client.search(query)
|
| 28 |
+
return response.get("text", "No suggestions available.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
except Exception as e:
|
| 30 |
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
+
# Step 4: Enhance Suggestions Using Groq
|
| 33 |
+
def enhance_with_groq(suggestions):
|
| 34 |
+
"""
|
| 35 |
+
Use the Groq model to enhance and organize the suggestions.
|
| 36 |
+
"""
|
| 37 |
+
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
# Perform chat completion using Groq for better formatting and response enhancement
|
| 41 |
+
chat_completion = groq_client.chat.completions.create(
|
| 42 |
+
messages=[{"role": "user", "content": suggestions}],
|
| 43 |
+
model="llama-3.3-70b-versatile", # Specify model for Groq
|
| 44 |
+
)
|
| 45 |
+
# Extract and return the enhanced message
|
| 46 |
+
return chat_completion.choices[0].message.content
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error with Groq API: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# Step 5: Build Streamlit UI
|
| 51 |
st.title("Rural Connectivity Advisor")
|
| 52 |
st.markdown("""
|
| 53 |
Welcome to the **Rural Connectivity Advisor**!
|
|
|
|
| 62 |
location = st.sidebar.text_input("Enter Your Location (e.g., City, District)")
|
| 63 |
needs = st.sidebar.selectbox("What do you need?", ["Internet Provider", "Signal Boosting Tips", "Offline Tools"])
|
| 64 |
|
| 65 |
+
# Fetch and Enhance Suggestions
|
| 66 |
if st.sidebar.button("Get Suggestions"):
|
| 67 |
if location:
|
| 68 |
st.subheader(f"Suggestions for {needs} in {location}")
|
| 69 |
+
tavily_suggestions = fetch_tavily_suggestions(location, needs)
|
| 70 |
+
st.write("Raw Suggestions from Tavily:")
|
| 71 |
+
st.write(tavily_suggestions)
|
| 72 |
+
|
| 73 |
+
# Enhance the response using Groq
|
| 74 |
+
enhanced_suggestions = enhance_with_groq(tavily_suggestions)
|
| 75 |
+
st.subheader("Enhanced Suggestions:")
|
| 76 |
+
st.write(enhanced_suggestions)
|
| 77 |
else:
|
| 78 |
st.warning("Please enter your location to get recommendations.")
|