SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
-
import requests
|
| 4 |
from tavily import TavilyClient
|
| 5 |
from groq import Groq
|
| 6 |
|
|
@@ -8,41 +7,41 @@ from groq import Groq
|
|
| 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(
|
| 20 |
"""
|
| 21 |
-
Use Tavily API to fetch real-time suggestions based on
|
| 22 |
"""
|
| 23 |
-
query = f"Provide internet connectivity options for rural areas near {location}, especially affordable providers."
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
# Execute the query using TavilyClient
|
| 27 |
response = tavily_client.search(query)
|
| 28 |
-
return response.get("text",
|
| 29 |
except Exception as e:
|
| 30 |
-
return
|
| 31 |
|
| 32 |
# Step 4: Enhance Suggestions Using Groq
|
| 33 |
-
def enhance_with_groq(suggestions):
|
| 34 |
"""
|
| 35 |
-
|
|
|
|
| 36 |
"""
|
| 37 |
-
if suggestions
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
# Perform chat completion using Groq for better formatting and response enhancement
|
| 44 |
chat_completion = groq_client.chat.completions.create(
|
| 45 |
-
messages=[{"role": "user", "content":
|
| 46 |
model="llama-3.3-70b-versatile", # Specify model for Groq
|
| 47 |
)
|
| 48 |
# Extract and return the enhanced message
|
|
@@ -68,18 +67,24 @@ else:
|
|
| 68 |
# Fetch and Enhance Suggestions
|
| 69 |
if st.sidebar.button("Get Suggestions"):
|
| 70 |
if location:
|
|
|
|
| 71 |
st.subheader(f"Suggestions for {needs} in {location}")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
# Enhance the response using Groq
|
| 79 |
-
enhanced_suggestions = enhance_with_groq(tavily_suggestions)
|
| 80 |
-
st.subheader("Enhanced Suggestions:")
|
| 81 |
st.write(enhanced_suggestions)
|
| 82 |
else:
|
| 83 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
st.warning("Please enter your location to get recommendations.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
| 3 |
from tavily import TavilyClient
|
| 4 |
from groq import Groq
|
| 5 |
|
|
|
|
| 7 |
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"]
|
| 8 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
| 9 |
|
| 10 |
+
# Step 2: Initialize Clients for Tavily and Groq
|
| 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 |
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
|
| 15 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 16 |
|
| 17 |
# Step 3: Fetch Suggestions Using Tavily
|
| 18 |
+
def fetch_tavily_suggestions(query):
|
| 19 |
"""
|
| 20 |
+
Use Tavily API to fetch real-time suggestions based on the query.
|
| 21 |
"""
|
|
|
|
|
|
|
| 22 |
try:
|
|
|
|
| 23 |
response = tavily_client.search(query)
|
| 24 |
+
return response.get("text", None) # Return None if no suggestions are available
|
| 25 |
except Exception as e:
|
| 26 |
+
return None # Return None in case of error
|
| 27 |
|
| 28 |
# Step 4: Enhance Suggestions Using Groq
|
| 29 |
+
def enhance_with_groq(query, suggestions=None):
|
| 30 |
"""
|
| 31 |
+
If suggestions are provided by Tavily, enhance them using Groq.
|
| 32 |
+
Otherwise, generate a response using Groq based on the original query.
|
| 33 |
"""
|
| 34 |
+
if suggestions is None:
|
| 35 |
+
# If no suggestions from Tavily, generate from Groq directly
|
| 36 |
+
response_content = query
|
| 37 |
+
else:
|
| 38 |
+
# If suggestions from Tavily exist, use them as context
|
| 39 |
+
response_content = suggestions
|
| 40 |
+
|
| 41 |
try:
|
| 42 |
# Perform chat completion using Groq for better formatting and response enhancement
|
| 43 |
chat_completion = groq_client.chat.completions.create(
|
| 44 |
+
messages=[{"role": "user", "content": response_content}],
|
| 45 |
model="llama-3.3-70b-versatile", # Specify model for Groq
|
| 46 |
)
|
| 47 |
# Extract and return the enhanced message
|
|
|
|
| 67 |
# Fetch and Enhance Suggestions
|
| 68 |
if st.sidebar.button("Get Suggestions"):
|
| 69 |
if location:
|
| 70 |
+
query = f"Provide internet connectivity options for rural areas near {location}, especially affordable providers."
|
| 71 |
st.subheader(f"Suggestions for {needs} in {location}")
|
| 72 |
+
|
| 73 |
+
# Get suggestions from Tavily
|
| 74 |
+
tavily_suggestions = fetch_tavily_suggestions(query)
|
| 75 |
|
| 76 |
+
if tavily_suggestions:
|
| 77 |
+
st.write("Raw Suggestions from Tavily:")
|
| 78 |
+
st.write(tavily_suggestions)
|
| 79 |
# Enhance the response using Groq
|
| 80 |
+
enhanced_suggestions = enhance_with_groq(query, tavily_suggestions)
|
| 81 |
+
st.subheader("Enhanced Suggestions from Groq:")
|
| 82 |
st.write(enhanced_suggestions)
|
| 83 |
else:
|
| 84 |
+
st.write("No suggestions available from Tavily. Generating response from Groq...")
|
| 85 |
+
# If Tavily doesn't provide any suggestions, generate a response from Groq
|
| 86 |
+
groq_response = enhance_with_groq(query)
|
| 87 |
+
st.subheader("Generated Response from Groq:")
|
| 88 |
+
st.write(groq_response)
|
| 89 |
else:
|
| 90 |
st.warning("Please enter your location to get recommendations.")
|