File size: 1,902 Bytes
235b1ba
1141e72
4718954
6a07e12
 
3b2955b
6a07e12
ef74b61
41bc4c0
5365751
fef5776
235b1ba
5365751
 
 
 
 
 
 
 
 
 
 
7a49b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5365751
d988bde
 
bee6385
d988bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235b1ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
from huggingface_hub import InferenceClient
pip install sentence_transformers
from sentence_transformers import SentenceTransformer
import torch
import numpy as np

with open("knowledge.txt" , "r", encoding="utf-8") as f:
    knowledge_base = f.read()

print("Knowledge base loaded.")

cleaned_text = knowledge_base.strip()

chunks = cleaned_text.split("\n")
cleaned_chunks = []

for chunk in chunks:
  stripped_chunk = chunk.strip()
  if stripped_chunk:
    cleaned_chunks.append(stripped_chunk)
print(cleaned_chunks)

model = SentenceTransformer('all-MiniLM-L6-v2')
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
print(chunk_embeddings)

def get_top_chunks(query):
  query_embedding = model.encode(query, convert_to_tensor=True)
  query_embedding_normalized = query_embedding / query_embedding.norm()
  chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)

  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
  print(similarities)

  top_indices = torch.topk(similarities, k=3).indices
  print(top_indices)

  top_chunks = []

  for i in top_indices:
    chunk = chunks[i]
    top_chunks.append(chunk)

  return top_chunks

client = InferenceClient("google/gemma-3-27b-it")

def respond(message,history):
    messages = [{"role": "system" , "content" : "You're a supportive and helpful feminist"}]
    if history:
        messages.extend(history)
        
    messages.append({"role" : "user", "content" : message})
    
    response = ""
    for message in client.chat_completion(
        messages, 
        max_tokens = 150,
        stream=True,
    ):
        token = message.choices[0].delta.content
        response += token
        yield response 
        
    print(response)
        
    
chatbot = gr.ChatInterface(respond, type = "messages")

chatbot.launch(debug=True)