SHAMIL SHAHBAZ AWAN commited on
Commit
936114c
·
verified ·
1 Parent(s): 6d1e3c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -33
app.py CHANGED
@@ -1,50 +1,52 @@
1
  import streamlit as st
2
- import os
3
- from tavily import TavilyClient
4
  from groq import Groq
 
5
 
6
  # Step 1: Retrieve API Keys from Hugging Face Secrets
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
48
  return chat_completion.choices[0].message.content
49
  except Exception as e:
50
  return f"Error with Groq API: {str(e)}"
@@ -67,23 +69,22 @@ else:
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:
 
1
  import streamlit as st
2
+ import wikipedia
 
3
  from groq import Groq
4
+ import os
5
 
6
  # Step 1: Retrieve API Keys from Hugging Face Secrets
 
7
  GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
8
 
9
+ # Step 2: Initialize Groq Client
10
+ if GROQ_API_KEY is None:
11
+ st.error("Groq API key is missing. Please add the API key in Hugging Face Secrets.")
12
  else:
 
13
  groq_client = Groq(api_key=GROQ_API_KEY)
14
 
15
+ # Step 3: Wikipedia Query Function
16
+ def get_wikipedia_response(query):
17
  """
18
+ Fetch information from Wikipedia based on the query.
19
  """
20
  try:
21
+ # Fetch a summary of the query from Wikipedia
22
+ result = wikipedia.summary(query, sentences=3)
23
+ return result
24
+ except wikipedia.exceptions.DisambiguationError as e:
25
+ return f"Ambiguous query, please clarify: {e.options}"
26
+ except wikipedia.exceptions.HTTPTimeoutError:
27
+ return "Error: Wikipedia request timed out. Please try again."
28
+ except wikipedia.exceptions.RedirectError:
29
+ return "Error: Redirect error while fetching Wikipedia data."
30
  except Exception as e:
31
+ return f"An unexpected error occurred: {str(e)}"
32
 
33
+ # Step 4: Enhance with Groq (if needed)
34
  def enhance_with_groq(query, suggestions=None):
35
  """
36
+ Enhance suggestions using Groq or generate from scratch.
 
37
  """
38
  if suggestions is None:
39
+ # If no suggestions, use Groq to generate an enhancement
40
  response_content = query
41
  else:
 
42
  response_content = suggestions
43
 
44
  try:
45
+ # Perform chat completion using Groq
46
  chat_completion = groq_client.chat.completions.create(
47
  messages=[{"role": "user", "content": response_content}],
48
  model="llama-3.3-70b-versatile", # Specify model for Groq
49
  )
 
50
  return chat_completion.choices[0].message.content
51
  except Exception as e:
52
  return f"Error with Groq API: {str(e)}"
 
69
  # Fetch and Enhance Suggestions
70
  if st.sidebar.button("Get Suggestions"):
71
  if location:
72
+ # Query Wikipedia first for general information about the location
73
+ wikipedia_query = f"Internet providers in {location}"
74
+ wikipedia_response = get_wikipedia_response(wikipedia_query)
75
+
76
  st.subheader(f"Suggestions for {needs} in {location}")
77
+ st.write("Raw Suggestions from Wikipedia:")
78
+ st.write(wikipedia_response)
 
79
 
80
+ # If Wikipedia response is not enough, use Groq to enhance
81
+ if wikipedia_response:
82
+ enhanced_response = enhance_with_groq(wikipedia_query, wikipedia_response)
 
 
83
  st.subheader("Enhanced Suggestions from Groq:")
84
+ st.write(enhanced_response)
85
  else:
86
+ # If no meaningful Wikipedia results, fallback to Groq
87
+ groq_response = enhance_with_groq(wikipedia_query)
 
88
  st.subheader("Generated Response from Groq:")
89
  st.write(groq_response)
90
  else: