Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import random
|
| 6 |
import os
|
| 7 |
-
from datetime import datetime
|
| 8 |
|
| 9 |
# Get Hugging Face token from environment variables
|
| 10 |
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", "")
|
|
@@ -36,31 +35,24 @@ COMPANY_INFO = {
|
|
| 36 |
"stage": "Growth and development, focused on building sustainable processes and systems"
|
| 37 |
}
|
| 38 |
|
| 39 |
-
# Knowledge base for the AI
|
| 40 |
KNOWLEDGE_BASE = {
|
| 41 |
"personal": {
|
| 42 |
-
"who are you": f"
|
| 43 |
-
"what are your goals": f"
|
| 44 |
-
"what do you do": f"
|
| 45 |
-
"tell me about yourself": f"
|
| 46 |
-
"background": f"
|
| 47 |
-
"experience": f"
|
| 48 |
},
|
| 49 |
"company": {
|
| 50 |
-
"what is yah tech": f"
|
| 51 |
-
"what does yah tech do": f"
|
| 52 |
-
"company philosophy": f"
|
| 53 |
-
"company stage": f"
|
| 54 |
-
"venture studio": "
|
| 55 |
-
"services": f"
|
| 56 |
-
"mission": f"
|
| 57 |
-
},
|
| 58 |
-
"general": {
|
| 59 |
-
"hello": "π Hello! I'm YAH Bot, your assistant for everything about Adedoyin James and YAH Tech. How can I help you today?",
|
| 60 |
-
"hi": "π Hi there! I'm here to tell you about Adedoyin James and YAH Tech. What would you like to know?",
|
| 61 |
-
"help": "π‘ I can answer questions about:\nβ’ Adedoyin James (background, goals, experience)\nβ’ YAH Tech (company info, services, philosophy)\nβ’ Our venture studio model and approach\nβ’ Technology and business insights",
|
| 62 |
-
"thanks": "π You're welcome! Is there anything else you'd like to know about Adedoyin or YAH Tech?",
|
| 63 |
-
"thank you": "π You're welcome! Feel free to ask more about our work at YAH Tech.",
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
|
@@ -75,97 +67,56 @@ class YAHBot:
|
|
| 75 |
"""Load the Hugging Face model"""
|
| 76 |
try:
|
| 77 |
print("π Loading YAH Tech AI model...")
|
| 78 |
-
|
| 79 |
-
self.
|
| 80 |
-
|
| 81 |
-
token=HUGGINGFACE_TOKEN if HUGGINGFACE_TOKEN else None
|
| 82 |
-
)
|
| 83 |
-
self.model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 84 |
-
"Adedoyinjames/YAH-Tech-Chat-Bot",
|
| 85 |
-
token=HUGGINGFACE_TOKEN if HUGGINGFACE_TOKEN else None,
|
| 86 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 87 |
-
)
|
| 88 |
-
print("β
YAH Tech AI model loaded successfully!")
|
| 89 |
except Exception as e:
|
| 90 |
-
print(f"β Failed to load
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 94 |
-
self.model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name)
|
| 95 |
-
print("β
Standard AI model loaded successfully!")
|
| 96 |
-
except Exception as e2:
|
| 97 |
-
print(f"β Failed to load AI model: {e2}")
|
| 98 |
-
self.model = None
|
| 99 |
-
self.tokenizer = None
|
| 100 |
|
| 101 |
def _get_knowledge_response(self, user_input):
|
| 102 |
"""Check if the input matches any knowledge base entries"""
|
| 103 |
user_input_lower = user_input.lower()
|
| 104 |
|
| 105 |
-
# Check
|
| 106 |
-
for category in KNOWLEDGE_BASE.values():
|
| 107 |
-
for key, response in category.items():
|
| 108 |
-
if key in user_input_lower:
|
| 109 |
-
return response
|
| 110 |
-
|
| 111 |
-
# Specific patterns for personal name
|
| 112 |
if any(word in user_input_lower for word in ["adedoyin", "ifeoluwa", "james"]):
|
| 113 |
return KNOWLEDGE_BASE["personal"]["who are you"]
|
| 114 |
|
| 115 |
-
# Specific patterns for company
|
| 116 |
if any(word in user_input_lower for word in ["yah tech", "your company", "venture studio"]):
|
| 117 |
return KNOWLEDGE_BASE["company"]["what is yah tech"]
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if any(word in user_input_lower for word in ["thank", "thanks"]):
|
| 125 |
-
return KNOWLEDGE_BASE["general"]["thanks"]
|
| 126 |
|
| 127 |
return None
|
| 128 |
|
| 129 |
-
def _create_smart_prompt(self, user_input
|
| 130 |
-
"""Create a
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
- {COMPANY_INFO['type']}
|
| 137 |
-
- Philosophy: "{COMPANY_INFO['philosophy']}"
|
| 138 |
-
- Purpose: {COMPANY_INFO['purpose']}
|
| 139 |
-
When answering questions:
|
| 140 |
-
1. First provide accurate, helpful information
|
| 141 |
-
2. Naturally mention YAH Tech or Adedoyin James when relevant
|
| 142 |
-
3. Be professional but conversational
|
| 143 |
-
4. Connect general topics to technology, business, or innovation when appropriate
|
| 144 |
-
Current conversation:"""
|
| 145 |
-
|
| 146 |
-
# Build conversation history
|
| 147 |
-
conv_history = ""
|
| 148 |
-
for msg in conversation_history[-4:]: # Last 4 exchanges
|
| 149 |
-
role = "Human" if msg["role"] == "user" else "Assistant"
|
| 150 |
-
conv_history += f"{role}: {msg['content']}\n"
|
| 151 |
-
|
| 152 |
-
prompt = f"{brand_context}\n{conv_history}Human: {user_input}\nAssistant:"
|
| 153 |
return prompt
|
| 154 |
|
| 155 |
-
def generate_response(self, user_input
|
| 156 |
"""Generate response using knowledge base or AI model"""
|
| 157 |
-
# First check knowledge base for
|
| 158 |
knowledge_response = self._get_knowledge_response(user_input)
|
| 159 |
if knowledge_response:
|
| 160 |
return knowledge_response
|
| 161 |
|
| 162 |
-
# Use AI model for
|
| 163 |
if self.model and self.tokenizer:
|
| 164 |
try:
|
| 165 |
-
# Create
|
| 166 |
-
prompt = self._create_smart_prompt(user_input
|
| 167 |
|
| 168 |
-
# Tokenize
|
| 169 |
inputs = self.tokenizer(
|
| 170 |
prompt,
|
| 171 |
return_tensors="pt",
|
|
@@ -178,139 +129,136 @@ Current conversation:"""
|
|
| 178 |
with torch.no_grad():
|
| 179 |
outputs = self.model.generate(
|
| 180 |
inputs.input_ids,
|
| 181 |
-
max_length=
|
| 182 |
num_return_sequences=1,
|
| 183 |
-
temperature=0.
|
| 184 |
do_sample=True,
|
| 185 |
-
pad_token_id=self.tokenizer.pad_token_id
|
| 186 |
-
repetition_penalty=1.
|
| 187 |
)
|
| 188 |
|
| 189 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 190 |
-
|
| 191 |
-
# Clean up the response and ensure brand mention when appropriate
|
| 192 |
-
cleaned_response = self._enhance_response(response, user_input)
|
| 193 |
-
return cleaned_response
|
| 194 |
|
| 195 |
except Exception as e:
|
| 196 |
print(f"Model error: {str(e)}")
|
| 197 |
-
return
|
| 198 |
|
| 199 |
-
|
| 200 |
-
return self._get_fallback_response(user_input)
|
| 201 |
-
|
| 202 |
-
def _enhance_response(self, response, user_input):
|
| 203 |
-
"""Enhance the response to include brand mentions when appropriate"""
|
| 204 |
-
user_input_lower = user_input.lower()
|
| 205 |
-
|
| 206 |
-
# Topics where brand mention would be natural
|
| 207 |
-
tech_topics = ["technology", "software", "app", "development", "coding", "programming"]
|
| 208 |
-
business_topics = ["business", "startup", "company", "venture", "entrepreneur"]
|
| 209 |
-
innovation_topics = ["innovation", "future", "solution", "system", "growth"]
|
| 210 |
-
|
| 211 |
-
# Check if the topic is relevant to our brand
|
| 212 |
-
is_relevant_topic = any(topic in user_input_lower for topic in
|
| 213 |
-
tech_topics + business_topics + innovation_topics)
|
| 214 |
-
|
| 215 |
-
# If response doesn't mention brand but topic is relevant, add a natural mention
|
| 216 |
-
if (is_relevant_topic and
|
| 217 |
-
"yah tech" not in response.lower() and
|
| 218 |
-
"adedoyin" not in response.lower()):
|
| 219 |
-
|
| 220 |
-
brand_connectors = [
|
| 221 |
-
f" {response} This aligns with the approach we take at YAH Tech under Adedoyin James' leadership.",
|
| 222 |
-
f" {response} At YAH Tech, we often work with similar concepts in our venture studio.",
|
| 223 |
-
f" {response} Speaking of which, Adedoyin James focuses on applying such principles at YAH Tech.",
|
| 224 |
-
f" {response} This is similar to the philosophy we embrace at YAH Tech - '{COMPANY_INFO['philosophy']}'."
|
| 225 |
-
]
|
| 226 |
-
return random.choice(brand_connectors)
|
| 227 |
-
|
| 228 |
-
return response
|
| 229 |
-
|
| 230 |
-
def _get_fallback_response(self, user_input):
|
| 231 |
-
"""Get a fallback response that still promotes the brand"""
|
| 232 |
-
fallback_responses = [
|
| 233 |
-
f"π That's an interesting question about {user_input.split()[0] if user_input.split() else 'that'}! At {COMPANY_INFO['name']}, we believe in **{COMPANY_INFO['philosophy']}** to create solutions that matter.",
|
| 234 |
-
f"π I'd be happy to discuss that. As {PERSONAL_INFO['name']}, I focus on building technology-driven ventures that create both **profit and societal value** through YAH Tech.",
|
| 235 |
-
f"π‘ Great question! Our approach at {COMPANY_INFO['name']} is to develop futuristic solutions through careful evaluation and systematic creation, which applies to many topics including this one.",
|
| 236 |
-
f"π― From my perspective as {PERSONAL_INFO['name']}, I focus on building systems that reshape the economic landscape through innovative technology at YAH Tech. How can I help you with this specifically?"
|
| 237 |
-
]
|
| 238 |
-
return random.choice(fallback_responses)
|
| 239 |
|
| 240 |
# Initialize the bot globally
|
| 241 |
yah_bot = YAHBot()
|
| 242 |
|
| 243 |
def chat_function(message, history):
|
| 244 |
-
"""
|
| 245 |
-
|
| 246 |
-
conversation_history = []
|
| 247 |
-
for human_msg, assistant_msg in history:
|
| 248 |
-
conversation_history.append({"role": "user", "content": human_msg})
|
| 249 |
-
conversation_history.append({"role": "assistant", "content": assistant_msg})
|
| 250 |
-
|
| 251 |
-
# Generate response
|
| 252 |
-
response = yah_bot.generate_response(message, conversation_history)
|
| 253 |
return response
|
| 254 |
|
| 255 |
def respond(message, chat_history):
|
| 256 |
-
"""
|
| 257 |
bot_response = chat_function(message, chat_history)
|
| 258 |
chat_history.append((message, bot_response))
|
| 259 |
return "", chat_history
|
| 260 |
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
custom_css = """
|
| 263 |
.gradio-container {
|
| 264 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 265 |
-
max-width:
|
| 266 |
margin: 0 auto !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
}
|
| 268 |
|
| 269 |
-
.
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
overflow-y: auto !important;
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
}
|
| 278 |
|
| 279 |
.user-message {
|
| 280 |
background: #007bff !important;
|
| 281 |
color: white !important;
|
| 282 |
-
padding: 12px 18px !important;
|
| 283 |
-
border-radius: 18px 18px 0 18px !important;
|
| 284 |
-
margin: 8px 0 !important;
|
| 285 |
-
max-width: 80% !important;
|
| 286 |
margin-left: auto !important;
|
| 287 |
-
|
| 288 |
-
|
| 289 |
}
|
| 290 |
|
| 291 |
.bot-message {
|
| 292 |
background: #f8f9fa !important;
|
| 293 |
color: #333 !important;
|
| 294 |
-
padding: 12px 18px !important;
|
| 295 |
-
border-radius: 18px 18px 18px 0 !important;
|
| 296 |
-
margin: 8px 0 !important;
|
| 297 |
-
max-width: 80% !important;
|
| 298 |
margin-right: auto !important;
|
|
|
|
|
|
|
| 299 |
border: 1px solid #e9ecef !important;
|
| 300 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.1) !important;
|
| 301 |
}
|
| 302 |
|
| 303 |
-
|
|
|
|
| 304 |
border-radius: 25px !important;
|
| 305 |
-
padding:
|
| 306 |
border: 2px solid #007bff !important;
|
| 307 |
font-size: 14px !important;
|
| 308 |
-
background: white !important;
|
| 309 |
}
|
| 310 |
|
| 311 |
-
.
|
| 312 |
border-color: #0056b3 !important;
|
| 313 |
-
box-shadow: 0 0 0
|
| 314 |
}
|
| 315 |
|
| 316 |
.send-button {
|
|
@@ -318,15 +266,13 @@ custom_css = """
|
|
| 318 |
background: #007bff !important;
|
| 319 |
color: white !important;
|
| 320 |
border: none !important;
|
| 321 |
-
padding:
|
| 322 |
font-weight: 600 !important;
|
| 323 |
-
transition: all 0.3s ease !important;
|
| 324 |
}
|
| 325 |
|
| 326 |
.send-button:hover {
|
| 327 |
background: #0056b3 !important;
|
| 328 |
transform: translateY(-1px) !important;
|
| 329 |
-
box-shadow: 0 4px 8px rgba(0,123,255,0.3) !important;
|
| 330 |
}
|
| 331 |
|
| 332 |
.clear-button {
|
|
@@ -334,164 +280,110 @@ custom_css = """
|
|
| 334 |
background: #6c757d !important;
|
| 335 |
color: white !important;
|
| 336 |
border: none !important;
|
| 337 |
-
padding:
|
| 338 |
font-weight: 600 !important;
|
| 339 |
-
transition: all 0.3s ease !important;
|
| 340 |
}
|
| 341 |
|
| 342 |
.clear-button:hover {
|
| 343 |
background: #545b62 !important;
|
| 344 |
-
transform: translateY(-1px) !important;
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
.title {
|
| 348 |
-
text-align: center !important;
|
| 349 |
-
color: #2c3e50 !important;
|
| 350 |
-
font-weight: bold !important;
|
| 351 |
-
margin-bottom: 10px !important;
|
| 352 |
-
font-size: 2.5em !important;
|
| 353 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 354 |
-
-webkit-background-clip: text !important;
|
| 355 |
-
-webkit-text-fill-color: transparent !important;
|
| 356 |
-
background-clip: text !important;
|
| 357 |
}
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
color: #6c757d !important;
|
| 362 |
-
margin-bottom: 30px !important;
|
| 363 |
-
font-size: 1.1em !important;
|
| 364 |
-
}
|
| 365 |
-
|
| 366 |
-
.footer {
|
| 367 |
-
text-align: center !important;
|
| 368 |
-
margin-top: 20px !important;
|
| 369 |
-
color: #888 !important;
|
| 370 |
-
font-size: 12px !important;
|
| 371 |
-
padding: 15px !important;
|
| 372 |
-
border-top: 1px solid #e9ecef !important;
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
/* Scrollbar styling */
|
| 376 |
-
.chat-container::-webkit-scrollbar {
|
| 377 |
width: 6px;
|
| 378 |
}
|
| 379 |
|
| 380 |
-
.
|
| 381 |
background: #f1f1f1;
|
| 382 |
border-radius: 3px;
|
| 383 |
}
|
| 384 |
|
| 385 |
-
.
|
| 386 |
background: #c1c1c1;
|
| 387 |
border-radius: 3px;
|
| 388 |
}
|
| 389 |
|
| 390 |
-
.
|
| 391 |
background: #a8a8a8;
|
| 392 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
"""
|
| 394 |
|
| 395 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 396 |
-
gr.Markdown(
|
| 397 |
-
"""
|
| 398 |
-
# <div class="title">YAH Tech AI Assistant</div>
|
| 399 |
-
<div class="subtitle">
|
| 400 |
-
Powered by Advanced AI β’ Ask me about YAH Tech, Adedoyin James, or general questions
|
| 401 |
-
</div>
|
| 402 |
-
"""
|
| 403 |
-
)
|
| 404 |
-
|
| 405 |
-
chatbot = gr.Chatbot(
|
| 406 |
-
label="Chat",
|
| 407 |
-
show_label=False,
|
| 408 |
-
height=600,
|
| 409 |
-
bubble_full_width=False,
|
| 410 |
-
show_copy_button=True,
|
| 411 |
-
placeholder="Start chatting with YAH Tech AI..."
|
| 412 |
-
)
|
| 413 |
|
| 414 |
-
with gr.
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
| 421 |
)
|
| 422 |
-
send = gr.Button("Send", variant="primary", scale=1, min_width=100)
|
| 423 |
-
clear = gr.Button("Clear", variant="secondary", scale=1, min_width=100)
|
| 424 |
-
|
| 425 |
-
# Footer
|
| 426 |
-
gr.Markdown(
|
| 427 |
-
f"""
|
| 428 |
-
<div class="footer">
|
| 429 |
-
<strong>YAH Tech AI Assistant</strong> β’ Building the future of technology β’
|
| 430 |
-
Founder: {PERSONAL_INFO['name']} β’
|
| 431 |
-
Venture Studio β’ Economic Innovation
|
| 432 |
-
</div>
|
| 433 |
-
"""
|
| 434 |
-
)
|
| 435 |
-
|
| 436 |
-
def clear_chat():
|
| 437 |
-
return []
|
| 438 |
-
|
| 439 |
-
def add_welcome_message():
|
| 440 |
-
welcome_msg = f"""
|
| 441 |
-
π **Hello! I'm YAH Bot, your AI assistant for everything related to {PERSONAL_INFO['name']} and {COMPANY_INFO['name']}!**
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
return [(welcome_msg, None)]
|
| 455 |
|
| 456 |
# Set initial welcome message
|
| 457 |
demo.load(add_welcome_message, None, [chatbot])
|
| 458 |
|
| 459 |
-
# Event handlers
|
| 460 |
-
msg.submit(
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
outputs=[msg, chatbot]
|
| 464 |
-
)
|
| 465 |
-
|
| 466 |
-
send.click(
|
| 467 |
-
respond,
|
| 468 |
-
inputs=[msg, chatbot],
|
| 469 |
-
outputs=[msg, chatbot]
|
| 470 |
-
)
|
| 471 |
-
|
| 472 |
-
clear.click(
|
| 473 |
-
clear_chat,
|
| 474 |
-
outputs=[chatbot]
|
| 475 |
-
)
|
| 476 |
-
|
| 477 |
-
# For API usage
|
| 478 |
-
def api_chat_endpoint(message: str, history: list = None):
|
| 479 |
-
"""API endpoint function"""
|
| 480 |
-
if history is None:
|
| 481 |
-
history = []
|
| 482 |
-
|
| 483 |
-
conversation_history = []
|
| 484 |
-
for human_msg, assistant_msg in history:
|
| 485 |
-
conversation_history.append({"role": "user", "content": human_msg})
|
| 486 |
-
conversation_history.append({"role": "assistant", "content": assistant_msg})
|
| 487 |
-
|
| 488 |
-
response = yah_bot.generate_response(message, conversation_history)
|
| 489 |
-
|
| 490 |
-
return {
|
| 491 |
-
"response": response,
|
| 492 |
-
"status": "success",
|
| 493 |
-
"message": message
|
| 494 |
-
}
|
| 495 |
|
| 496 |
if __name__ == "__main__":
|
| 497 |
demo.launch(
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import random
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Get Hugging Face token from environment variables
|
| 9 |
HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", "")
|
|
|
|
| 35 |
"stage": "Growth and development, focused on building sustainable processes and systems"
|
| 36 |
}
|
| 37 |
|
| 38 |
+
# Knowledge base for the AI
|
| 39 |
KNOWLEDGE_BASE = {
|
| 40 |
"personal": {
|
| 41 |
+
"who are you": f"I am YAH Bot, the personal AI assistant for {PERSONAL_INFO['name']}. I help answer questions about him and his company YAH Tech.",
|
| 42 |
+
"what are your goals": f"My purpose is to assist with information about {PERSONAL_INFO['name']} and YAH Tech. His goals include: {', '.join(PERSONAL_INFO['goals'])}",
|
| 43 |
+
"what do you do": f"I provide information about {PERSONAL_INFO['name']} and his work at YAH Tech.",
|
| 44 |
+
"tell me about yourself": f"I'm YAH Bot, an AI assistant created to help people learn about {PERSONAL_INFO['name']} and his technology ventures.",
|
| 45 |
+
"background": f"{PERSONAL_INFO['name']} is {PERSONAL_INFO['role']} focused on building impactful technology systems.",
|
| 46 |
+
"experience": f"As {PERSONAL_INFO['role']}, {PERSONAL_INFO['name']} has focused on building YAH Tech into a successful venture studio.",
|
| 47 |
},
|
| 48 |
"company": {
|
| 49 |
+
"what is yah tech": f"{COMPANY_INFO['name']} is a {COMPANY_INFO['type']} that {COMPANY_INFO['purpose'].lower()}.",
|
| 50 |
+
"what does yah tech do": f"We specialize in {COMPANY_INFO['primary_activity'].lower()}. {COMPANY_INFO['purpose']}.",
|
| 51 |
+
"company philosophy": f"Our philosophy is: '{COMPANY_INFO['philosophy']}'",
|
| 52 |
+
"company stage": f"We're currently in the {COMPANY_INFO['stage']} phase.",
|
| 53 |
+
"venture studio": "As a venture studio, we build complete businesses with scalable systems and futuristic solutions.",
|
| 54 |
+
"services": f"{COMPANY_INFO['name']} offers app development, venture building, business system design, technology solutions, and scalable platform development.",
|
| 55 |
+
"mission": f"Our mission at {COMPANY_INFO['name']} is to {COMPANY_INFO['purpose'].lower()}.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
|
|
|
| 67 |
"""Load the Hugging Face model"""
|
| 68 |
try:
|
| 69 |
print("π Loading YAH Tech AI model...")
|
| 70 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
| 71 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(self.model_name)
|
| 72 |
+
print("β
AI model loaded successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
+
print(f"β Failed to load AI model: {e}")
|
| 75 |
+
self.model = None
|
| 76 |
+
self.tokenizer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def _get_knowledge_response(self, user_input):
|
| 79 |
"""Check if the input matches any knowledge base entries"""
|
| 80 |
user_input_lower = user_input.lower()
|
| 81 |
|
| 82 |
+
# Check for specific brand-related queries
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if any(word in user_input_lower for word in ["adedoyin", "ifeoluwa", "james"]):
|
| 84 |
return KNOWLEDGE_BASE["personal"]["who are you"]
|
| 85 |
|
|
|
|
| 86 |
if any(word in user_input_lower for word in ["yah tech", "your company", "venture studio"]):
|
| 87 |
return KNOWLEDGE_BASE["company"]["what is yah tech"]
|
| 88 |
|
| 89 |
+
# Check knowledge base
|
| 90 |
+
for category in KNOWLEDGE_BASE.values():
|
| 91 |
+
for key, response in category.items():
|
| 92 |
+
if key in user_input_lower:
|
| 93 |
+
return response
|
|
|
|
|
|
|
| 94 |
|
| 95 |
return None
|
| 96 |
|
| 97 |
+
def _create_smart_prompt(self, user_input):
|
| 98 |
+
"""Create a much better prompt that separates brand info from general knowledge"""
|
| 99 |
+
prompt = f"""You are a helpful AI assistant. Answer the following question accurately and helpfully.
|
| 100 |
+
|
| 101 |
+
Question: {user_input}
|
| 102 |
+
|
| 103 |
+
Answer: """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
return prompt
|
| 105 |
|
| 106 |
+
def generate_response(self, user_input):
|
| 107 |
"""Generate response using knowledge base or AI model"""
|
| 108 |
+
# First check knowledge base for brand-specific queries
|
| 109 |
knowledge_response = self._get_knowledge_response(user_input)
|
| 110 |
if knowledge_response:
|
| 111 |
return knowledge_response
|
| 112 |
|
| 113 |
+
# Use AI model for general knowledge questions
|
| 114 |
if self.model and self.tokenizer:
|
| 115 |
try:
|
| 116 |
+
# Create clean prompt without brand bias for general questions
|
| 117 |
+
prompt = self._create_smart_prompt(user_input)
|
| 118 |
|
| 119 |
+
# Tokenize
|
| 120 |
inputs = self.tokenizer(
|
| 121 |
prompt,
|
| 122 |
return_tensors="pt",
|
|
|
|
| 129 |
with torch.no_grad():
|
| 130 |
outputs = self.model.generate(
|
| 131 |
inputs.input_ids,
|
| 132 |
+
max_length=150,
|
| 133 |
num_return_sequences=1,
|
| 134 |
+
temperature=0.7,
|
| 135 |
do_sample=True,
|
| 136 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
| 137 |
+
repetition_penalty=1.2
|
| 138 |
)
|
| 139 |
|
| 140 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 141 |
+
return response
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
print(f"Model error: {str(e)}")
|
| 145 |
+
return "I apologize, but I'm having trouble processing your question right now. Please try again."
|
| 146 |
|
| 147 |
+
return "I'm currently unavailable. Please check back later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
# Initialize the bot globally
|
| 150 |
yah_bot = YAHBot()
|
| 151 |
|
| 152 |
def chat_function(message, history):
|
| 153 |
+
"""Simple chat function without complex history processing"""
|
| 154 |
+
response = yah_bot.generate_response(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
return response
|
| 156 |
|
| 157 |
def respond(message, chat_history):
|
| 158 |
+
"""Main response function"""
|
| 159 |
bot_response = chat_function(message, chat_history)
|
| 160 |
chat_history.append((message, bot_response))
|
| 161 |
return "", chat_history
|
| 162 |
|
| 163 |
+
def clear_chat():
|
| 164 |
+
"""Clear chat history"""
|
| 165 |
+
return []
|
| 166 |
+
|
| 167 |
+
# MODERN CSS WITH FIXED LAYOUT
|
| 168 |
custom_css = """
|
| 169 |
.gradio-container {
|
| 170 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 171 |
+
max-width: 800px !important;
|
| 172 |
margin: 0 auto !important;
|
| 173 |
+
height: 100vh !important;
|
| 174 |
+
padding: 20px !important;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.main {
|
| 178 |
+
height: 100vh !important;
|
| 179 |
+
display: flex !important;
|
| 180 |
+
flex-direction: column !important;
|
| 181 |
+
gap: 0 !important;
|
| 182 |
}
|
| 183 |
|
| 184 |
+
.contain {
|
| 185 |
+
display: flex !important;
|
| 186 |
+
flex-direction: column !important;
|
| 187 |
+
height: 100vh !important;
|
| 188 |
+
gap: 0 !important;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
/* Header */
|
| 192 |
+
.header {
|
| 193 |
+
text-align: center;
|
| 194 |
+
margin-bottom: 20px !important;
|
| 195 |
+
padding: 20px;
|
| 196 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 197 |
+
border-radius: 12px;
|
| 198 |
+
color: white;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/* Chatbot Container - FIXED HEIGHT */
|
| 202 |
+
.chatbot {
|
| 203 |
+
flex: 1 !important;
|
| 204 |
+
min-height: 400px !important;
|
| 205 |
+
max-height: 65vh !important;
|
| 206 |
+
border: 1px solid #e1e5e9 !important;
|
| 207 |
+
border-radius: 12px !important;
|
| 208 |
+
margin-bottom: 20px !important;
|
| 209 |
+
background: white !important;
|
| 210 |
overflow-y: auto !important;
|
| 211 |
+
padding: 10px !important;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
/* Input Area - ALWAYS VISIBLE */
|
| 215 |
+
.input-area {
|
| 216 |
+
position: sticky !important;
|
| 217 |
+
bottom: 0 !important;
|
| 218 |
+
background: white !important;
|
| 219 |
+
padding: 15px 0 !important;
|
| 220 |
+
border-top: 1px solid #e1e5e9 !important;
|
| 221 |
+
margin-top: auto !important;
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
/* Message Styling */
|
| 225 |
+
.message {
|
| 226 |
+
max-width: 85% !important;
|
| 227 |
+
margin: 8px 0 !important;
|
| 228 |
+
padding: 12px 16px !important;
|
| 229 |
+
border-radius: 18px !important;
|
| 230 |
+
line-height: 1.4 !important;
|
| 231 |
+
word-wrap: break-word !important;
|
| 232 |
}
|
| 233 |
|
| 234 |
.user-message {
|
| 235 |
background: #007bff !important;
|
| 236 |
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
margin-left: auto !important;
|
| 238 |
+
margin-right: 0 !important;
|
| 239 |
+
border-bottom-right-radius: 4px !important;
|
| 240 |
}
|
| 241 |
|
| 242 |
.bot-message {
|
| 243 |
background: #f8f9fa !important;
|
| 244 |
color: #333 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
margin-right: auto !important;
|
| 246 |
+
margin-left: 0 !important;
|
| 247 |
+
border-bottom-left-radius: 4px !important;
|
| 248 |
border: 1px solid #e9ecef !important;
|
|
|
|
| 249 |
}
|
| 250 |
|
| 251 |
+
/* Input and Buttons */
|
| 252 |
+
.textbox {
|
| 253 |
border-radius: 25px !important;
|
| 254 |
+
padding: 12px 20px !important;
|
| 255 |
border: 2px solid #007bff !important;
|
| 256 |
font-size: 14px !important;
|
|
|
|
| 257 |
}
|
| 258 |
|
| 259 |
+
.textbox:focus {
|
| 260 |
border-color: #0056b3 !important;
|
| 261 |
+
box-shadow: 0 0 0 2px rgba(0,123,255,.25) !important;
|
| 262 |
}
|
| 263 |
|
| 264 |
.send-button {
|
|
|
|
| 266 |
background: #007bff !important;
|
| 267 |
color: white !important;
|
| 268 |
border: none !important;
|
| 269 |
+
padding: 12px 25px !important;
|
| 270 |
font-weight: 600 !important;
|
|
|
|
| 271 |
}
|
| 272 |
|
| 273 |
.send-button:hover {
|
| 274 |
background: #0056b3 !important;
|
| 275 |
transform: translateY(-1px) !important;
|
|
|
|
| 276 |
}
|
| 277 |
|
| 278 |
.clear-button {
|
|
|
|
| 280 |
background: #6c757d !important;
|
| 281 |
color: white !important;
|
| 282 |
border: none !important;
|
| 283 |
+
padding: 12px 20px !important;
|
| 284 |
font-weight: 600 !important;
|
|
|
|
| 285 |
}
|
| 286 |
|
| 287 |
.clear-button:hover {
|
| 288 |
background: #545b62 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
}
|
| 290 |
|
| 291 |
+
/* Scrollbar */
|
| 292 |
+
.chatbot::-webkit-scrollbar {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
width: 6px;
|
| 294 |
}
|
| 295 |
|
| 296 |
+
.chatbot::-webkit-scrollbar-track {
|
| 297 |
background: #f1f1f1;
|
| 298 |
border-radius: 3px;
|
| 299 |
}
|
| 300 |
|
| 301 |
+
.chatbot::-scrollbar-thumb {
|
| 302 |
background: #c1c1c1;
|
| 303 |
border-radius: 3px;
|
| 304 |
}
|
| 305 |
|
| 306 |
+
.chatbot::-webkit-scrollbar-thumb:hover {
|
| 307 |
background: #a8a8a8;
|
| 308 |
}
|
| 309 |
+
|
| 310 |
+
/* Footer */
|
| 311 |
+
.footer {
|
| 312 |
+
text-align: center;
|
| 313 |
+
margin-top: 15px;
|
| 314 |
+
color: #6c757d;
|
| 315 |
+
font-size: 12px;
|
| 316 |
+
padding: 10px;
|
| 317 |
+
}
|
| 318 |
"""
|
| 319 |
|
| 320 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
with gr.Column(elem_classes=["contain"]):
|
| 323 |
+
# Header
|
| 324 |
+
gr.Markdown(
|
| 325 |
+
f"""
|
| 326 |
+
<div class="header">
|
| 327 |
+
<h1 style="margin: 0; font-size: 2em;">YAH Tech AI Assistant</h1>
|
| 328 |
+
<p style="margin: 5px 0 0 0; opacity: 0.9;">Personal Assistant for {PERSONAL_INFO['name']}</p>
|
| 329 |
+
</div>
|
| 330 |
+
""",
|
| 331 |
+
elem_classes=["header"]
|
| 332 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
+
# Chatbot - FIXED HEIGHT
|
| 335 |
+
chatbot = gr.Chatbot(
|
| 336 |
+
label="Chat",
|
| 337 |
+
show_label=False,
|
| 338 |
+
elem_classes=["chatbot"],
|
| 339 |
+
show_copy_button=True,
|
| 340 |
+
placeholder="Chat with YAH Bot...",
|
| 341 |
+
height=400
|
| 342 |
+
)
|
| 343 |
|
| 344 |
+
# Input Area - ALWAYS VISIBLE AT BOTTOM
|
| 345 |
+
with gr.Row(elem_classes=["input-area"]):
|
| 346 |
+
msg = gr.Textbox(
|
| 347 |
+
placeholder="Ask me anything...",
|
| 348 |
+
show_label=False,
|
| 349 |
+
scale=4,
|
| 350 |
+
container=False,
|
| 351 |
+
elem_classes=["textbox"],
|
| 352 |
+
autofocus=True
|
| 353 |
+
)
|
| 354 |
+
with gr.Column(scale=1, min_width=120):
|
| 355 |
+
send = gr.Button("Send", variant="primary", elem_classes=["send-button"])
|
| 356 |
+
clear = gr.Button("Clear", variant="secondary", elem_classes=["clear-button"])
|
| 357 |
|
| 358 |
+
# Footer
|
| 359 |
+
gr.Markdown(
|
| 360 |
+
f"""
|
| 361 |
+
<div class="footer">
|
| 362 |
+
<strong>YAH Tech Assistant</strong> β’ Assistant to {PERSONAL_INFO['name']} β’ Venture Studio
|
| 363 |
+
</div>
|
| 364 |
+
""",
|
| 365 |
+
elem_classes=["footer"]
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
# Welcome message
|
| 369 |
+
def add_welcome_message():
|
| 370 |
+
welcome_msg = f"""π **Hello! I'm YAH Bot, the personal AI assistant for {PERSONAL_INFO['name']}.**
|
| 371 |
+
|
| 372 |
+
I can help you with:
|
| 373 |
+
β’ Information about {PERSONAL_INFO['name']} and YAH Tech
|
| 374 |
+
β’ General knowledge questions
|
| 375 |
+
β’ Technology and business discussions
|
| 376 |
+
|
| 377 |
+
**What would you like to know?**"""
|
| 378 |
return [(welcome_msg, None)]
|
| 379 |
|
| 380 |
# Set initial welcome message
|
| 381 |
demo.load(add_welcome_message, None, [chatbot])
|
| 382 |
|
| 383 |
+
# Event handlers
|
| 384 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 385 |
+
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 386 |
+
clear.click(clear_chat, outputs=[chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
if __name__ == "__main__":
|
| 389 |
demo.launch(
|