Spaces:
Sleeping
Sleeping
let's hope for the bestttttttt
Browse files
app.py
CHANGED
|
@@ -1,14 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
import os
|
|
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import torch
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=os.environ.get("HF"))
|
| 12 |
|
| 13 |
def respond(message, history):
|
| 14 |
messages = [{"role": "system",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=os.environ.get("HF"))
|
| 5 |
import os
|
| 6 |
+
!pip install -q sentence-transformers
|
| 7 |
from sentence_transformers import SentenceTransformer
|
| 8 |
import torch
|
| 9 |
|
| 10 |
+
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 11 |
+
knowledge_text = file.read()
|
| 12 |
+
|
| 13 |
+
def preprocess_text(text):
|
| 14 |
+
cleaned_text = text.strip()
|
| 15 |
+
chunks = cleaned_text.split("\n")
|
| 16 |
+
cleaned_chunks = []
|
| 17 |
+
for chunk in chunks:
|
| 18 |
+
stripped_chunk = chunk.strip()
|
| 19 |
+
if len(stripped_chunk) > 0:
|
| 20 |
+
cleaned_chunks.append(stripped_chunk)
|
| 21 |
+
return cleaned_chunks
|
| 22 |
+
cleaned_chunks = preprocess_text(knowledge_text)
|
| 23 |
+
|
| 24 |
+
=
|
| 25 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 26 |
+
|
| 27 |
+
def create_embeddings(text_chunks):
|
| 28 |
+
# Convert each text chunk into a vector embedding and store as a tensor
|
| 29 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the cleaned_chunks list
|
| 30 |
+
# Return the chunk_embeddings
|
| 31 |
+
return chunk_embeddings
|
| 32 |
+
|
| 33 |
+
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
|
| 34 |
+
chunk_embeddings = create_embeddings(cleaned_chunks) #complete this line
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 38 |
+
# Convert the query text into a vector embedding
|
| 39 |
+
query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
|
| 40 |
+
|
| 41 |
+
# Normalize the query embedding to unit length for accurate similarity comparison
|
| 42 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 43 |
+
|
| 44 |
+
# Normalize all chunk embeddings to unit length for consistent comparison
|
| 45 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 46 |
+
|
| 47 |
+
# Calculate cosine similarity between query and all chunks using matrix multiplication
|
| 48 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
|
| 49 |
+
|
| 50 |
+
# Find the indices of the 3 chunks with highest similarity scores
|
| 51 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Create an empty list to store the most relevant chunks
|
| 55 |
+
top_chunks = []
|
| 56 |
+
|
| 57 |
+
# Loop through the top indices and retrieve the corresponding text chunks
|
| 58 |
+
# This is only one way scholars may write this, but there are other ways!
|
| 59 |
+
for i in top_indices:
|
| 60 |
+
chunk = text_chunks[i]
|
| 61 |
+
top_chunks.append(chunk)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Return the list of most relevant chunks
|
| 65 |
+
return top_chunks
|
| 66 |
+
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
def respond(message, history):
|
| 70 |
messages = [{"role": "system",
|