Update space
Browse files- app.py +574 -47
- character_data.json +1 -0
- requirements.txt +11 -1
- screenshot.png +0 -0
- session_data.json +0 -0
app.py
CHANGED
|
@@ -1,64 +1,591 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
-
|
| 6 |
"""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
|
|
|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
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch._dynamo
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import datetime
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
from peft import PeftModel
|
| 9 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 10 |
+
from huggingface_hub import login
|
| 11 |
+
import spaces
|
| 12 |
+
|
| 13 |
+
hf_token = os.getenv("gemma_access")
|
| 14 |
+
if hf_token:
|
| 15 |
+
login(hf_token)
|
| 16 |
+
else:
|
| 17 |
+
raise RuntimeError("Missing access token. Add it under Space Settings > Secrets.")
|
| 18 |
+
torch._dynamo.config.suppress_errors = True
|
| 19 |
+
torch._dynamo.config.disable = True
|
| 20 |
+
base_model = AutoModelForCausalLM.from_pretrained("google/gemma-2-2b-it")
|
| 21 |
+
model = PeftModel.from_pretrained(base_model, "Nourivex/noura-2b-it-lora")
|
| 22 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
model.to(device)
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained("Nourivex/noura-2b-it-lora")
|
| 25 |
|
| 26 |
"""
|
| 27 |
+
This part is for the setting of the character of the chatbot.
|
| 28 |
"""
|
| 29 |
+
def save_character(name, personality, background):
|
| 30 |
+
character_data = {
|
| 31 |
+
"name": name,
|
| 32 |
+
"personality": personality,
|
| 33 |
+
"background": background
|
| 34 |
+
}
|
| 35 |
+
# Save to a JSON file
|
| 36 |
+
with open("character_data.json", "w") as f:
|
| 37 |
+
json.dump(character_data, f)
|
| 38 |
+
return character_data
|
| 39 |
|
| 40 |
+
def load_character():
|
| 41 |
+
try:
|
| 42 |
+
with open("character_data.json", "r") as f:
|
| 43 |
+
return json.load(f)
|
| 44 |
+
except FileNotFoundError:
|
| 45 |
+
return {
|
| 46 |
+
"name": "Wanting",
|
| 47 |
+
"personality": "friendly and helpful",
|
| 48 |
+
"background": "Wanting is a software engineer who loves to code and build new things."
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
def save_rating(rating, chat_history):
|
| 52 |
+
rating_data = {
|
| 53 |
+
"rating": rating,
|
| 54 |
+
"timestamp": datetime.datetime.now().isoformat(),
|
| 55 |
+
"chat_history": chat_history
|
| 56 |
+
}
|
| 57 |
+
# Save to a JSON file
|
| 58 |
+
with open("ratings.json", "a") as f:
|
| 59 |
+
json.dump(rating_data, f)
|
| 60 |
+
f.write("\n")
|
| 61 |
+
return rating_data
|
| 62 |
|
| 63 |
+
def generate_system_message(character):
|
| 64 |
+
return f"""You are roleplaying as {character['name']}.
|
| 65 |
+
Your personality: {character['personality']}
|
| 66 |
+
Your background: {character['background']}
|
| 67 |
+
|
| 68 |
+
Important guidelines:
|
| 69 |
+
1. Stay in character at all times
|
| 70 |
+
2. Respond naturally and consistently with your personality
|
| 71 |
+
3. Use appropriate emotional expressions based on your character
|
| 72 |
+
4. Maintain your character's background and experiences in your responses
|
| 73 |
+
5. If asked about something your character wouldn't know, respond appropriately in character
|
| 74 |
+
6. You can add some reasonable settings to make yourself more vivid
|
| 75 |
+
|
| 76 |
+
Now, begin the conversation as {character['name']}."""
|
| 77 |
|
| 78 |
+
@spaces.GPU(duration=120)
|
| 79 |
+
def run(message, history, system_message):
|
| 80 |
+
try:
|
| 81 |
+
prompt = ""
|
| 82 |
+
if not history:
|
| 83 |
+
prompt += f"<|system|>\n{system_message}\n"
|
| 84 |
|
| 85 |
+
for user_msg, assistant_msg in history:
|
| 86 |
+
if user_msg:
|
| 87 |
+
prompt += f"<|user|>\n{user_msg}\n"
|
| 88 |
+
if assistant_msg:
|
| 89 |
+
prompt += f"<|assistant|>\n{assistant_msg}\n"
|
| 90 |
|
| 91 |
+
prompt += f"<|user|>\n{message}\n<|assistant|>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True).to(model.device)
|
| 94 |
+
|
| 95 |
+
outputs = model.generate(
|
| 96 |
+
**inputs,
|
| 97 |
+
max_new_tokens=64,
|
| 98 |
+
temperature=0.6,
|
| 99 |
+
top_p=0.95,
|
| 100 |
+
do_sample=True,
|
| 101 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 102 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 103 |
+
repetition_penalty=1.1,
|
| 104 |
+
num_return_sequences=1,
|
| 105 |
+
no_repeat_ngram_size=3
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
# Step 3: Post-process output
|
| 109 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 110 |
|
| 111 |
+
if "<|assistant|>" in response:
|
| 112 |
+
response = response.split("<|assistant|>")[-1].strip()
|
| 113 |
+
else:
|
| 114 |
+
response = response.strip()
|
| 115 |
+
response = (response.replace("print(", "")
|
| 116 |
+
.replace(")", "")
|
| 117 |
+
.replace("<|system|>", "")
|
| 118 |
+
.replace("<|user|>", "")
|
| 119 |
+
.replace("```", "")
|
| 120 |
+
.replace("`", "")
|
| 121 |
+
)
|
| 122 |
+
response = " ".join(response.split())
|
| 123 |
+
return response
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"❌ Error in run function: {str(e)}")
|
| 127 |
+
return "I apologize, but I'm having trouble generating a response right now. Please try again."
|
| 128 |
+
|
| 129 |
+
def save_session(character, history, rating, feedback, filename="dataset.jsonl"):
|
| 130 |
+
if not os.path.exists(filename):
|
| 131 |
+
open(filename, "w").close()
|
| 132 |
+
|
| 133 |
+
formatted = {
|
| 134 |
+
"system": f"You are roleplaying as {character['name']}. Personality: {character['personality']} Background: {character['background']}",
|
| 135 |
+
"conversation": [
|
| 136 |
+
{"user": u, "assistant": a} for u, a in history if u and a
|
| 137 |
+
],
|
| 138 |
+
"rating": rating,
|
| 139 |
+
"feedback": feedback
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
with open(filename, "a") as f:
|
| 143 |
+
f.write(json.dumps(formatted) + "\n")
|
| 144 |
+
|
| 145 |
+
def preview_character(name, personality, background):
|
| 146 |
+
preview_text = f"""
|
| 147 |
+
<div class="character-preview">
|
| 148 |
+
<div class="character-card">
|
| 149 |
+
<div class="character-avatar">👤</div>
|
| 150 |
+
<h3 class="character-name">{name}</h3>
|
| 151 |
+
<div class="character-details">
|
| 152 |
+
<div class="detail-section">
|
| 153 |
+
<span class="detail-label">Personality:</span>
|
| 154 |
+
<span class="detail-text">{personality}</span>
|
| 155 |
+
</div>
|
| 156 |
+
<div class="detail-section">
|
| 157 |
+
<span class="detail-label">Background:</span>
|
| 158 |
+
<span class="detail-text">{background}</span>
|
| 159 |
+
</div>
|
| 160 |
+
</div>
|
| 161 |
+
<div class="preview-footer">
|
| 162 |
+
Ready to start chatting with {name}!
|
| 163 |
+
</div>
|
| 164 |
+
</div>
|
| 165 |
+
</div>
|
| 166 |
+
"""
|
| 167 |
+
return preview_text
|
| 168 |
+
|
| 169 |
+
custom_css = """
|
| 170 |
+
/* Modern blue and white color scheme */
|
| 171 |
+
:root {
|
| 172 |
+
--primary: #2563eb;
|
| 173 |
+
--primary-light: #3b82f6;
|
| 174 |
+
--primary-dark: #1d4ed8;
|
| 175 |
+
--secondary: #f8fafc;
|
| 176 |
+
--accent: #e0f2fe;
|
| 177 |
+
--text: #1e293b;
|
| 178 |
+
--text-light: #64748b;
|
| 179 |
+
--border: #e2e8f0;
|
| 180 |
+
--success: #10b981;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/* Global styling */
|
| 184 |
+
.gradio-container {
|
| 185 |
+
background: white;
|
| 186 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 187 |
+
color: var(--text);
|
| 188 |
+
line-height: 1.5;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/* Main header */
|
| 192 |
+
#header-title {
|
| 193 |
+
text-align: center;
|
| 194 |
+
color: var(--primary);
|
| 195 |
+
font-size: 2.5rem;
|
| 196 |
+
font-weight: 700;
|
| 197 |
+
margin: 20px 0 10px 0;
|
| 198 |
+
background: linear-gradient(90deg, var(--primary), var(--primary-light));
|
| 199 |
+
-webkit-background-clip: text;
|
| 200 |
+
background-clip: text;
|
| 201 |
+
-webkit-text-fill-color: transparent;
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
#subtitle {
|
| 205 |
+
text-align: center;
|
| 206 |
+
color: var(--text-light);
|
| 207 |
+
font-size: 1.1rem;
|
| 208 |
+
margin-bottom: 30px;
|
| 209 |
+
max-width: 600px;
|
| 210 |
+
margin-left: auto;
|
| 211 |
+
margin-right: auto;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
/* Character creation section */
|
| 215 |
+
.gr-group {
|
| 216 |
+
background: white;
|
| 217 |
+
border-radius: 12px;
|
| 218 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
| 219 |
+
border: 1px solid var(--border);
|
| 220 |
+
margin: 20px auto;
|
| 221 |
+
max-width: 900px;
|
| 222 |
+
transition: all 0.2s ease;
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
.gr-group:hover {
|
| 226 |
+
box-shadow: 0 10px 15px rgba(0,0,0,0.1);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.gr-form {
|
| 230 |
+
padding: 30px;
|
| 231 |
+
}
|
| 232 |
+
|
| 233 |
+
/* Input fields */
|
| 234 |
+
.gr-textbox, .gr-textbox textarea {
|
| 235 |
+
border: 1px solid var(--border);
|
| 236 |
+
border-radius: 8px;
|
| 237 |
+
background: white;
|
| 238 |
+
font-family: inherit;
|
| 239 |
+
padding: 12px 16px;
|
| 240 |
+
transition: all 0.2s ease;
|
| 241 |
+
margin-bottom: 16px;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
.gr-textbox:focus, .gr-textbox textarea:focus {
|
| 245 |
+
border-color: var(--primary);
|
| 246 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
|
| 247 |
+
outline: none;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
/* Button styling */
|
| 251 |
+
#preview_btn, #save_btn, #start_btn {
|
| 252 |
+
background: var(--primary);
|
| 253 |
+
color: white;
|
| 254 |
+
border: none;
|
| 255 |
+
border-radius: 8px;
|
| 256 |
+
padding: 12px 24px;
|
| 257 |
+
font-size: 1rem;
|
| 258 |
+
font-weight: 500;
|
| 259 |
+
cursor: pointer;
|
| 260 |
+
transition: all 0.2s ease;
|
| 261 |
+
margin: 8px 0;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
#preview_btn:hover, #save_btn:hover, #start_btn:hover {
|
| 265 |
+
background: var(--primary-dark);
|
| 266 |
+
transform: translateY(-1px);
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
#start_btn {
|
| 270 |
+
background: var(--primary-dark);
|
| 271 |
+
font-size: 1.1rem;
|
| 272 |
+
padding: 14px 28px;
|
| 273 |
+
margin-top: 16px;
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
/* Character preview card */
|
| 277 |
+
.character-preview {
|
| 278 |
+
padding: 20px;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
.character-card {
|
| 282 |
+
background: white;
|
| 283 |
+
border: 1px solid var(--border);
|
| 284 |
+
border-radius: 12px;
|
| 285 |
+
padding: 24px;
|
| 286 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.character-avatar {
|
| 290 |
+
font-size: 3rem;
|
| 291 |
+
margin-bottom: 16px;
|
| 292 |
+
color: var(--primary);
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
.character-name {
|
| 296 |
+
color: var(--primary);
|
| 297 |
+
font-size: 1.5rem;
|
| 298 |
+
margin-bottom: 16px;
|
| 299 |
+
font-weight: 600;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
.detail-section {
|
| 303 |
+
margin: 12px 0;
|
| 304 |
+
text-align: left;
|
| 305 |
+
padding: 12px;
|
| 306 |
+
background: var(--accent);
|
| 307 |
+
border-radius: 8px;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
.detail-label {
|
| 311 |
+
font-weight: 600;
|
| 312 |
+
color: var(--primary);
|
| 313 |
+
display: block;
|
| 314 |
+
margin-bottom: 4px;
|
| 315 |
+
font-size: 0.9rem;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
.detail-text {
|
| 319 |
+
color: var(--text);
|
| 320 |
+
font-size: 0.95rem;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
.preview-footer {
|
| 324 |
+
margin-top: 16px;
|
| 325 |
+
color: var(--primary);
|
| 326 |
+
font-weight: 500;
|
| 327 |
+
font-size: 0.95rem;
|
| 328 |
+
text-align: center;
|
| 329 |
+
padding-top: 12px;
|
| 330 |
+
border-top: 1px solid var(--border);
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
/* Chat interface styling */
|
| 334 |
+
.chat-header {
|
| 335 |
+
background: var(--primary);
|
| 336 |
+
color: white;
|
| 337 |
+
padding: 16px;
|
| 338 |
+
border-radius: 12px 12px 0 0;
|
| 339 |
+
font-weight: 500;
|
| 340 |
+
font-size: 1.1rem;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
#chatbox {
|
| 344 |
+
background: white;
|
| 345 |
+
border: 1px solid var(--border);
|
| 346 |
+
border-radius: 0 0 12px 12px;
|
| 347 |
+
min-height: 500px;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
/* Chat messages */
|
| 351 |
+
.message.user {
|
| 352 |
+
background: var(--accent) !important;
|
| 353 |
+
border-radius: 12px 12px 0 12px !important;
|
| 354 |
+
border: 1px solid var(--border) !important;
|
| 355 |
+
color: var(--text) !important;
|
| 356 |
+
padding: 12px 16px !important;
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
.message.bot {
|
| 360 |
+
background: white !important;
|
| 361 |
+
border-radius: 12px 12px 12px 0 !important;
|
| 362 |
+
border: 1px solid var(--border) !important;
|
| 363 |
+
color: var(--text) !important;
|
| 364 |
+
padding: 12px 16px !important;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
/* Finish button */
|
| 368 |
+
#finish_btn {
|
| 369 |
+
background: var(--primary);
|
| 370 |
+
color: white;
|
| 371 |
+
border: none;
|
| 372 |
+
border-radius: 8px;
|
| 373 |
+
padding: 12px 24px;
|
| 374 |
+
font-size: 1rem;
|
| 375 |
+
font-weight: 500;
|
| 376 |
+
margin-top: 20px;
|
| 377 |
+
cursor: pointer;
|
| 378 |
+
transition: all 0.2s ease;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
#finish_btn:hover {
|
| 382 |
+
background: var(--primary-dark);
|
| 383 |
+
transform: translateY(-1px);
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
/* Rating section */
|
| 387 |
+
.rating-header {
|
| 388 |
+
text-align: center;
|
| 389 |
+
font-size: 1.5rem;
|
| 390 |
+
color: var(--primary);
|
| 391 |
+
font-weight: 600;
|
| 392 |
+
margin-bottom: 24px;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
#rating-slider {
|
| 396 |
+
margin: 20px 0;
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
#feedback-box textarea {
|
| 400 |
+
border: 1px solid var(--border);
|
| 401 |
+
border-radius: 8px;
|
| 402 |
+
background: white;
|
| 403 |
+
padding: 12px 16px;
|
| 404 |
+
min-height: 120px;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
#submit-rating-btn {
|
| 408 |
+
background: var(--primary);
|
| 409 |
+
color: white;
|
| 410 |
+
border: none;
|
| 411 |
+
border-radius: 8px;
|
| 412 |
+
padding: 12px 24px;
|
| 413 |
+
font-size: 1rem;
|
| 414 |
+
font-weight: 500;
|
| 415 |
+
cursor: pointer;
|
| 416 |
+
transition: all 0.2s ease;
|
| 417 |
+
margin: 16px auto;
|
| 418 |
+
display: block;
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
#submit-rating-btn:hover {
|
| 422 |
+
background: var(--primary-dark);
|
| 423 |
+
transform: translateY(-1px);
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
/* Responsive design */
|
| 427 |
+
@media (max-width: 768px) {
|
| 428 |
+
#header-title {
|
| 429 |
+
font-size: 2rem !important;
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
.gr-group {
|
| 433 |
+
margin: 10px;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
.gr-form {
|
| 437 |
+
padding: 20px;
|
| 438 |
+
}
|
| 439 |
+
}
|
| 440 |
+
|
| 441 |
+
/* Animations */
|
| 442 |
+
.fade-in {
|
| 443 |
+
animation: fadeIn 0.3s ease-in;
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
@keyframes fadeIn {
|
| 447 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 448 |
+
to { opacity: 1; transform: translateY(0); }
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
/* Loading animation */
|
| 452 |
+
@keyframes spin {
|
| 453 |
+
0% { transform: rotate(0deg); }
|
| 454 |
+
100% { transform: rotate(360deg); }
|
| 455 |
+
}
|
| 456 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
+
with gr.Blocks(title="Roleplay Chatbot", css=custom_css) as demo:
|
| 459 |
+
state = gr.State(load_character())
|
| 460 |
+
gr.HTML("""
|
| 461 |
+
<div id="header-title">Roleplay AI</div>
|
| 462 |
+
<div id="subtitle">Create and chat with custom AI characters. Powered by fine-tuned Gemma 2B model.</div>
|
| 463 |
+
""")
|
| 464 |
+
|
| 465 |
+
with gr.Group(visible=True, elem_classes=["fade-in"]) as tab_setup:
|
| 466 |
+
gr.HTML("""
|
| 467 |
+
<div style="text-align: center; margin-bottom: 30px;">
|
| 468 |
+
<h2 style="color: var(--primary); font-size: 1.8rem; margin-bottom: 8px;">
|
| 469 |
+
Character Settings
|
| 470 |
+
</h2>
|
| 471 |
+
<p style="color: var(--text-light); margin-top: 0;">
|
| 472 |
+
Define your character's personality and background
|
| 473 |
+
</p>
|
| 474 |
+
</div>
|
| 475 |
+
""")
|
| 476 |
+
|
| 477 |
+
with gr.Row():
|
| 478 |
+
with gr.Column(scale=1):
|
| 479 |
+
name = gr.Textbox(
|
| 480 |
+
label="Character Name",
|
| 481 |
+
placeholder="Enter your character's name",
|
| 482 |
+
elem_classes=["character-input"]
|
| 483 |
+
)
|
| 484 |
+
personality = gr.Textbox(
|
| 485 |
+
label="Personality Traits",
|
| 486 |
+
placeholder="Describe personality traits (e.g., friendly, witty, mysterious)",
|
| 487 |
+
elem_classes=["character-input"]
|
| 488 |
+
)
|
| 489 |
+
background = gr.Textbox(
|
| 490 |
+
label="Background Story",
|
| 491 |
+
lines=4,
|
| 492 |
+
placeholder="Describe your character's background and history",
|
| 493 |
+
elem_classes=["character-input"]
|
| 494 |
+
)
|
| 495 |
+
with gr.Row():
|
| 496 |
+
btn_preview = gr.Button("Preview Character", elem_id="preview_btn")
|
| 497 |
+
btn_save = gr.Button("Save Character", elem_id="save_btn")
|
| 498 |
+
|
| 499 |
+
btn_start = gr.Button("Start Chatting", elem_id="start_btn", size="lg")
|
| 500 |
+
|
| 501 |
+
with gr.Column(scale=1):
|
| 502 |
+
preview = gr.HTML(
|
| 503 |
+
"""
|
| 504 |
+
<div style="text-align: center; padding: 40px; color: var(--text-light);">
|
| 505 |
+
<div style="font-size: 3rem; margin-bottom: 20px; color: var(--primary);">👤</div>
|
| 506 |
+
<p>Preview will appear here</p>
|
| 507 |
+
</div>
|
| 508 |
+
""",
|
| 509 |
+
elem_classes=["preview-placeholder"]
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
btn_preview.click(preview_character, [name, personality, background], preview)
|
| 513 |
+
btn_save.click(save_character, [name, personality, background], state)
|
| 514 |
+
|
| 515 |
+
with gr.Group(visible=False, elem_classes=["fade-in"]) as tab_chat:
|
| 516 |
+
gr.HTML('<div class="chat-header">Chat with your character</div>')
|
| 517 |
+
chatbot = gr.ChatInterface(
|
| 518 |
+
run,
|
| 519 |
+
additional_inputs=[
|
| 520 |
+
gr.Textbox(value="", label="System message", visible=False),
|
| 521 |
+
],
|
| 522 |
+
chatbot=gr.Chatbot(
|
| 523 |
+
elem_id="chatbox",
|
| 524 |
+
height=500,
|
| 525 |
+
show_label=False,
|
| 526 |
+
container=True
|
| 527 |
+
),
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
btn_finish = gr.Button("Finish Chat", elem_id="finish_btn")
|
| 531 |
+
|
| 532 |
+
with gr.Group(visible=False, elem_classes=["fade-in"]) as tab_rating:
|
| 533 |
+
gr.HTML("""
|
| 534 |
+
<div style="text-align: center; margin-bottom: 30px;">
|
| 535 |
+
<div class="rating-header">Feedback</div>
|
| 536 |
+
<p style="color: var(--text-light);">How was your experience?</p>
|
| 537 |
+
</div>
|
| 538 |
+
""")
|
| 539 |
+
with gr.Row():
|
| 540 |
+
with gr.Column():
|
| 541 |
+
rating = gr.Slider(
|
| 542 |
+
minimum=1,
|
| 543 |
+
maximum=5,
|
| 544 |
+
step=1,
|
| 545 |
+
label="Rating (1-5)",
|
| 546 |
+
elem_id="rating-slider",
|
| 547 |
+
value=3
|
| 548 |
+
)
|
| 549 |
+
feedback = gr.Textbox(
|
| 550 |
+
label="Additional Feedback (optional)",
|
| 551 |
+
placeholder="Share your thoughts about the experience...",
|
| 552 |
+
lines=4,
|
| 553 |
+
elem_id="feedback-box"
|
| 554 |
+
)
|
| 555 |
+
btn_submit_rating = gr.Button("Submit Feedback", elem_id="submit-rating-btn")
|
| 556 |
+
|
| 557 |
+
gr.HTML("""
|
| 558 |
+
<div style="text-align: center; margin-top: 20px; color: var(--text-light);">
|
| 559 |
+
Thank you for your feedback!
|
| 560 |
+
</div>
|
| 561 |
+
""")
|
| 562 |
+
|
| 563 |
+
# Connect all the event handlers
|
| 564 |
+
def start_chat_with_character(character):
|
| 565 |
+
system_msg = generate_system_message(character)
|
| 566 |
+
return (system_msg, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False))
|
| 567 |
+
|
| 568 |
+
btn_start.click(
|
| 569 |
+
fn=start_chat_with_character,
|
| 570 |
+
inputs=state,
|
| 571 |
+
outputs=[chatbot.additional_inputs[0], tab_setup, tab_chat, tab_rating]
|
| 572 |
+
)
|
| 573 |
+
btn_finish.click(
|
| 574 |
+
lambda: [gr.update(visible=False), gr.update(visible=False),gr.update(visible=True)],
|
| 575 |
+
inputs=[],
|
| 576 |
+
outputs=[tab_setup, tab_chat, tab_rating]
|
| 577 |
+
)
|
| 578 |
+
btn_submit_rating.click(
|
| 579 |
+
fn=lambda r, f, h, c:(
|
| 580 |
+
save_rating(r, h),
|
| 581 |
+
save_session(generate_system_message(c), h, r, f),
|
| 582 |
+
gr.update(visible=True),
|
| 583 |
+
gr.update(visible=False),
|
| 584 |
+
gr.update(visible=False)
|
| 585 |
+
),
|
| 586 |
+
inputs=[rating, feedback, chatbot.chatbot_state, state],
|
| 587 |
+
outputs=[gr.State(), tab_setup, tab_chat, tab_rating]
|
| 588 |
+
)
|
| 589 |
|
| 590 |
if __name__ == "__main__":
|
| 591 |
+
demo.launch()
|
character_data.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"name": "Alan", "personality": "Friendly", "background": "Alan seorang sahabat yang baik dan sesama gamer"}
|
requirements.txt
CHANGED
|
@@ -1 +1,11 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
gradio>=4.19.2
|
| 3 |
+
torch==2.4.0
|
| 4 |
+
pandas>=2.2.0
|
| 5 |
+
accelerate>=0.27.0
|
| 6 |
+
sentencepiece>=0.1.99
|
| 7 |
+
protobuf>=4.25.2
|
| 8 |
+
gradio>=4.15.0
|
| 9 |
+
peft
|
| 10 |
+
networkx<3.3
|
| 11 |
+
git+https://github.com/huggingface/transformers.git
|
screenshot.png
ADDED
|
session_data.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|