Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
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 |
|
@@ -30,7 +168,8 @@ def respond(message, history, name, char_class, char_alignment):
|
|
30 |
"role": "system",
|
31 |
"content": (
|
32 |
f"You are a chatbot that helps users create characters for role-playing games."
|
33 |
-
f"The user might also provide the following information, please include their inputs in your response: \n
|
|
|
34 |
f"Use the following knowledge to inform your answers:\n\n{context}\n\n"
|
35 |
"Keep your answers under 300 words."
|
36 |
)
|
@@ -55,63 +194,40 @@ def respond(message, history, name, char_class, char_alignment):
|
|
55 |
response += token
|
56 |
yield response
|
57 |
|
58 |
-
# ===
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
grid-template-columns: repeat(3, 1fr);
|
64 |
-
gap: 0 !important;
|
65 |
width: 300px;
|
|
|
66 |
}
|
67 |
-
|
68 |
-
#alignment_radio input[type="radio"] {
|
69 |
-
display: none;
|
70 |
-
}
|
71 |
-
|
72 |
-
#alignment_radio label {
|
73 |
-
display: flex !important;
|
74 |
-
align-items: center;
|
75 |
-
justify-content: center;
|
76 |
aspect-ratio: 1 / 1;
|
77 |
background-color: #eee;
|
78 |
-
border: 1px solid #
|
79 |
font-size: 0.75rem;
|
80 |
-
margin: 0
|
81 |
-
padding: 0
|
82 |
cursor: pointer;
|
83 |
-
user-select: none;
|
84 |
transition: background-color 0.2s, color 0.2s;
|
85 |
}
|
86 |
-
|
87 |
-
/* This is the magic: target the label of the checked input */
|
88 |
-
#alignment_radio input[type="radio"]:checked + label {
|
89 |
background-color: red !important;
|
90 |
-
color: white
|
91 |
-
border-color: darkred
|
92 |
}
|
93 |
"""
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
window.addEventListener('load', () => {
|
98 |
-
const container = document.querySelector('#alignment_radio .gr-radio');
|
99 |
-
if (container) {
|
100 |
-
container.classList.add('radio-grid');
|
101 |
-
Array.from(container.querySelectorAll('input[type="radio"]')).forEach(input => {
|
102 |
-
const wrapper = document.createElement('div');
|
103 |
-
wrapper.classList.add('radio-wrapper');
|
104 |
-
const label = container.querySelector(`label[for="${input.id}"]`);
|
105 |
-
input.replaceWith(wrapper);
|
106 |
-
wrapper.appendChild(input);
|
107 |
-
if (label) wrapper.appendChild(label);
|
108 |
-
});
|
109 |
-
}
|
110 |
-
});
|
111 |
-
</script>
|
112 |
-
"""
|
113 |
-
|
114 |
-
with gr.Blocks(css=chat_css, js=js) as chatbot:
|
115 |
with gr.Row(scale=1):
|
116 |
with gr.Column(scale=1):
|
117 |
gr.Image(
|
@@ -121,17 +237,49 @@ with gr.Blocks(css=chat_css, js=js) as chatbot:
|
|
121 |
show_download_button=False
|
122 |
)
|
123 |
with gr.Column(scale=1):
|
124 |
-
character_name = gr.Textbox(label
|
125 |
-
character_class = gr.CheckboxGroup(
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
|
|
128 |
gr.ChatInterface(
|
129 |
fn=respond,
|
130 |
-
additional_inputs=[character_name, character_class,
|
131 |
type="messages",
|
132 |
-
examples=None,
|
133 |
title="Character Creator",
|
134 |
description="Welcome! Tell me what you want to create and we can make your character come to life!"
|
135 |
)
|
136 |
|
137 |
-
chatbot.launch()
|
|
|
1 |
+
# import gradio as gr
|
2 |
+
# from huggingface_hub import InferenceClient
|
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, char_class, char_alignment):
|
24 |
+
# response = ""
|
25 |
+
# top_chunks = get_top_chunks(message)
|
26 |
+
# context = "\n".join(top_chunks)
|
27 |
+
|
28 |
+
# messages = [
|
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 might also provide the following information, please include their inputs in your response: \n Character name: {name}, Character Class: {char_class}, Character Alignment: {char_alignment}"
|
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 |
+
# ]
|
39 |
+
|
40 |
+
# if history:
|
41 |
+
# messages.extend(history)
|
42 |
+
|
43 |
+
# messages.append({"role": "user", "content": message})
|
44 |
+
|
45 |
+
# stream = client.chat_completion(
|
46 |
+
# messages,
|
47 |
+
# max_tokens=300,
|
48 |
+
# temperature=1.2,
|
49 |
+
# stream=True,
|
50 |
+
# )
|
51 |
+
|
52 |
+
# for message in stream:
|
53 |
+
# token = message.choices[0].delta.content
|
54 |
+
# if token is not None:
|
55 |
+
# response += token
|
56 |
+
# yield response
|
57 |
+
|
58 |
+
# # === GUI ===
|
59 |
+
|
60 |
+
# chat_css="""
|
61 |
+
# #alignment_radio .gr-radio {
|
62 |
+
# display: grid !important;
|
63 |
+
# grid-template-columns: repeat(3, 1fr);
|
64 |
+
# gap: 0 !important;
|
65 |
+
# width: 300px;
|
66 |
+
# }
|
67 |
+
|
68 |
+
# #alignment_radio input[type="radio"] {
|
69 |
+
# display: none;
|
70 |
+
# }
|
71 |
+
|
72 |
+
# #alignment_radio label {
|
73 |
+
# display: flex !important;
|
74 |
+
# align-items: center;
|
75 |
+
# justify-content: center;
|
76 |
+
# aspect-ratio: 1 / 1;
|
77 |
+
# background-color: #eee;
|
78 |
+
# border: 1px solid #ccc;
|
79 |
+
# font-size: 0.75rem;
|
80 |
+
# margin: 0 !important;
|
81 |
+
# padding: 0 !important;
|
82 |
+
# cursor: pointer;
|
83 |
+
# user-select: none;
|
84 |
+
# transition: background-color 0.2s, color 0.2s;
|
85 |
+
# }
|
86 |
+
|
87 |
+
# /* This is the magic: target the label of the checked input */
|
88 |
+
# #alignment_radio input[type="radio"]:checked + label {
|
89 |
+
# background-color: red !important;
|
90 |
+
# color: white !important;
|
91 |
+
# border-color: darkred !important;
|
92 |
+
# }
|
93 |
+
# """
|
94 |
+
|
95 |
+
# js = """
|
96 |
+
# <script>
|
97 |
+
# window.addEventListener('load', () => {
|
98 |
+
# const container = document.querySelector('#alignment_radio .gr-radio');
|
99 |
+
# if (container) {
|
100 |
+
# container.classList.add('radio-grid');
|
101 |
+
# Array.from(container.querySelectorAll('input[type="radio"]')).forEach(input => {
|
102 |
+
# const wrapper = document.createElement('div');
|
103 |
+
# wrapper.classList.add('radio-wrapper');
|
104 |
+
# const label = container.querySelector(`label[for="${input.id}"]`);
|
105 |
+
# input.replaceWith(wrapper);
|
106 |
+
# wrapper.appendChild(input);
|
107 |
+
# if (label) wrapper.appendChild(label);
|
108 |
+
# });
|
109 |
+
# }
|
110 |
+
# });
|
111 |
+
# </script>
|
112 |
+
# """
|
113 |
+
|
114 |
+
# with gr.Blocks(css=chat_css, js=js) as chatbot:
|
115 |
+
# with gr.Row(scale=1):
|
116 |
+
# with gr.Column(scale=1):
|
117 |
+
# gr.Image(
|
118 |
+
# value="frog.png",
|
119 |
+
# show_label=False,
|
120 |
+
# show_share_button=False,
|
121 |
+
# show_download_button=False
|
122 |
+
# )
|
123 |
+
# with gr.Column(scale=1):
|
124 |
+
# character_name = gr.Textbox(label = "Character Name", placeholder="Type your name here…", info ="optional")
|
125 |
+
# character_class = gr.CheckboxGroup(['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin', 'Ranger', 'Rogue', 'Sorcerer', 'Warlock', 'Wizard'], label="Character Class", info="Choose one or more")
|
126 |
+
# character_alignment = gr.Radio(["Lawful Good", "Neutral Good", "Chaotic Good", "Lawful Neutral", "True Neutral", "Chaotic Neutral", "Lawful Evil", "Neutral Evil", "Chaotic Evil"], elem_id="alignment_radio")
|
127 |
+
|
128 |
+
# gr.ChatInterface(
|
129 |
+
# fn=respond,
|
130 |
+
# additional_inputs=[character_name, character_class, character_alignment], # Pass name into function!
|
131 |
+
# type="messages",
|
132 |
+
# examples=None,
|
133 |
+
# title="Character Creator",
|
134 |
+
# description="Welcome! Tell me what you want to create and we can make your character come to life!"
|
135 |
+
# )
|
136 |
+
|
137 |
+
# chatbot.launch()
|
138 |
+
|
139 |
import gradio as gr
|
140 |
from huggingface_hub import InferenceClient
|
141 |
from sentence_transformers import SentenceTransformer
|
142 |
import torch
|
143 |
|
144 |
+
# === Load knowledge ===
|
145 |
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
146 |
knowledge = file.read()
|
147 |
|
|
|
168 |
"role": "system",
|
169 |
"content": (
|
170 |
f"You are a chatbot that helps users create characters for role-playing games."
|
171 |
+
f"The user might also provide the following information, please include their inputs in your response: \n"
|
172 |
+
f"Character name: {name}, Character Class: {char_class}, Character Alignment: {char_alignment}."
|
173 |
f"Use the following knowledge to inform your answers:\n\n{context}\n\n"
|
174 |
"Keep your answers under 300 words."
|
175 |
)
|
|
|
194 |
response += token
|
195 |
yield response
|
196 |
|
197 |
+
# === Character alignment setup ===
|
198 |
+
alignment_choices = [
|
199 |
+
"Lawful Good", "Neutral Good", "Chaotic Good",
|
200 |
+
"Lawful Neutral", "True Neutral", "Chaotic Neutral",
|
201 |
+
"Lawful Evil", "Neutral Evil", "Chaotic Evil"
|
202 |
+
]
|
203 |
|
204 |
+
# === CSS for buttons ===
|
205 |
+
chat_css = """
|
206 |
+
#alignment_container {
|
207 |
+
display: grid;
|
208 |
grid-template-columns: repeat(3, 1fr);
|
|
|
209 |
width: 300px;
|
210 |
+
gap: 0px;
|
211 |
}
|
212 |
+
.alignment-btn {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
aspect-ratio: 1 / 1;
|
214 |
background-color: #eee;
|
215 |
+
border: 1px solid #aaa;
|
216 |
font-size: 0.75rem;
|
217 |
+
margin: 0;
|
218 |
+
padding: 0;
|
219 |
cursor: pointer;
|
|
|
220 |
transition: background-color 0.2s, color 0.2s;
|
221 |
}
|
222 |
+
.alignment-btn.selected {
|
|
|
|
|
223 |
background-color: red !important;
|
224 |
+
color: white;
|
225 |
+
border-color: darkred;
|
226 |
}
|
227 |
"""
|
228 |
|
229 |
+
# === GUI ===
|
230 |
+
with gr.Blocks(css=chat_css) as chatbot:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
with gr.Row(scale=1):
|
232 |
with gr.Column(scale=1):
|
233 |
gr.Image(
|
|
|
237 |
show_download_button=False
|
238 |
)
|
239 |
with gr.Column(scale=1):
|
240 |
+
character_name = gr.Textbox(label="Character Name", placeholder="Type your name here…", info="optional")
|
241 |
+
character_class = gr.CheckboxGroup(
|
242 |
+
['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin',
|
243 |
+
'Ranger', 'Rogue', 'Sorcerer', 'Warlock', 'Wizard'],
|
244 |
+
label="Character Class", info="Choose one or more")
|
245 |
+
|
246 |
+
selected_alignment = gr.State("")
|
247 |
+
|
248 |
+
with gr.Row(elem_id="alignment_container"):
|
249 |
+
buttons = []
|
250 |
+
for choice in alignment_choices:
|
251 |
+
btn = gr.Button(choice, elem_classes=["alignment-btn"])
|
252 |
+
buttons.append(btn)
|
253 |
+
|
254 |
+
# Click logic for all alignment buttons
|
255 |
+
for btn, choice in zip(buttons, alignment_choices):
|
256 |
+
btn.click(
|
257 |
+
fn=lambda c=choice: c,
|
258 |
+
inputs=None,
|
259 |
+
outputs=selected_alignment
|
260 |
+
).then(
|
261 |
+
None,
|
262 |
+
None,
|
263 |
+
None,
|
264 |
+
_js=f"""
|
265 |
+
() => {{
|
266 |
+
document.querySelectorAll('.alignment-btn').forEach(b => b.classList.remove('selected'));
|
267 |
+
document.querySelectorAll('.alignment-btn').forEach(b => {{
|
268 |
+
if (b.innerText === "{choice}") {{
|
269 |
+
b.classList.add('selected');
|
270 |
+
}}
|
271 |
+
}});
|
272 |
+
}}
|
273 |
+
"""
|
274 |
+
)
|
275 |
|
276 |
+
# Chat interface
|
277 |
gr.ChatInterface(
|
278 |
fn=respond,
|
279 |
+
additional_inputs=[character_name, character_class, selected_alignment],
|
280 |
type="messages",
|
|
|
281 |
title="Character Creator",
|
282 |
description="Welcome! Tell me what you want to create and we can make your character come to life!"
|
283 |
)
|
284 |
|
285 |
+
chatbot.launch()
|