SHAMIL SHAHBAZ AWAN commited on
Commit
9a762b6
·
verified ·
1 Parent(s): 0b76d11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -47
app.py CHANGED
@@ -1,9 +1,8 @@
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
@@ -12,46 +11,22 @@ if GROQ_API_KEY is None:
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)}"
53
 
54
- # Step 5: Build Streamlit UI
55
  st.title("Rural Connectivity Advisor")
56
  st.markdown("""
57
  Welcome to the **Rural Connectivity Advisor**!
@@ -69,23 +44,16 @@ else:
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:
91
  st.warning("Please enter your location to get recommendations.")
 
1
  import streamlit as st
 
2
  from groq import Groq
3
  import os
4
 
5
+ # Step 1: Retrieve API Key from Hugging Face Secrets
6
  GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
7
 
8
  # Step 2: Initialize Groq Client
 
11
  else:
12
  groq_client = Groq(api_key=GROQ_API_KEY)
13
 
14
+ # Step 3: Enhance Suggestions Using Groq
15
+ def enhance_with_groq(query):
16
  """
17
+ Enhance the suggestions using Groq.
18
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  try:
20
  # Perform chat completion using Groq
21
  chat_completion = groq_client.chat.completions.create(
22
+ messages=[{"role": "user", "content": query}],
23
  model="llama-3.3-70b-versatile", # Specify model for Groq
24
  )
25
  return chat_completion.choices[0].message.content
26
  except Exception as e:
27
  return f"Error with Groq API: {str(e)}"
28
 
29
+ # Step 4: Build Streamlit UI
30
  st.title("Rural Connectivity Advisor")
31
  st.markdown("""
32
  Welcome to the **Rural Connectivity Advisor**!
 
44
  # Fetch and Enhance Suggestions
45
  if st.sidebar.button("Get Suggestions"):
46
  if location:
47
+ # Formulate the query for Groq based on user inputs
48
+ query = f"Provide suggestions for {needs} in {location}, especially affordable options and tips."
49
+
 
50
  st.subheader(f"Suggestions for {needs} in {location}")
51
+
52
+ # Get response from Groq
53
+ groq_response = enhance_with_groq(query)
54
 
55
+ # Display Groq response
56
+ st.write("Suggestions from Groq:")
57
+ st.write(groq_response)
 
 
 
 
 
 
 
58
  else:
59
  st.warning("Please enter your location to get recommendations.")