Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,8 +22,7 @@ from bs4 import BeautifulSoup
|
|
| 22 |
import requests
|
| 23 |
from io import BytesIO
|
| 24 |
|
| 25 |
-
|
| 26 |
-
def __init__(self):
|
| 27 |
self.PIXABAY_API_KEY = "48069976-37e20099248207cee12385560"
|
| 28 |
self.headers = {
|
| 29 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
@@ -116,17 +115,104 @@ class ImageScraper:
|
|
| 116 |
return category
|
| 117 |
return 'general'
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def get_images_for_keyword(self, keyword: str) -> List[Dict[str, str]]:
|
| 120 |
-
"""Get images for a specific keyword with
|
| 121 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
base_url = "https://pixabay.com/api/"
|
| 123 |
params = {
|
| 124 |
'key': self.PIXABAY_API_KEY,
|
| 125 |
-
'q':
|
| 126 |
'image_type': 'photo',
|
| 127 |
'per_page': 5,
|
| 128 |
'safesearch': True,
|
| 129 |
-
'lang': 'en'
|
|
|
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
response = requests.get(base_url, params=params, headers=self.headers)
|
|
@@ -137,10 +223,11 @@ class ImageScraper:
|
|
| 137 |
return [{
|
| 138 |
'url': img['largeImageURL'],
|
| 139 |
'keyword': keyword,
|
| 140 |
-
'relevance': 'Primary match' if keyword in img['tags'] else 'Related',
|
| 141 |
'tags': img['tags']
|
| 142 |
} for img in data['hits']]
|
| 143 |
return []
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error fetching images for keyword {keyword}: {e}")
|
| 146 |
return []
|
|
@@ -791,41 +878,6 @@ class EnhancedVideoGenerator:
|
|
| 791 |
|
| 792 |
return text.strip()
|
| 793 |
|
| 794 |
-
def extract_key_topics(self, script: str) -> List[str]:
|
| 795 |
-
"""Extract key topics from a long text prompt"""
|
| 796 |
-
try:
|
| 797 |
-
# Common tech-related terms to look for
|
| 798 |
-
tech_terms = {
|
| 799 |
-
'security', 'digital', 'technology', 'data', 'encryption',
|
| 800 |
-
'privacy', 'protection', 'software', 'platform', 'system',
|
| 801 |
-
'AI', 'artificial intelligence', 'computing', 'cyber'
|
| 802 |
-
}
|
| 803 |
-
|
| 804 |
-
# Split into words and get unique terms
|
| 805 |
-
words = script.lower().split()
|
| 806 |
-
topics = set()
|
| 807 |
-
|
| 808 |
-
# Extract single word topics
|
| 809 |
-
for word in words:
|
| 810 |
-
if word in tech_terms:
|
| 811 |
-
topics.add(word)
|
| 812 |
-
|
| 813 |
-
# Extract two-word phrases
|
| 814 |
-
for i in range(len(words) - 1):
|
| 815 |
-
phrase = f"{words[i]} {words[i+1]}"
|
| 816 |
-
if any(term in phrase for term in tech_terms):
|
| 817 |
-
topics.add(phrase)
|
| 818 |
-
|
| 819 |
-
# Add some default topics if none found
|
| 820 |
-
if not topics:
|
| 821 |
-
topics = {'digital security', 'data protection', 'technology'}
|
| 822 |
-
|
| 823 |
-
return list(topics)[:5] # Return top 5 topics
|
| 824 |
-
|
| 825 |
-
except Exception as e:
|
| 826 |
-
print(f"Topic extraction error: {e}")
|
| 827 |
-
return ['digital security']
|
| 828 |
-
|
| 829 |
def generate_ai_image(self, prompt: str, style: str) -> Optional[Image.Image]:
|
| 830 |
"""Generate an AI image using Stability AI"""
|
| 831 |
try:
|
|
|
|
| 22 |
import requests
|
| 23 |
from io import BytesIO
|
| 24 |
|
| 25 |
+
def __init__(self):
|
|
|
|
| 26 |
self.PIXABAY_API_KEY = "48069976-37e20099248207cee12385560"
|
| 27 |
self.headers = {
|
| 28 |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
|
|
|
| 115 |
return category
|
| 116 |
return 'general'
|
| 117 |
|
| 118 |
+
|
| 119 |
+
def extract_key_topics(self, script: str) -> List[str]:
|
| 120 |
+
"""Extract key topics from a long text prompt with improved accuracy"""
|
| 121 |
+
try:
|
| 122 |
+
# Define relevant categories for VaultGenix
|
| 123 |
+
categories = {
|
| 124 |
+
'security': ['security', 'encryption', 'protection', 'privacy', 'safe', 'secure'],
|
| 125 |
+
'digital': ['digital', 'online', 'virtual', 'cyber', 'electronic'],
|
| 126 |
+
'legacy': ['legacy', 'inheritance', 'heir', 'posthumous', 'estate'],
|
| 127 |
+
'management': ['management', 'planning', 'organization', 'control', 'administration'],
|
| 128 |
+
'technology': ['AI', 'artificial intelligence', 'technology', 'platform', 'system'],
|
| 129 |
+
'family': ['family', 'heir', 'custodian', 'relative', 'loved ones']
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
# Process text
|
| 133 |
+
text = script.lower()
|
| 134 |
+
found_topics = set()
|
| 135 |
+
|
| 136 |
+
# Extract single-word matches
|
| 137 |
+
words = text.split()
|
| 138 |
+
for category, terms in categories.items():
|
| 139 |
+
for term in terms:
|
| 140 |
+
if term in text:
|
| 141 |
+
found_topics.add(term)
|
| 142 |
+
found_topics.add(category)
|
| 143 |
+
|
| 144 |
+
# Extract meaningful phrases
|
| 145 |
+
important_phrases = [
|
| 146 |
+
'digital legacy',
|
| 147 |
+
'legacy management',
|
| 148 |
+
'digital security',
|
| 149 |
+
'data protection',
|
| 150 |
+
'artificial intelligence',
|
| 151 |
+
'digital estate',
|
| 152 |
+
'digital identity',
|
| 153 |
+
'secure platform',
|
| 154 |
+
'family protection',
|
| 155 |
+
'digital inheritance'
|
| 156 |
+
]
|
| 157 |
+
|
| 158 |
+
for phrase in important_phrases:
|
| 159 |
+
if phrase in text:
|
| 160 |
+
found_topics.add(phrase)
|
| 161 |
+
|
| 162 |
+
# Combine related topics
|
| 163 |
+
combined_topics = []
|
| 164 |
+
for topic in found_topics:
|
| 165 |
+
# Create meaningful combinations
|
| 166 |
+
if topic in ['digital', 'secure', 'smart', 'AI']:
|
| 167 |
+
related = ['legacy', 'security', 'protection', 'management']
|
| 168 |
+
for rel in related:
|
| 169 |
+
if rel in found_topics:
|
| 170 |
+
combined_topics.append(f"{topic} {rel}")
|
| 171 |
+
|
| 172 |
+
# Add combined topics to results
|
| 173 |
+
found_topics.update(combined_topics)
|
| 174 |
+
|
| 175 |
+
# Prioritize topics
|
| 176 |
+
priority_topics = [
|
| 177 |
+
topic for topic in found_topics
|
| 178 |
+
if any(key in topic for key in ['digital', 'security', 'legacy', 'AI'])
|
| 179 |
+
]
|
| 180 |
+
|
| 181 |
+
# Ensure we have enough topics
|
| 182 |
+
if len(priority_topics) < 3:
|
| 183 |
+
priority_topics.extend(['digital security', 'legacy management', 'data protection'][:3 - len(priority_topics)])
|
| 184 |
+
|
| 185 |
+
return list(set(priority_topics))[:5] # Return top 5 unique topics
|
| 186 |
+
|
| 187 |
+
except Exception as e:
|
| 188 |
+
print(f"Topic extraction error: {e}")
|
| 189 |
+
return ['digital security', 'legacy management', 'data protection']
|
| 190 |
+
|
| 191 |
def get_images_for_keyword(self, keyword: str) -> List[Dict[str, str]]:
|
| 192 |
+
"""Get images for a specific keyword with improved relevance"""
|
| 193 |
try:
|
| 194 |
+
# Enhance keyword for better search results
|
| 195 |
+
enhanced_keywords = {
|
| 196 |
+
'digital': 'digital technology security',
|
| 197 |
+
'security': 'cybersecurity protection',
|
| 198 |
+
'legacy': 'digital legacy inheritance',
|
| 199 |
+
'management': 'digital management system',
|
| 200 |
+
'AI': 'artificial intelligence technology',
|
| 201 |
+
'protection': 'data protection security'
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
search_term = enhanced_keywords.get(keyword, keyword)
|
| 205 |
+
|
| 206 |
base_url = "https://pixabay.com/api/"
|
| 207 |
params = {
|
| 208 |
'key': self.PIXABAY_API_KEY,
|
| 209 |
+
'q': search_term,
|
| 210 |
'image_type': 'photo',
|
| 211 |
'per_page': 5,
|
| 212 |
'safesearch': True,
|
| 213 |
+
'lang': 'en',
|
| 214 |
+
'category': 'technology', # Focus on technology category
|
| 215 |
+
'orientation': 'horizontal' # Better for video
|
| 216 |
}
|
| 217 |
|
| 218 |
response = requests.get(base_url, params=params, headers=self.headers)
|
|
|
|
| 223 |
return [{
|
| 224 |
'url': img['largeImageURL'],
|
| 225 |
'keyword': keyword,
|
| 226 |
+
'relevance': 'Primary match' if keyword.lower() in img['tags'].lower() else 'Related',
|
| 227 |
'tags': img['tags']
|
| 228 |
} for img in data['hits']]
|
| 229 |
return []
|
| 230 |
+
|
| 231 |
except Exception as e:
|
| 232 |
print(f"Error fetching images for keyword {keyword}: {e}")
|
| 233 |
return []
|
|
|
|
| 878 |
|
| 879 |
return text.strip()
|
| 880 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 881 |
def generate_ai_image(self, prompt: str, style: str) -> Optional[Image.Image]:
|
| 882 |
"""Generate an AI image using Stability AI"""
|
| 883 |
try:
|