Izza-shahzad-13 commited on
Commit
08eb120
1 Parent(s): 7625568

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import streamlit as st
2
  import os
3
  import requests
 
 
 
4
 
5
- # Ensure that the Groq API key is set
6
  os.environ["GROQ_API_KEY"] = "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"
7
 
8
  # Groq API endpoint
@@ -33,14 +36,13 @@ def generate_response(context):
33
  return response.json()["text"]
34
 
35
  # Load the counseling conversations dataset
36
- from datasets import load_dataset
37
  dataset = load_dataset("Amod/mental_health_counseling_conversations")["train"]
38
 
39
  # Precompute embeddings for the dataset responses using Groq API
40
- @st.cache(allow_output_mutation=True)
41
- def embed_dataset(dataset):
42
  embeddings = []
43
- for entry in dataset:
44
  embedding = retrieve_embedding(entry["response"])
45
  embeddings.append(embedding)
46
  return embeddings
@@ -48,9 +50,6 @@ def embed_dataset(dataset):
48
  dataset_embeddings = embed_dataset(dataset)
49
 
50
  # Function to retrieve closest responses from the dataset using cosine similarity
51
- from sklearn.metrics.pairwise import cosine_similarity
52
- import numpy as np
53
-
54
  def retrieve_response(user_query, dataset, dataset_embeddings, k=5):
55
  query_embedding = retrieve_embedding(user_query)
56
  cos_scores = cosine_similarity([query_embedding], dataset_embeddings)[0]
 
1
  import streamlit as st
2
  import os
3
  import requests
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ import numpy as np
6
+ from datasets import load_dataset
7
 
8
+ # Groq API key setup
9
  os.environ["GROQ_API_KEY"] = "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"
10
 
11
  # Groq API endpoint
 
36
  return response.json()["text"]
37
 
38
  # Load the counseling conversations dataset
 
39
  dataset = load_dataset("Amod/mental_health_counseling_conversations")["train"]
40
 
41
  # Precompute embeddings for the dataset responses using Groq API
42
+ @st.cache_resource
43
+ def embed_dataset(_dataset):
44
  embeddings = []
45
+ for entry in _dataset:
46
  embedding = retrieve_embedding(entry["response"])
47
  embeddings.append(embedding)
48
  return embeddings
 
50
  dataset_embeddings = embed_dataset(dataset)
51
 
52
  # Function to retrieve closest responses from the dataset using cosine similarity
 
 
 
53
  def retrieve_response(user_query, dataset, dataset_embeddings, k=5):
54
  query_embedding = retrieve_embedding(user_query)
55
  cos_scores = cosine_similarity([query_embedding], dataset_embeddings)[0]