Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,87 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def respond(message, history):
|
7 |
response = ""
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
if history:
|
12 |
messages.extend(history)
|
@@ -22,7 +97,6 @@ def respond(message, history):
|
|
22 |
|
23 |
for message in stream:
|
24 |
token = message.choices[0].delta.content
|
25 |
-
|
26 |
if token is not None:
|
27 |
response += token
|
28 |
yield response
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
!pip install -q sentence-transformers
|
4 |
+
from sentence_transformers import SentenceTransformer
|
5 |
+
import torch
|
6 |
+
|
7 |
+
with open("knowlege.txt", "r", encoding="utf-8") as file:
|
8 |
+
knowlege = file.read()
|
9 |
+
|
10 |
+
print(knowlege)
|
11 |
+
|
12 |
+
cleaned_chunks = [chunk.strip() for chunk in knowlege.strip().split("\n") if chunk.strip()]
|
13 |
+
print(cleaned_chunks)
|
14 |
+
|
15 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
16 |
+
|
17 |
+
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
18 |
+
print(chunk_embeddings)
|
19 |
+
|
20 |
+
cleaned_text = ""
|
21 |
+
|
22 |
+
def get_top_chunks(query):
|
23 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
24 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
25 |
+
|
26 |
+
similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
|
27 |
+
print(similarities)
|
28 |
+
top_indices = torch.topk(similarities, k=5).indices.tolist()
|
29 |
+
print(top_indices)
|
30 |
+
|
31 |
+
return [cleaned_chunks[i] for i in top_indices]
|
32 |
+
|
33 |
+
top_results = get_top_chunks("What are some good wizard characters?")
|
34 |
+
print(top_results)
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
|
42 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
43 |
|
44 |
+
# def respond(message, history):
|
45 |
+
# response = ""
|
46 |
+
|
47 |
+
# messages = [{"role": "system", "content": "You are a chatbot that helps users create characters for role playing games."}]
|
48 |
+
|
49 |
+
# if history:
|
50 |
+
# messages.extend(history)
|
51 |
+
|
52 |
+
# messages.append({"role": "user", "content": message})
|
53 |
+
|
54 |
+
# stream = client.chat_completion(
|
55 |
+
# messages,
|
56 |
+
# max_tokens=100,
|
57 |
+
# temperature=1.2,
|
58 |
+
# stream=True
|
59 |
+
# )
|
60 |
+
|
61 |
+
# for message in stream:
|
62 |
+
# token = message.choices[0].delta.content
|
63 |
+
|
64 |
+
# if token is not None:
|
65 |
+
# response += token
|
66 |
+
# yield response
|
67 |
+
|
68 |
def respond(message, history):
|
69 |
response = ""
|
70 |
|
71 |
+
# Retrieve top chunks based on the current user message
|
72 |
+
top_chunks = get_top_chunks(message)
|
73 |
+
context = "\n".join(top_chunks)
|
74 |
+
|
75 |
+
# Add knowledge as part of system instructions
|
76 |
+
messages = [
|
77 |
+
{
|
78 |
+
"role": "system",
|
79 |
+
"content": (
|
80 |
+
"You are a chatbot that helps users create characters for role-playing games. "
|
81 |
+
"Use the following knowledge to inform your answers:\n\n" + context
|
82 |
+
)
|
83 |
+
}
|
84 |
+
]
|
85 |
|
86 |
if history:
|
87 |
messages.extend(history)
|
|
|
97 |
|
98 |
for message in stream:
|
99 |
token = message.choices[0].delta.content
|
|
|
100 |
if token is not None:
|
101 |
response += token
|
102 |
yield response
|