Update app.py
Browse files
app.py
CHANGED
|
@@ -3,45 +3,25 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 8 |
-
|
| 9 |
|
| 10 |
-
print(knowledge)
|
| 11 |
-
|
| 12 |
-
# ✅✅✅ Change \n to wherever you want the text file to be broken into chunks
|
| 13 |
cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
|
| 14 |
-
print(cleaned_chunks)
|
| 15 |
-
|
| 16 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 17 |
-
|
| 18 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 19 |
-
print(chunk_embeddings)
|
| 20 |
-
|
| 21 |
-
cleaned_text = ""
|
| 22 |
|
| 23 |
def get_top_chunks(query):
|
| 24 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 25 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 26 |
-
|
| 27 |
similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
|
| 28 |
-
print(similarities)
|
| 29 |
top_indices = torch.topk(similarities, k=5).indices.tolist()
|
| 30 |
-
print(top_indices)
|
| 31 |
-
|
| 32 |
return [cleaned_chunks[i] for i in top_indices]
|
| 33 |
|
| 34 |
-
top_results = get_top_chunks("What are some good wizard characters?")
|
| 35 |
-
print(top_results)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
| 39 |
|
| 40 |
-
|
| 41 |
-
def respond(message, history):
|
| 42 |
response = ""
|
| 43 |
-
# char_limit= 500
|
| 44 |
-
|
| 45 |
top_chunks = get_top_chunks(message)
|
| 46 |
context = "\n".join(top_chunks)
|
| 47 |
|
|
@@ -49,9 +29,10 @@ def respond(message, history):
|
|
| 49 |
{
|
| 50 |
"role": "system",
|
| 51 |
"content": (
|
| 52 |
-
"You are a chatbot that helps users create characters for role-playing games."
|
| 53 |
-
"The user wants their character to be called
|
| 54 |
-
"Use the following knowledge to inform your answers:\n\n
|
|
|
|
| 55 |
)
|
| 56 |
}
|
| 57 |
]
|
|
@@ -65,7 +46,7 @@ def respond(message, history):
|
|
| 65 |
messages,
|
| 66 |
max_tokens=300,
|
| 67 |
temperature=1.2,
|
| 68 |
-
stream=True,
|
| 69 |
)
|
| 70 |
|
| 71 |
for message in stream:
|
|
@@ -74,43 +55,27 @@ def respond(message, history):
|
|
| 74 |
response += token
|
| 75 |
yield response
|
| 76 |
|
| 77 |
-
|
| 78 |
-
# response = response[:char_limit]
|
| 79 |
-
|
| 80 |
-
# for punc in [".", "!", "?"]:
|
| 81 |
-
# i = response.rfind(punc)
|
| 82 |
-
# if i != -1:
|
| 83 |
-
# response = response[:i+1]
|
| 84 |
-
# break
|
| 85 |
-
|
| 86 |
-
# yield response
|
| 87 |
-
|
| 88 |
-
# chatbot = gr.ChatInterface(respond, type="messages")
|
| 89 |
-
|
| 90 |
|
| 91 |
with gr.Blocks() as chatbot:
|
| 92 |
with gr.Row(scale=1):
|
| 93 |
with gr.Column(scale=1):
|
| 94 |
gr.Image(
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
with gr.Column(scale=1):
|
| 100 |
character_name = gr.Textbox(placeholder="Type your name here…")
|
| 101 |
-
gr.ChatInterface( respond,
|
| 102 |
-
type="messages",
|
| 103 |
-
examples=None,
|
| 104 |
-
title="Character Creator",
|
| 105 |
-
description="Welcome! Tell me what you want to create and we can make your character come to life!")
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
chatbot.launch()
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
|
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load knowledge
|
| 7 |
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 8 |
+
knowledge = file.read()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
|
|
|
|
|
|
|
| 11 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 12 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def get_top_chunks(query):
|
| 15 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 16 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
|
|
|
| 17 |
similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
|
|
|
|
| 18 |
top_indices = torch.topk(similarities, k=5).indices.tolist()
|
|
|
|
|
|
|
| 19 |
return [cleaned_chunks[i] for i in top_indices]
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
| 22 |
|
| 23 |
+
def respond(message, history, name):
|
|
|
|
| 24 |
response = ""
|
|
|
|
|
|
|
| 25 |
top_chunks = get_top_chunks(message)
|
| 26 |
context = "\n".join(top_chunks)
|
| 27 |
|
|
|
|
| 29 |
{
|
| 30 |
"role": "system",
|
| 31 |
"content": (
|
| 32 |
+
f"You are a chatbot that helps users create characters for role-playing games. "
|
| 33 |
+
f"The user wants their character to be called {name}. "
|
| 34 |
+
f"Use the following knowledge to inform your answers:\n\n{context}\n\n"
|
| 35 |
+
"Keep your answers under 300 words."
|
| 36 |
)
|
| 37 |
}
|
| 38 |
]
|
|
|
|
| 46 |
messages,
|
| 47 |
max_tokens=300,
|
| 48 |
temperature=1.2,
|
| 49 |
+
stream=True,
|
| 50 |
)
|
| 51 |
|
| 52 |
for message in stream:
|
|
|
|
| 55 |
response += token
|
| 56 |
yield response
|
| 57 |
|
| 58 |
+
# === GUI ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
with gr.Blocks() as chatbot:
|
| 61 |
with gr.Row(scale=1):
|
| 62 |
with gr.Column(scale=1):
|
| 63 |
gr.Image(
|
| 64 |
+
value="frog.png",
|
| 65 |
+
show_label=False,
|
| 66 |
+
show_share_button=False,
|
| 67 |
+
show_download_button=False
|
| 68 |
+
)
|
| 69 |
with gr.Column(scale=1):
|
| 70 |
character_name = gr.Textbox(placeholder="Type your name here…")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
chat = gr.ChatInterface(
|
| 73 |
+
fn=respond,
|
| 74 |
+
additional_inputs=[character_name], # Pass name into function!
|
| 75 |
+
type="messages",
|
| 76 |
+
examples=None,
|
| 77 |
+
title="Character Creator",
|
| 78 |
+
description="Welcome! Tell me what you want to create and we can make your character come to life!"
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
chatbot.launch()
|