Hady Rashwan commited on
Commit
5b4c4b1
·
1 Parent(s): 4e65b85

adding initial code for quote feature

Browse files
Files changed (3) hide show
  1. .env.example +3 -1
  2. app.py +37 -2
  3. matching_documents.sql +21 -0
.env.example CHANGED
@@ -1,2 +1,4 @@
1
  SUPABASE_URL=supabase_url
2
- SUPABASE_KEY=supabase_key
 
 
 
1
  SUPABASE_URL=supabase_url
2
+ SUPABASE_KEY=supabase_key
3
+ OPENWEATHERMAP_API_KEY=weather_api_key
4
+ HUGGINGFACE_API_KEY=hugging_face_api_key
app.py CHANGED
@@ -1,19 +1,34 @@
1
  import streamlit as st
2
  import requests
3
  import datetime
 
4
  from huggingface_hub import InferenceClient
 
 
5
  from dotenv import load_dotenv
6
 
7
  # Load environment variables
8
  load_dotenv()
9
 
10
  # Get API keys from environment variables
11
- WEATHER_API_KEY = st.secrets["OPENWEATHERMAP_API_KEY"]
12
- HF_API_KEY = st.secrets["HUGGINGFACE_API_KEY"]
 
 
 
 
 
 
 
13
 
14
  # Initialize the Hugging Face Inference Client
15
  client = InferenceClient(token=HF_API_KEY)
16
 
 
 
 
 
 
17
  def get_weather(city):
18
  base_url = "http://api.openweathermap.org/data/2.5/weather"
19
  params = {
@@ -48,6 +63,22 @@ def get_ai_clothing_suggestion(weather_data):
48
 
49
  return response
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  st.title("AI-Powered Weather and Clothing Suggestion App")
52
 
53
  city = st.text_input("Enter a city name:", "London")
@@ -72,6 +103,10 @@ if st.button("Get Weather and Clothing Suggestion"):
72
  clothing_suggestion = get_ai_clothing_suggestion(weather_data)
73
  st.subheader("What to Wear (AI Suggestion):")
74
  st.write(clothing_suggestion)
 
 
 
 
75
  else:
76
  st.error("City not found. Please check the spelling and try again.")
77
 
 
1
  import streamlit as st
2
  import requests
3
  import datetime
4
+ import os
5
  from huggingface_hub import InferenceClient
6
+ from sentence_transformers import SentenceTransformer
7
+ from supabase import create_client, Client
8
  from dotenv import load_dotenv
9
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
  # Get API keys from environment variables
14
+ WEATHER_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY")
15
+ HF_API_KEY = os.getenv("HUGGINGFACE_API_KEY",)
16
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
17
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
18
+
19
+ # WEATHER_API_KEY = os.getenv("OPENWEATHERMAP_API_KEY",st.secrets["OPENWEATHERMAP_API_KEY"])
20
+ # HF_API_KEY = os.getenv("HUGGINGFACE_API_KEY",st.secrets["HUGGINGFACE_API_KEY"])
21
+ # SUPABASE_URL = os.getenv("SUPABASE_URL",st.secrets["SUPABASE_URL"])
22
+ # SUPABASE_KEY = os.getenv("SUPABASE_KEY",st.secrets["SUPABASE_KEY"])
23
 
24
  # Initialize the Hugging Face Inference Client
25
  client = InferenceClient(token=HF_API_KEY)
26
 
27
+ # Initialize Supabase
28
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
29
+
30
+ model = SentenceTransformer('all-MiniLM-L6-v2')
31
+
32
  def get_weather(city):
33
  base_url = "http://api.openweathermap.org/data/2.5/weather"
34
  params = {
 
63
 
64
  return response
65
 
66
+ def get_relevant_quote(weather_condition):
67
+ # Encode the weather condition
68
+ weather_embedding = model.encode(weather_condition).tolist()
69
+
70
+ response = supabase.rpc("match_quote_embeddings",{
71
+ 'query_embedding': weather_embedding,
72
+ 'match_threshold': 0.5,
73
+ 'match_count': 1
74
+ }).execute()
75
+
76
+
77
+ if response.data and len(response.data) > 0:
78
+ return response.data[0]['content']
79
+ else:
80
+ return "No relevant quote found."
81
+
82
  st.title("AI-Powered Weather and Clothing Suggestion App")
83
 
84
  city = st.text_input("Enter a city name:", "London")
 
103
  clothing_suggestion = get_ai_clothing_suggestion(weather_data)
104
  st.subheader("What to Wear (AI Suggestion):")
105
  st.write(clothing_suggestion)
106
+ with st.spinner("Finding a relevant quote..."):
107
+ quote = get_relevant_quote(f"{main_weather} {description}")
108
+ st.subheader("Quote of the Day:")
109
+ st.write(quote)
110
  else:
111
  st.error("City not found. Please check the spelling and try again.")
112
 
matching_documents.sql ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ create or replace function match_handbook_docs (
2
+ query_embedding vector(1024),
3
+ match_threshold float,
4
+ match_count int
5
+ )
6
+ returns table (
7
+ id bigint,
8
+ content text,
9
+ similarity float
10
+ )
11
+ language sql stable
12
+ as $$
13
+ select
14
+ handbook_docs.id,
15
+ handbook_docs.content,
16
+ 1 - (handbook_docs.embedding <=> query_embedding) as similarity
17
+ from handbook_docs
18
+ where 1 - (handbook_docs.embedding <=> query_embedding) > match_threshold
19
+ order by (handbook_docs.embedding <=> query_embedding) asc
20
+ limit match_count;
21
+ $$;