Spaces:
Sleeping
Sleeping
First trial code written through - mostly drawn straight from lessons
Browse files
app.py
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
!pip install -q sentence-transformers
|
| 3 |
+
#this line is unneccesary in HF
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import torch
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
|
| 8 |
+
client = InferenceClient("google/gemma-3-27b-it")
|
| 9 |
+
|
| 10 |
+
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 11 |
+
knowledge = file.read()
|
| 12 |
+
cleaned_text = knowledge.strip()
|
| 13 |
+
# cleaning up the text
|
| 14 |
+
chunks = cleaned_text.split("\n")
|
| 15 |
+
# separating the text into one sentence chunks
|
| 16 |
+
cleaned_chunks = []
|
| 17 |
+
# creating a empty list called cleaned_chunks
|
| 18 |
+
|
| 19 |
+
for chunk in chunks:
|
| 20 |
+
# for every chunk in the chunks list,
|
| 21 |
+
stripped_chunk = chunk.strip()
|
| 22 |
+
# the chunk is getting stripped
|
| 23 |
+
if stripped_chunk:
|
| 24 |
+
#if the chunk is not empty then it is being appended to the cleaned chunk list.
|
| 25 |
+
cleaned_chunks.append(stripped_chunk)
|
| 26 |
+
print(cleaned_chunks)
|
| 27 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 28 |
+
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 29 |
+
# encode the model, pass through my clean chunks and convert to vector embeddings (not arrays)
|
| 30 |
+
print(chunk_embeddings
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_top_chunks(query): # store a function that gets the most relevant_info and make it return a variable “relevant_info” then
|
| 35 |
+
# create my function taking query as my parameter
|
| 36 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 37 |
+
# turning my query to a vector embedding for comparison
|
| 38 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 39 |
+
# normlaize my query to 1: allows for comparison of meaning
|
| 40 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 41 |
+
# normalizing chunks for comparison of meaning
|
| 42 |
+
|
| 43 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 44 |
+
print(similarities)
|
| 45 |
+
# using my matmul(matrix multiplication method to compare query to chunks)
|
| 46 |
+
|
| 47 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 48 |
+
print(top_indices)
|
| 49 |
+
# get the indices of the chunks that are most similar to my query
|
| 50 |
+
|
| 51 |
+
top_chunks = []
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
for i in top_indices:
|
| 55 |
+
chunk = chunks[i]
|
| 56 |
+
# for each index number in top_indices, get back the text
|
| 57 |
+
top_chunks.append(chunk) # adding the sentences that are associated with the top indices to the list
|
| 58 |
+
|
| 59 |
+
return top_chunks
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def respond(message, history):
|
| 65 |
+
#use created variable for relevant info
|
| 66 |
+
|
| 67 |
+
info = pull_relevant_info(message, top_k=3)
|
| 68 |
+
system_message = (f"You are a helpful and kind teacher. You respond clearly in no more than three complete sentences. Use the following information to help answer the user's question:\n\n{info}\n\n")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
#change HOW the bot responds
|
| 73 |
+
|
| 74 |
+
if history:
|
| 75 |
+
messages.extend(history)
|
| 76 |
+
messages.append({"role": "user", "content": message})
|
| 77 |
+
|
| 78 |
+
response = ""
|
| 79 |
+
for messages in client.chat_completion(
|
| 80 |
+
messages,
|
| 81 |
+
max_tokens = 100,#change the length of the messages
|
| 82 |
+
stream = True
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
):
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
token = messages.choices[0].delta.content
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
response += token
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
yield response
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|