Spaces:
Runtime error
Runtime error
spotify please please please work
Browse files
app.py
CHANGED
|
@@ -5,49 +5,29 @@ import torch
|
|
| 5 |
import numpy as np
|
| 6 |
import requests
|
| 7 |
|
| 8 |
-
#LLM we are using
|
| 9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
|
| 11 |
-
#adding text
|
| 12 |
with open("be_a_better_you.txt", "r", encoding="utf-8") as file1, open("journal_prompts.txt", "r", encoding="utf-8") as file2, open("workout.txt", "r", encoding="utf-8") as file3:
|
| 13 |
-
wellness_text = file1.read() + "\n" + file2.read() +"\n" + file3.read()
|
| 14 |
|
| 15 |
-
#cleaning up the text
|
| 16 |
cleaned_text = wellness_text.strip()
|
| 17 |
chunks = cleaned_text.split("\n")
|
| 18 |
-
cleaned_chunks = []
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
for chunk in chunks:
|
| 22 |
-
stripped_chunk = chunk.strip()
|
| 23 |
-
if stripped_chunk:
|
| 24 |
-
cleaned_chunks.append(stripped_chunk)
|
| 25 |
-
|
| 26 |
-
#import model for embeddings
|
| 27 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 28 |
-
|
| 29 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 30 |
|
| 31 |
def get_top_chunks(query):
|
| 32 |
-
# creating a function taking query as my parameter
|
| 33 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 34 |
-
# encode query to vector embedding for comparison
|
| 35 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 36 |
-
# normalize query to 1: allows for comparison of meaning
|
| 37 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 38 |
-
# normalizing chunks for comparison of meaning
|
| 39 |
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 40 |
-
# using matmul (matrix multiplication) method to compare query to chunks
|
| 41 |
top_indices = torch.topk(similarities, k=3).indices
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
top_chunks = []
|
| 45 |
-
|
| 46 |
-
for i in top_indices:
|
| 47 |
-
chunk = cleaned_chunks[i]
|
| 48 |
-
# for each index number in top_indices, get back the text
|
| 49 |
-
top_chunks.append(chunk)
|
| 50 |
-
|
| 51 |
return top_chunks
|
| 52 |
|
| 53 |
def get_nutrition_info(food_query):
|
|
@@ -62,12 +42,10 @@ def get_nutrition_info(food_query):
|
|
| 62 |
}
|
| 63 |
response = requests.post(url, headers=headers, json=body)
|
| 64 |
if response.status_code == 200:
|
| 65 |
-
|
| 66 |
-
return data
|
| 67 |
else:
|
| 68 |
return None
|
| 69 |
|
| 70 |
-
|
| 71 |
def get_motivational_quote():
|
| 72 |
url = "https://zenquotes.io/api/random"
|
| 73 |
response = requests.get(url)
|
|
@@ -77,30 +55,27 @@ def get_motivational_quote():
|
|
| 77 |
else:
|
| 78 |
return "Keep going! You're doing great."
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
def respond(message, history):
|
| 83 |
messages = [{
|
| 84 |
-
"role": "system",
|
| 85 |
"content": "You are a big sister chatbot named Nessie. You help people feel better in a simple manner. Always reply in 3 sentences or less. Do NOT stop mid-sentence, as you may confuse them even more. When the user is asking for ideas give 5 max, as you don't want to overwhelm them"
|
| 86 |
}]
|
| 87 |
-
# change the personality
|
| 88 |
context = get_top_chunks(message)
|
| 89 |
-
|
| 90 |
if history:
|
| 91 |
messages.extend(history)
|
| 92 |
messages.append({"role": "user", "content": message})
|
| 93 |
|
| 94 |
user_context = f"{message}\nInformation: {context}"
|
| 95 |
messages.append({"role": "user", "content": user_context})
|
|
|
|
| 96 |
response = ""
|
| 97 |
for messages in client.chat_completion(
|
| 98 |
messages,
|
| 99 |
-
max_tokens
|
| 100 |
-
stream
|
| 101 |
):
|
| 102 |
token = messages.choices[0].delta.content
|
| 103 |
-
response+= token
|
| 104 |
yield response
|
| 105 |
|
| 106 |
theme = gr.themes.Soft(
|
|
@@ -117,8 +92,24 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 117 |
examples=[
|
| 118 |
"Can you help me with my dietary goals? I want to track my calories, macros, and get advice based on myself.",
|
| 119 |
"Can you help me reach my fitness goals? I would like guidance and recommendations on workouts based on my goals.",
|
| 120 |
-
"Can you give me some journal prompts? I want to start journaling to help myself reflect on my goals and have some daily affirmations.
|
| 121 |
]
|
| 122 |
)
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
demo.launch(debug=True)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import requests
|
| 7 |
|
| 8 |
+
# LLM we are using
|
| 9 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
|
| 11 |
+
# adding text files
|
| 12 |
with open("be_a_better_you.txt", "r", encoding="utf-8") as file1, open("journal_prompts.txt", "r", encoding="utf-8") as file2, open("workout.txt", "r", encoding="utf-8") as file3:
|
| 13 |
+
wellness_text = file1.read() + "\n" + file2.read() + "\n" + file3.read()
|
| 14 |
|
| 15 |
+
# cleaning up the text
|
| 16 |
cleaned_text = wellness_text.strip()
|
| 17 |
chunks = cleaned_text.split("\n")
|
| 18 |
+
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
|
| 19 |
|
| 20 |
+
# import model for embeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 22 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 23 |
|
| 24 |
def get_top_chunks(query):
|
|
|
|
| 25 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
|
|
|
| 26 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
|
|
|
| 27 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
|
|
|
| 28 |
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
|
|
|
| 29 |
top_indices = torch.topk(similarities, k=3).indices
|
| 30 |
+
top_chunks = [cleaned_chunks[i] for i in top_indices]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
return top_chunks
|
| 32 |
|
| 33 |
def get_nutrition_info(food_query):
|
|
|
|
| 42 |
}
|
| 43 |
response = requests.post(url, headers=headers, json=body)
|
| 44 |
if response.status_code == 200:
|
| 45 |
+
return response.json()
|
|
|
|
| 46 |
else:
|
| 47 |
return None
|
| 48 |
|
|
|
|
| 49 |
def get_motivational_quote():
|
| 50 |
url = "https://zenquotes.io/api/random"
|
| 51 |
response = requests.get(url)
|
|
|
|
| 55 |
else:
|
| 56 |
return "Keep going! You're doing great."
|
| 57 |
|
|
|
|
|
|
|
| 58 |
def respond(message, history):
|
| 59 |
messages = [{
|
| 60 |
+
"role": "system",
|
| 61 |
"content": "You are a big sister chatbot named Nessie. You help people feel better in a simple manner. Always reply in 3 sentences or less. Do NOT stop mid-sentence, as you may confuse them even more. When the user is asking for ideas give 5 max, as you don't want to overwhelm them"
|
| 62 |
}]
|
|
|
|
| 63 |
context = get_top_chunks(message)
|
|
|
|
| 64 |
if history:
|
| 65 |
messages.extend(history)
|
| 66 |
messages.append({"role": "user", "content": message})
|
| 67 |
|
| 68 |
user_context = f"{message}\nInformation: {context}"
|
| 69 |
messages.append({"role": "user", "content": user_context})
|
| 70 |
+
|
| 71 |
response = ""
|
| 72 |
for messages in client.chat_completion(
|
| 73 |
messages,
|
| 74 |
+
max_tokens=250,
|
| 75 |
+
stream=True,
|
| 76 |
):
|
| 77 |
token = messages.choices[0].delta.content
|
| 78 |
+
response += token
|
| 79 |
yield response
|
| 80 |
|
| 81 |
theme = gr.themes.Soft(
|
|
|
|
| 92 |
examples=[
|
| 93 |
"Can you help me with my dietary goals? I want to track my calories, macros, and get advice based on myself.",
|
| 94 |
"Can you help me reach my fitness goals? I would like guidance and recommendations on workouts based on my goals.",
|
| 95 |
+
"Can you give me some journal prompts? I want to start journaling to help myself reflect on my goals and have some daily affirmations."
|
| 96 |
]
|
| 97 |
)
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
gr.HTML(
|
| 101 |
+
"""
|
| 102 |
+
<iframe style="border-radius:12px"
|
| 103 |
+
src="https://open.spotify.com/playlist/5dBkWtszs4KBDjwmpedb66?si=9cc22b91e49446d3&pt=dc6aaab05d72d3056f5891cd2c4e1ce7"
|
| 104 |
+
width="100%"
|
| 105 |
+
height="152"
|
| 106 |
+
frameBorder="0"
|
| 107 |
+
allowfullscreen=""
|
| 108 |
+
allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"
|
| 109 |
+
loading="lazy">
|
| 110 |
+
</iframe>
|
| 111 |
+
"""
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Launch the app
|
| 115 |
demo.launch(debug=True)
|