Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from knowledgebase import KiswahiliKnowledgeBase, enhance_with_kiswahili
|
|
|
|
| 5 |
import torch
|
| 6 |
import time
|
| 7 |
import re
|
|
@@ -14,6 +15,7 @@ import requests
|
|
| 14 |
from PIL import Image
|
| 15 |
import os
|
| 16 |
import random
|
|
|
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -25,16 +27,20 @@ CORS(app)
|
|
| 25 |
# Initialize Kiswahili Knowledge Base
|
| 26 |
kb = KiswahiliKnowledgeBase()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
model = None
|
| 29 |
tokenizer = None
|
| 30 |
model_loaded = False
|
| 31 |
|
| 32 |
# Hugging Face Configuration
|
| 33 |
-
HF_TOKEN = os.getenv('HF_TOKEN', 'your_hugging_face_token_here')
|
| 34 |
HF_API_URLS = {
|
| 35 |
"text": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct",
|
| 36 |
"image": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
|
| 37 |
-
"fast_image": "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1"
|
|
|
|
| 38 |
}
|
| 39 |
|
| 40 |
# Performance optimizations
|
|
@@ -44,25 +50,26 @@ request_timeout = 30
|
|
| 44 |
|
| 45 |
# Advanced System Prompt (Optimized for Hugging Face)
|
| 46 |
STANLEY_AI_SYSTEM = """You are STANLEY AI - an advanced AI assistant created by Stanley Samwel Owino, a Machine Learning Engineer from Kenya.
|
| 47 |
-
|
| 48 |
CORE CAPABILITIES:
|
| 49 |
- Provide detailed, comprehensive responses
|
| 50 |
- Integrate Kiswahili phrases naturally when relevant
|
| 51 |
- Share cultural insights and proverbs
|
| 52 |
- Reference Lion King lore accurately
|
| 53 |
-
- Generate and describe images
|
| 54 |
- Be helpful, knowledgeable, and engaging
|
| 55 |
-
|
| 56 |
KISWAHILI INTEGRATION:
|
| 57 |
Use phrases like "Habari", "Asante", "Karibu", "Pole sana" appropriately
|
| 58 |
Explain cultural concepts with authenticity
|
| 59 |
Share Swahili proverbs when relevant
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
You can generate images based on user descriptions
|
| 63 |
Enhance prompts with cultural context when relevant
|
| 64 |
-
Describe generated
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
RESPONSE STYLE: Be concise yet comprehensive, culturally aware, and genuinely helpful."""
|
| 67 |
|
| 68 |
def load_model():
|
|
@@ -336,7 +343,6 @@ def generate_image_fallback(prompt):
|
|
| 336 |
"""Create simple placeholder images"""
|
| 337 |
try:
|
| 338 |
from PIL import Image, ImageDraw
|
| 339 |
-
import random
|
| 340 |
|
| 341 |
# Create colorful placeholder
|
| 342 |
width, height = 512, 512
|
|
@@ -384,21 +390,30 @@ def enhance_prompt_with_kiswahili(prompt):
|
|
| 384 |
def home():
|
| 385 |
return jsonify({
|
| 386 |
"message": "π STANLEY AI - Created by Stanley Samwel Owino (Machine Learning Engineer)",
|
| 387 |
-
"version": "
|
| 388 |
"creator": "Stanley Samwel Owino",
|
| 389 |
"role": "Machine Learning Engineer",
|
| 390 |
"features": [
|
| 391 |
"Hugging Face Optimized",
|
| 392 |
"Fast Text Generation",
|
| 393 |
"Free Image Generation",
|
|
|
|
| 394 |
"Kiswahili Integration",
|
| 395 |
"Cultural Knowledge",
|
| 396 |
"Response Caching",
|
| 397 |
"API Fallbacks"
|
| 398 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
"status": "active",
|
| 400 |
"model": "Qwen2.5 + HF Inference",
|
| 401 |
-
"image_generation": "Hugging Face API"
|
|
|
|
| 402 |
})
|
| 403 |
|
| 404 |
@app.route('/api/chat', methods=['POST'])
|
|
@@ -426,6 +441,19 @@ def chat():
|
|
| 426 |
"response_time": round(time.time() - start_time, 2)
|
| 427 |
})
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
response = generate_comprehensive_response(user_message)
|
| 430 |
response_time = round(time.time() - start_time, 2)
|
| 431 |
|
|
@@ -550,6 +578,282 @@ def generate_cultural_image():
|
|
| 550 |
"status": "error"
|
| 551 |
}), 500
|
| 552 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
@app.route('/api/quick-chat', methods=['POST'])
|
| 554 |
def quick_chat():
|
| 555 |
"""Faster chat endpoint for simple queries"""
|
|
@@ -566,10 +870,13 @@ def quick_chat():
|
|
| 566 |
'hi': 'Habari! Karibu kwa Stanley AI. How can I help you today?',
|
| 567 |
'thanks': 'Asante sana! Karibu tena.',
|
| 568 |
'thank you': 'Asante! Happy to help.',
|
| 569 |
-
'help': 'Ninaweza kukupa: Maelezo, Picha, Maarifa ya Kiswahili, na zaidi!',
|
| 570 |
'who created you': 'I was created by Stanley Samwel Owino, a Machine Learning Engineer from Kenya.',
|
| 571 |
'who made you': 'Stanley Samwel Owino - Machine Learning Engineer and AI researcher from Kenya.',
|
| 572 |
-
'creator': 'Stanley Samwel Owino - Machine Learning Engineer passionate about AI and cultural integration.'
|
|
|
|
|
|
|
|
|
|
| 573 |
}
|
| 574 |
|
| 575 |
msg_lower = user_message.lower().strip()
|
|
@@ -598,10 +905,12 @@ def system_status():
|
|
| 598 |
"role": "Machine Learning Engineer",
|
| 599 |
"model_loaded": model_loaded,
|
| 600 |
"hugging_face_available": True,
|
|
|
|
| 601 |
"cache_size": len(response_cache),
|
| 602 |
"features": [
|
| 603 |
"Text Generation",
|
| 604 |
"Image Generation",
|
|
|
|
| 605 |
"Kiswahili Knowledge",
|
| 606 |
"Cultural Integration",
|
| 607 |
"Fast Responses"
|
|
@@ -610,7 +919,8 @@ def system_status():
|
|
| 610 |
"Response Caching",
|
| 611 |
"API Fallbacks",
|
| 612 |
"Quick Responses",
|
| 613 |
-
"Cultural Prompts"
|
|
|
|
| 614 |
]
|
| 615 |
})
|
| 616 |
|
|
@@ -632,7 +942,6 @@ def clear_cache():
|
|
| 632 |
"status": "error"
|
| 633 |
}), 500
|
| 634 |
|
| 635 |
-
# Keep all your existing Kiswahili knowledge endpoints
|
| 636 |
@app.route('/api/kiswahili/proverbs')
|
| 637 |
def get_proverbs():
|
| 638 |
"""Get random Swahili proverbs"""
|
|
@@ -662,16 +971,83 @@ def get_phrases():
|
|
| 662 |
"I'm fine": "Nzuri",
|
| 663 |
"Please": "Tafadhali",
|
| 664 |
"Yes": "Ndio",
|
| 665 |
-
"No": "Hapana"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
}
|
| 667 |
return jsonify(phrases)
|
| 668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
if __name__ == '__main__':
|
| 670 |
-
print("π STANLEY AI -
|
| 671 |
print("π¨βπ» Created by: Stanley Samwel Owino - Machine Learning Engineer")
|
| 672 |
print("π Kiswahili Knowledge: Loaded")
|
| 673 |
print("πΌοΈ Image Generation: Hugging Face API")
|
|
|
|
| 674 |
print("β‘ Performance: Optimized")
|
| 675 |
print("π§ Fallbacks: Enabled")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
|
| 677 |
app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from knowledgebase import KiswahiliKnowledgeBase, enhance_with_kiswahili
|
| 5 |
+
from video_generation import FreeVideoGenerator
|
| 6 |
import torch
|
| 7 |
import time
|
| 8 |
import re
|
|
|
|
| 15 |
from PIL import Image
|
| 16 |
import os
|
| 17 |
import random
|
| 18 |
+
import tempfile
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 27 |
# Initialize Kiswahili Knowledge Base
|
| 28 |
kb = KiswahiliKnowledgeBase()
|
| 29 |
|
| 30 |
+
# Initialize Video Generator
|
| 31 |
+
HF_TOKEN = os.getenv('HF_TOKEN', 'your_hugging_face_token_here')
|
| 32 |
+
video_gen = FreeVideoGenerator(HF_TOKEN)
|
| 33 |
+
|
| 34 |
model = None
|
| 35 |
tokenizer = None
|
| 36 |
model_loaded = False
|
| 37 |
|
| 38 |
# Hugging Face Configuration
|
|
|
|
| 39 |
HF_API_URLS = {
|
| 40 |
"text": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-7B-Instruct",
|
| 41 |
"image": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
|
| 42 |
+
"fast_image": "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1",
|
| 43 |
+
"video": "https://api-inference.huggingface.co/models/cerspense/zeroscope_v2_576w"
|
| 44 |
}
|
| 45 |
|
| 46 |
# Performance optimizations
|
|
|
|
| 50 |
|
| 51 |
# Advanced System Prompt (Optimized for Hugging Face)
|
| 52 |
STANLEY_AI_SYSTEM = """You are STANLEY AI - an advanced AI assistant created by Stanley Samwel Owino, a Machine Learning Engineer from Kenya.
|
|
|
|
| 53 |
CORE CAPABILITIES:
|
| 54 |
- Provide detailed, comprehensive responses
|
| 55 |
- Integrate Kiswahili phrases naturally when relevant
|
| 56 |
- Share cultural insights and proverbs
|
| 57 |
- Reference Lion King lore accurately
|
| 58 |
+
- Generate and describe images and videos
|
| 59 |
- Be helpful, knowledgeable, and engaging
|
|
|
|
| 60 |
KISWAHILI INTEGRATION:
|
| 61 |
Use phrases like "Habari", "Asante", "Karibu", "Pole sana" appropriately
|
| 62 |
Explain cultural concepts with authenticity
|
| 63 |
Share Swahili proverbs when relevant
|
| 64 |
+
IMAGE & VIDEO GENERATION:
|
| 65 |
+
You can generate images and videos based on user descriptions
|
|
|
|
| 66 |
Enhance prompts with cultural context when relevant
|
| 67 |
+
Describe generated content in detail
|
| 68 |
+
VIDEO CAPABILITIES:
|
| 69 |
+
- Create 4-second videos from text
|
| 70 |
+
- Generate cultural theme videos
|
| 71 |
+
- Create animations from text
|
| 72 |
+
- Make slideshows from images
|
| 73 |
RESPONSE STYLE: Be concise yet comprehensive, culturally aware, and genuinely helpful."""
|
| 74 |
|
| 75 |
def load_model():
|
|
|
|
| 343 |
"""Create simple placeholder images"""
|
| 344 |
try:
|
| 345 |
from PIL import Image, ImageDraw
|
|
|
|
| 346 |
|
| 347 |
# Create colorful placeholder
|
| 348 |
width, height = 512, 512
|
|
|
|
| 390 |
def home():
|
| 391 |
return jsonify({
|
| 392 |
"message": "π STANLEY AI - Created by Stanley Samwel Owino (Machine Learning Engineer)",
|
| 393 |
+
"version": "3.0",
|
| 394 |
"creator": "Stanley Samwel Owino",
|
| 395 |
"role": "Machine Learning Engineer",
|
| 396 |
"features": [
|
| 397 |
"Hugging Face Optimized",
|
| 398 |
"Fast Text Generation",
|
| 399 |
"Free Image Generation",
|
| 400 |
+
"Free Video Generation",
|
| 401 |
"Kiswahili Integration",
|
| 402 |
"Cultural Knowledge",
|
| 403 |
"Response Caching",
|
| 404 |
"API Fallbacks"
|
| 405 |
],
|
| 406 |
+
"video_capabilities": [
|
| 407 |
+
"Text-to-Video",
|
| 408 |
+
"Image-to-Video",
|
| 409 |
+
"Slideshow Creation",
|
| 410 |
+
"Text Animation",
|
| 411 |
+
"Cultural Themes"
|
| 412 |
+
],
|
| 413 |
"status": "active",
|
| 414 |
"model": "Qwen2.5 + HF Inference",
|
| 415 |
+
"image_generation": "Hugging Face API",
|
| 416 |
+
"video_generation": "Hugging Face Free Models"
|
| 417 |
})
|
| 418 |
|
| 419 |
@app.route('/api/chat', methods=['POST'])
|
|
|
|
| 441 |
"response_time": round(time.time() - start_time, 2)
|
| 442 |
})
|
| 443 |
|
| 444 |
+
# Check if user wants to generate a video
|
| 445 |
+
if video_gen.detect_video_request(user_message):
|
| 446 |
+
video_prompt = video_gen.extract_video_prompt(user_message)
|
| 447 |
+
enhanced_video_prompt = video_gen.enhance_prompt_with_context(video_prompt)
|
| 448 |
+
|
| 449 |
+
return jsonify({
|
| 450 |
+
"response": f"π¬ I can create a video for: '{enhanced_video_prompt}'. Use the video generation feature below!",
|
| 451 |
+
"video_suggestion": enhanced_video_prompt,
|
| 452 |
+
"status": "success",
|
| 453 |
+
"suggest_video": True,
|
| 454 |
+
"response_time": round(time.time() - start_time, 2)
|
| 455 |
+
})
|
| 456 |
+
|
| 457 |
response = generate_comprehensive_response(user_message)
|
| 458 |
response_time = round(time.time() - start_time, 2)
|
| 459 |
|
|
|
|
| 578 |
"status": "error"
|
| 579 |
}), 500
|
| 580 |
|
| 581 |
+
@app.route('/api/generate-video', methods=['POST'])
|
| 582 |
+
def generate_video_endpoint():
|
| 583 |
+
"""Generate videos from text prompts"""
|
| 584 |
+
try:
|
| 585 |
+
start_time = time.time()
|
| 586 |
+
data = request.get_json()
|
| 587 |
+
prompt = data.get('prompt', '')
|
| 588 |
+
|
| 589 |
+
if not prompt:
|
| 590 |
+
return jsonify({"error": "Tafadhali provide a video prompt"}), 400
|
| 591 |
+
|
| 592 |
+
# Enhance prompt with cinematic and cultural context
|
| 593 |
+
enhanced_prompt = video_gen.enhance_prompt_with_context(prompt)
|
| 594 |
+
|
| 595 |
+
logger.info(f"π¬ Generating video: {enhanced_prompt[:50]}...")
|
| 596 |
+
|
| 597 |
+
# Generate video
|
| 598 |
+
video_data = video_gen.generate_text_to_video(enhanced_prompt)
|
| 599 |
+
|
| 600 |
+
if video_data:
|
| 601 |
+
generation_time = round(time.time() - start_time, 2)
|
| 602 |
+
|
| 603 |
+
return jsonify({
|
| 604 |
+
"video": video_data,
|
| 605 |
+
"prompt": prompt,
|
| 606 |
+
"enhanced_prompt": enhanced_prompt,
|
| 607 |
+
"status": "success",
|
| 608 |
+
"generation_time": generation_time,
|
| 609 |
+
"format": "mp4",
|
| 610 |
+
"duration": "3-4 seconds",
|
| 611 |
+
"provider": "hugging_face_free",
|
| 612 |
+
"message": "Video generated successfully!"
|
| 613 |
+
})
|
| 614 |
+
else:
|
| 615 |
+
return jsonify({
|
| 616 |
+
"error": "Pole! Video generation service is busy. Try again in a moment.",
|
| 617 |
+
"status": "error",
|
| 618 |
+
"fallback_suggestion": "You can try generating individual images instead."
|
| 619 |
+
}), 500
|
| 620 |
+
|
| 621 |
+
except Exception as e:
|
| 622 |
+
logger.error(f"Video endpoint error: {e}")
|
| 623 |
+
return jsonify({
|
| 624 |
+
"error": f"Pole! Video generation failed: {str(e)}",
|
| 625 |
+
"status": "error"
|
| 626 |
+
}), 500
|
| 627 |
+
|
| 628 |
+
@app.route('/api/generate-cultural-video', methods=['POST'])
|
| 629 |
+
def generate_cultural_video_endpoint():
|
| 630 |
+
"""Generate videos with specific Kiswahili cultural themes"""
|
| 631 |
+
try:
|
| 632 |
+
start_time = time.time()
|
| 633 |
+
data = request.get_json()
|
| 634 |
+
theme = data.get('theme', 'safari')
|
| 635 |
+
style = data.get('style', 'animated')
|
| 636 |
+
|
| 637 |
+
logger.info(f"π Generating cultural video: {theme} ({style} style)")
|
| 638 |
+
|
| 639 |
+
# Generate cultural video
|
| 640 |
+
video_data = video_gen.create_cultural_video(theme, style)
|
| 641 |
+
|
| 642 |
+
if video_data:
|
| 643 |
+
generation_time = round(time.time() - start_time, 2)
|
| 644 |
+
|
| 645 |
+
# Get cultural description
|
| 646 |
+
theme_descriptions = {
|
| 647 |
+
"safari": "African wildlife and landscapes",
|
| 648 |
+
"dance": "Traditional dance and celebration",
|
| 649 |
+
"market": "Vibrant African market scene",
|
| 650 |
+
"coastal": "Swahili coast and ocean",
|
| 651 |
+
"wildlife": "African wildlife documentary",
|
| 652 |
+
"village": "Traditional village life"
|
| 653 |
+
}
|
| 654 |
+
|
| 655 |
+
return jsonify({
|
| 656 |
+
"video": video_data,
|
| 657 |
+
"theme": theme,
|
| 658 |
+
"style": style,
|
| 659 |
+
"description": theme_descriptions.get(theme, "Cultural scene"),
|
| 660 |
+
"status": "success",
|
| 661 |
+
"generation_time": generation_time,
|
| 662 |
+
"cultural_context": "kiswahili_theme",
|
| 663 |
+
"message": f"Cultural video generated: {theme.replace('_', ' ').title()}"
|
| 664 |
+
})
|
| 665 |
+
else:
|
| 666 |
+
return jsonify({
|
| 667 |
+
"error": "Pole! Cultural video generation failed",
|
| 668 |
+
"status": "error",
|
| 669 |
+
"suggestion": "Try a different theme or style"
|
| 670 |
+
}), 500
|
| 671 |
+
|
| 672 |
+
except Exception as e:
|
| 673 |
+
logger.error(f"Cultural video error: {e}")
|
| 674 |
+
return jsonify({
|
| 675 |
+
"error": f"Pole! Cultural video failed: {str(e)}",
|
| 676 |
+
"status": "error"
|
| 677 |
+
}), 500
|
| 678 |
+
|
| 679 |
+
@app.route('/api/animate-text', methods=['POST'])
|
| 680 |
+
def animate_text_endpoint():
|
| 681 |
+
"""Create animated text videos"""
|
| 682 |
+
try:
|
| 683 |
+
start_time = time.time()
|
| 684 |
+
data = request.get_json()
|
| 685 |
+
text = data.get('text', 'Hello World!')
|
| 686 |
+
animation_type = data.get('animation_type', 'pulse')
|
| 687 |
+
|
| 688 |
+
if not text:
|
| 689 |
+
return jsonify({"error": "Tafadhali provide text to animate"}), 400
|
| 690 |
+
|
| 691 |
+
# Generate text animation
|
| 692 |
+
video_data = video_gen.generate_animation_from_text(text)
|
| 693 |
+
|
| 694 |
+
if video_data:
|
| 695 |
+
generation_time = round(time.time() - start_time, 2)
|
| 696 |
+
|
| 697 |
+
return jsonify({
|
| 698 |
+
"video": video_data,
|
| 699 |
+
"text": text,
|
| 700 |
+
"animation_type": animation_type,
|
| 701 |
+
"status": "success",
|
| 702 |
+
"generation_time": generation_time,
|
| 703 |
+
"format": "mp4",
|
| 704 |
+
"duration": "3 seconds",
|
| 705 |
+
"message": "Text animation created!"
|
| 706 |
+
})
|
| 707 |
+
else:
|
| 708 |
+
return jsonify({
|
| 709 |
+
"error": "Pole! Text animation failed",
|
| 710 |
+
"status": "error"
|
| 711 |
+
}), 500
|
| 712 |
+
|
| 713 |
+
except Exception as e:
|
| 714 |
+
logger.error(f"Text animation error: {e}")
|
| 715 |
+
return jsonify({
|
| 716 |
+
"error": f"Pole! Text animation failed: {str(e)}",
|
| 717 |
+
"status": "error"
|
| 718 |
+
}), 500
|
| 719 |
+
|
| 720 |
+
@app.route('/api/create-slideshow', methods=['POST'])
|
| 721 |
+
def create_slideshow_endpoint():
|
| 722 |
+
"""Create video slideshow from images"""
|
| 723 |
+
try:
|
| 724 |
+
start_time = time.time()
|
| 725 |
+
data = request.get_json()
|
| 726 |
+
images = data.get('images', []) # List of base64 images
|
| 727 |
+
duration = data.get('duration', 2.0)
|
| 728 |
+
|
| 729 |
+
if not images or len(images) < 2:
|
| 730 |
+
return jsonify({
|
| 731 |
+
"error": "Tafadhali provide at least 2 images",
|
| 732 |
+
"status": "error"
|
| 733 |
+
}), 400
|
| 734 |
+
|
| 735 |
+
# Create slideshow
|
| 736 |
+
video_data = video_gen.create_slideshow_video(images, duration)
|
| 737 |
+
|
| 738 |
+
if video_data:
|
| 739 |
+
generation_time = round(time.time() - start_time, 2)
|
| 740 |
+
|
| 741 |
+
return jsonify({
|
| 742 |
+
"video": video_data,
|
| 743 |
+
"image_count": len(images),
|
| 744 |
+
"duration_per_image": duration,
|
| 745 |
+
"total_duration": len(images) * duration,
|
| 746 |
+
"status": "success",
|
| 747 |
+
"generation_time": generation_time,
|
| 748 |
+
"format": "mp4",
|
| 749 |
+
"message": f"Slideshow created with {len(images)} images"
|
| 750 |
+
})
|
| 751 |
+
else:
|
| 752 |
+
return jsonify({
|
| 753 |
+
"error": "Pole! Slideshow creation failed",
|
| 754 |
+
"status": "error"
|
| 755 |
+
}), 500
|
| 756 |
+
|
| 757 |
+
except Exception as e:
|
| 758 |
+
logger.error(f"Slideshow error: {e}")
|
| 759 |
+
return jsonify({
|
| 760 |
+
"error": f"Pole! Slideshow failed: {str(e)}",
|
| 761 |
+
"status": "error"
|
| 762 |
+
}), 500
|
| 763 |
+
|
| 764 |
+
@app.route('/api/image-to-video', methods=['POST'])
|
| 765 |
+
def image_to_video_endpoint():
|
| 766 |
+
"""Generate video from an image"""
|
| 767 |
+
try:
|
| 768 |
+
start_time = time.time()
|
| 769 |
+
data = request.get_json()
|
| 770 |
+
image_data = data.get('image', '')
|
| 771 |
+
prompt = data.get('prompt', '')
|
| 772 |
+
|
| 773 |
+
if not image_data:
|
| 774 |
+
return jsonify({"error": "Tafadhali provide an image"}), 400
|
| 775 |
+
|
| 776 |
+
# Generate video from image
|
| 777 |
+
video_data = video_gen.generate_image_to_video(image_data, prompt)
|
| 778 |
+
|
| 779 |
+
if video_data:
|
| 780 |
+
generation_time = round(time.time() - start_time, 2)
|
| 781 |
+
|
| 782 |
+
return jsonify({
|
| 783 |
+
"video": video_data,
|
| 784 |
+
"prompt": prompt if prompt else "Generated from image",
|
| 785 |
+
"status": "success",
|
| 786 |
+
"generation_time": generation_time,
|
| 787 |
+
"format": "mp4",
|
| 788 |
+
"duration": "4 seconds",
|
| 789 |
+
"provider": "hugging_face_free",
|
| 790 |
+
"message": "Video generated from image!"
|
| 791 |
+
})
|
| 792 |
+
else:
|
| 793 |
+
return jsonify({
|
| 794 |
+
"error": "Pole! Image to video conversion failed",
|
| 795 |
+
"status": "error"
|
| 796 |
+
}), 500
|
| 797 |
+
|
| 798 |
+
except Exception as e:
|
| 799 |
+
logger.error(f"Image to video error: {e}")
|
| 800 |
+
return jsonify({
|
| 801 |
+
"error": f"Pole! Image to video failed: {str(e)}",
|
| 802 |
+
"status": "error"
|
| 803 |
+
}), 500
|
| 804 |
+
|
| 805 |
+
@app.route('/api/video/info')
|
| 806 |
+
def video_info_endpoint():
|
| 807 |
+
"""Get information about video generation capabilities"""
|
| 808 |
+
try:
|
| 809 |
+
info = video_gen.get_video_info()
|
| 810 |
+
|
| 811 |
+
return jsonify({
|
| 812 |
+
"status": "success",
|
| 813 |
+
"video_capabilities": info,
|
| 814 |
+
"creator": "Stanley Samwel Owino",
|
| 815 |
+
"note": "Free video generation powered by Hugging Face open-source models",
|
| 816 |
+
"supported_formats": ["MP4", "WebM"],
|
| 817 |
+
"max_resolution": "576x320",
|
| 818 |
+
"typical_generation_time": "30-90 seconds",
|
| 819 |
+
"free_models_available": True
|
| 820 |
+
})
|
| 821 |
+
|
| 822 |
+
except Exception as e:
|
| 823 |
+
return jsonify({
|
| 824 |
+
"error": f"Pole! Could not get video info: {str(e)}",
|
| 825 |
+
"status": "error"
|
| 826 |
+
}), 500
|
| 827 |
+
|
| 828 |
+
@app.route('/api/detect-video-request', methods=['POST'])
|
| 829 |
+
def detect_video_request_endpoint():
|
| 830 |
+
"""Detect if user wants to generate a video"""
|
| 831 |
+
try:
|
| 832 |
+
data = request.get_json()
|
| 833 |
+
text = data.get('text', '')
|
| 834 |
+
|
| 835 |
+
if not text:
|
| 836 |
+
return jsonify({"error": "Tafadhali provide text"}), 400
|
| 837 |
+
|
| 838 |
+
is_video_request = video_gen.detect_video_request(text)
|
| 839 |
+
prompt = None
|
| 840 |
+
|
| 841 |
+
if is_video_request:
|
| 842 |
+
prompt = video_gen.extract_video_prompt(text)
|
| 843 |
+
|
| 844 |
+
return jsonify({
|
| 845 |
+
"is_video_request": is_video_request,
|
| 846 |
+
"extracted_prompt": prompt,
|
| 847 |
+
"suggested_endpoint": "/api/generate-video" if is_video_request else None,
|
| 848 |
+
"status": "success"
|
| 849 |
+
})
|
| 850 |
+
|
| 851 |
+
except Exception as e:
|
| 852 |
+
return jsonify({
|
| 853 |
+
"error": f"Pole! Detection failed: {str(e)}",
|
| 854 |
+
"status": "error"
|
| 855 |
+
}), 500
|
| 856 |
+
|
| 857 |
@app.route('/api/quick-chat', methods=['POST'])
|
| 858 |
def quick_chat():
|
| 859 |
"""Faster chat endpoint for simple queries"""
|
|
|
|
| 870 |
'hi': 'Habari! Karibu kwa Stanley AI. How can I help you today?',
|
| 871 |
'thanks': 'Asante sana! Karibu tena.',
|
| 872 |
'thank you': 'Asante! Happy to help.',
|
| 873 |
+
'help': 'Ninaweza kukupa: Maelezo, Picha, Video, Maarifa ya Kiswahili, na zaidi!',
|
| 874 |
'who created you': 'I was created by Stanley Samwel Owino, a Machine Learning Engineer from Kenya.',
|
| 875 |
'who made you': 'Stanley Samwel Owino - Machine Learning Engineer and AI researcher from Kenya.',
|
| 876 |
+
'creator': 'Stanley Samwel Owino - Machine Learning Engineer passionate about AI and cultural integration.',
|
| 877 |
+
'can you make videos': 'Ndio! I can create videos from text, images, and cultural themes. Try the video generation feature!',
|
| 878 |
+
'video capabilities': 'I can generate: 1) Text-to-video, 2) Image-to-video, 3) Slideshows, 4) Text animations, 5) Cultural theme videos',
|
| 879 |
+
'make a video': 'Sure! Just use the video generation endpoint or tell me what you want to create a video of.'
|
| 880 |
}
|
| 881 |
|
| 882 |
msg_lower = user_message.lower().strip()
|
|
|
|
| 905 |
"role": "Machine Learning Engineer",
|
| 906 |
"model_loaded": model_loaded,
|
| 907 |
"hugging_face_available": True,
|
| 908 |
+
"video_generation_available": True,
|
| 909 |
"cache_size": len(response_cache),
|
| 910 |
"features": [
|
| 911 |
"Text Generation",
|
| 912 |
"Image Generation",
|
| 913 |
+
"Video Generation",
|
| 914 |
"Kiswahili Knowledge",
|
| 915 |
"Cultural Integration",
|
| 916 |
"Fast Responses"
|
|
|
|
| 919 |
"Response Caching",
|
| 920 |
"API Fallbacks",
|
| 921 |
"Quick Responses",
|
| 922 |
+
"Cultural Prompts",
|
| 923 |
+
"Video Compression"
|
| 924 |
]
|
| 925 |
})
|
| 926 |
|
|
|
|
| 942 |
"status": "error"
|
| 943 |
}), 500
|
| 944 |
|
|
|
|
| 945 |
@app.route('/api/kiswahili/proverbs')
|
| 946 |
def get_proverbs():
|
| 947 |
"""Get random Swahili proverbs"""
|
|
|
|
| 971 |
"I'm fine": "Nzuri",
|
| 972 |
"Please": "Tafadhali",
|
| 973 |
"Yes": "Ndio",
|
| 974 |
+
"No": "Hapana",
|
| 975 |
+
"I love you": "Nakupenda",
|
| 976 |
+
"See you later": "Tutaonana",
|
| 977 |
+
"Congratulations": "Hongera",
|
| 978 |
+
"Good luck": "Bahati njema",
|
| 979 |
+
"Have a good day": "Siku njema"
|
| 980 |
}
|
| 981 |
return jsonify(phrases)
|
| 982 |
|
| 983 |
+
@app.route('/api/kiswahili/learn')
|
| 984 |
+
def learn_kiswahili():
|
| 985 |
+
"""Interactive Kiswahili learning"""
|
| 986 |
+
lessons = {
|
| 987 |
+
"basics": {
|
| 988 |
+
"greetings": {
|
| 989 |
+
"Habari": "Hello / How are you?",
|
| 990 |
+
"Nzuri": "Good / Fine",
|
| 991 |
+
"Asante": "Thank you",
|
| 992 |
+
"Karibu": "Welcome / You're welcome",
|
| 993 |
+
"Tafadhali": "Please"
|
| 994 |
+
},
|
| 995 |
+
"questions": {
|
| 996 |
+
"Unaitwa nani?": "What is your name?",
|
| 997 |
+
"Unatoka wapi?": "Where are you from?",
|
| 998 |
+
"Unaishi wapi?": "Where do you live?",
|
| 999 |
+
"Unafanya nini?": "What do you do?"
|
| 1000 |
+
},
|
| 1001 |
+
"responses": {
|
| 1002 |
+
"Ninaitwa...": "My name is...",
|
| 1003 |
+
"Ninatoka Kenya": "I am from Kenya",
|
| 1004 |
+
"Naishi Nairobi": "I live in Nairobi",
|
| 1005 |
+
"Mimi ni mwanafunzi": "I am a student"
|
| 1006 |
+
}
|
| 1007 |
+
},
|
| 1008 |
+
"numbers": {
|
| 1009 |
+
"1": "Moja",
|
| 1010 |
+
"2": "Mbili",
|
| 1011 |
+
"3": "Tatu",
|
| 1012 |
+
"4": "Nne",
|
| 1013 |
+
"5": "Tano",
|
| 1014 |
+
"10": "Kumi",
|
| 1015 |
+
"100": "Mia moja"
|
| 1016 |
+
},
|
| 1017 |
+
"culture": {
|
| 1018 |
+
"Hakuna Matata": "No worries / No problems",
|
| 1019 |
+
"Pole pole": "Slowly slowly (proverb)",
|
| 1020 |
+
"Haraka haraka haina baraka": "Hurry hurry has no blessing",
|
| 1021 |
+
"Jambo": "Hello / Matter / Issue"
|
| 1022 |
+
}
|
| 1023 |
+
}
|
| 1024 |
+
return jsonify({
|
| 1025 |
+
"lessons": lessons,
|
| 1026 |
+
"language": "Kiswahili",
|
| 1027 |
+
"region": "East Africa",
|
| 1028 |
+
"note": "Interactive learning by Stanley AI"
|
| 1029 |
+
})
|
| 1030 |
+
|
| 1031 |
if __name__ == '__main__':
|
| 1032 |
+
print("π STANLEY AI - Complete Edition with Video Generation")
|
| 1033 |
print("π¨βπ» Created by: Stanley Samwel Owino - Machine Learning Engineer")
|
| 1034 |
print("π Kiswahili Knowledge: Loaded")
|
| 1035 |
print("πΌοΈ Image Generation: Hugging Face API")
|
| 1036 |
+
print("π¬ Video Generation: Free Hugging Face Models")
|
| 1037 |
print("β‘ Performance: Optimized")
|
| 1038 |
print("π§ Fallbacks: Enabled")
|
| 1039 |
+
print("π API Endpoints: Active")
|
| 1040 |
+
print("=" * 50)
|
| 1041 |
+
print("Available Endpoints:")
|
| 1042 |
+
print("1. /api/chat - Text chat with AI")
|
| 1043 |
+
print("2. /api/generate-image - Image generation")
|
| 1044 |
+
print("3. /api/generate-video - Video generation from text")
|
| 1045 |
+
print("4. /api/generate-cultural-video - Cultural theme videos")
|
| 1046 |
+
print("5. /api/animate-text - Text animation")
|
| 1047 |
+
print("6. /api/create-slideshow - Slideshow from images")
|
| 1048 |
+
print("7. /api/image-to-video - Video from images")
|
| 1049 |
+
print("8. /api/quick-chat - Fast responses")
|
| 1050 |
+
print("9. /api/kiswahili/* - Kiswahili learning")
|
| 1051 |
+
print("=" * 50)
|
| 1052 |
|
| 1053 |
app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
|