Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 9 |
+
GROQ_API_URL = "https://api.groq.com/v1/inference"
|
| 10 |
+
|
| 11 |
+
# Function to perform embedding retrieval using MiniLM via Groq API
|
| 12 |
+
def retrieve_embedding(user_query):
|
| 13 |
+
payload = {
|
| 14 |
+
"model": "microsoft/MiniLM-L6-H384-uncased",
|
| 15 |
+
"input_text": user_query
|
| 16 |
+
}
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}"
|
| 19 |
+
}
|
| 20 |
+
response = requests.post(f"{GROQ_API_URL}/embedding", json=payload, headers=headers)
|
| 21 |
+
return response.json()["embedding"]
|
| 22 |
+
|
| 23 |
+
# Function to perform response generation using FLAN-T5 via Groq API
|
| 24 |
+
def generate_response(context):
|
| 25 |
+
payload = {
|
| 26 |
+
"model": "google/flan-t5-small",
|
| 27 |
+
"input_text": f"Given the following context, provide a supportive response: {context}"
|
| 28 |
+
}
|
| 29 |
+
headers = {
|
| 30 |
+
"Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}"
|
| 31 |
+
}
|
| 32 |
+
response = requests.post(f"{GROQ_API_URL}/generate", json=payload, headers=headers)
|
| 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
|
| 47 |
+
|
| 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]
|
| 57 |
+
top_indices = np.argsort(cos_scores)[-k:][::-1]
|
| 58 |
+
|
| 59 |
+
retrieved_responses = []
|
| 60 |
+
for idx in top_indices:
|
| 61 |
+
retrieved_responses.append(dataset[idx]["response"])
|
| 62 |
+
return retrieved_responses
|
| 63 |
+
|
| 64 |
+
# Streamlit app UI
|
| 65 |
+
st.title("Emotional Support Buddy")
|
| 66 |
+
st.write("Enter your thoughts or concerns, and I'll provide some comforting words.")
|
| 67 |
+
|
| 68 |
+
# User input
|
| 69 |
+
user_query = st.text_input("How are you feeling today?")
|
| 70 |
+
|
| 71 |
+
if user_query:
|
| 72 |
+
# Retrieve similar responses from the dataset
|
| 73 |
+
retrieved_responses = retrieve_response(user_query, dataset, dataset_embeddings)
|
| 74 |
+
|
| 75 |
+
# Join retrieved responses to create a supportive context
|
| 76 |
+
context = " ".join(retrieved_responses)
|
| 77 |
+
|
| 78 |
+
# Generate a supportive response using FLAN-T5 via Groq API
|
| 79 |
+
supportive_response = generate_response(context)
|
| 80 |
+
|
| 81 |
+
st.write("Here's some advice or support for you:")
|
| 82 |
+
st.write(supportive_response)
|