Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,9 +28,95 @@ class ImageScraper:
|
|
| 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'
|
| 30 |
}
|
| 31 |
-
# Add temp_dir attribute
|
| 32 |
self.temp_dir = Path(tempfile.mkdtemp())
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def generate_fallback_audio(self, script: str) -> AudioFileClip:
|
| 35 |
"""Generate fallback audio using gTTS"""
|
| 36 |
try:
|
|
@@ -40,22 +126,9 @@ class ImageScraper:
|
|
| 40 |
return AudioFileClip(str(audio_path))
|
| 41 |
except Exception as e:
|
| 42 |
print(f"Fallback audio generation failed: {e}")
|
| 43 |
-
|
| 44 |
-
duration = len(script.split()) * 0.3 # Approximate duration based on word count
|
| 45 |
return AudioFileClip(duration=duration)
|
| 46 |
-
|
| 47 |
-
def get_pixabay_images(self, query: str) -> List[str]:
|
| 48 |
-
try:
|
| 49 |
-
url = f"https://pixabay.com/api/?key={self.PIXABAY_API_KEY}&q={query.replace(' ', '+')}&image_type=photo&per_page=20"
|
| 50 |
-
response = requests.get(url)
|
| 51 |
-
if response.status_code == 200:
|
| 52 |
-
data = response.json()
|
| 53 |
-
if 'hits' in data:
|
| 54 |
-
return [img['largeImageURL'] for img in data['hits']]
|
| 55 |
-
return []
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"Pixabay API error: {e}")
|
| 58 |
-
return []
|
| 59 |
|
| 60 |
def get_stock_images(self) -> List[str]:
|
| 61 |
"""Return preset stock images as fallback"""
|
|
@@ -120,14 +193,6 @@ class ImageScraper:
|
|
| 120 |
print(f"Unsplash scraping error: {e}")
|
| 121 |
return urls
|
| 122 |
|
| 123 |
-
def get_images(self, query: str, num_images: int = 15) -> List[str]:
|
| 124 |
-
all_urls = []
|
| 125 |
-
all_urls.extend(self.scrape_pexels(query))
|
| 126 |
-
all_urls.extend(self.scrape_unsplash(query))
|
| 127 |
-
|
| 128 |
-
# Remove duplicates and limit to num_images
|
| 129 |
-
return list(set(all_urls))[:num_images]
|
| 130 |
-
|
| 131 |
class EnhancedVideoGenerator:
|
| 132 |
def __init__(self):
|
| 133 |
try:
|
|
@@ -696,70 +761,82 @@ class VideoGeneratorUI:
|
|
| 696 |
prompt = st.text_area("Enter your video script", height=200)
|
| 697 |
|
| 698 |
if prompt:
|
| 699 |
-
st.
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
output_path = f"vaultgenix_video_{int(time.time())}.mp4"
|
| 742 |
-
video_path = self.generator.create_video(
|
| 743 |
-
prompt,
|
| 744 |
-
style,
|
| 745 |
-
duration,
|
| 746 |
-
output_path,
|
| 747 |
-
selected_images
|
| 748 |
)
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
)
|
| 761 |
-
|
| 762 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 763 |
|
| 764 |
if __name__ == "__main__":
|
| 765 |
ui = VideoGeneratorUI()
|
|
|
|
| 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'
|
| 30 |
}
|
|
|
|
| 31 |
self.temp_dir = Path(tempfile.mkdtemp())
|
| 32 |
|
| 33 |
+
def get_pixabay_images(self, query: str) -> List[str]:
|
| 34 |
+
"""Get images from Pixabay API with enhanced error handling"""
|
| 35 |
+
try:
|
| 36 |
+
# Clean and encode the query
|
| 37 |
+
clean_query = query.replace(' ', '+').strip()
|
| 38 |
+
base_url = "https://pixabay.com/api/"
|
| 39 |
+
|
| 40 |
+
params = {
|
| 41 |
+
'key': self.PIXABAY_API_KEY,
|
| 42 |
+
'q': clean_query,
|
| 43 |
+
'image_type': 'photo',
|
| 44 |
+
'per_page': 20,
|
| 45 |
+
'safesearch': True,
|
| 46 |
+
'lang': 'en'
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
response = requests.get(base_url, params=params, headers=self.headers)
|
| 50 |
+
|
| 51 |
+
# Debug logging
|
| 52 |
+
print(f"Pixabay API URL: {response.url}")
|
| 53 |
+
print(f"Response status: {response.status_code}")
|
| 54 |
+
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
data = response.json()
|
| 57 |
+
print(f"Total hits: {data.get('totalHits', 0)}")
|
| 58 |
+
if 'hits' in data and data['hits']:
|
| 59 |
+
image_urls = [img['largeImageURL'] for img in data['hits']]
|
| 60 |
+
print(f"Found {len(image_urls)} images")
|
| 61 |
+
return image_urls
|
| 62 |
+
else:
|
| 63 |
+
print("No images found in response")
|
| 64 |
+
return self.get_stock_images()
|
| 65 |
+
else:
|
| 66 |
+
print(f"Pixabay API error: Status code {response.status_code}")
|
| 67 |
+
return self.get_stock_images()
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Exception in get_pixabay_images: {str(e)}")
|
| 71 |
+
return self.get_stock_images()
|
| 72 |
+
|
| 73 |
+
def get_stock_images(self) -> List[str]:
|
| 74 |
+
"""Return preset stock images as fallback"""
|
| 75 |
+
return [
|
| 76 |
+
"https://images.pexels.com/photos/60504/security-protection-anti-virus-software-60504.jpeg",
|
| 77 |
+
"https://images.pexels.com/photos/5380642/pexels-photo-5380642.jpeg",
|
| 78 |
+
"https://images.pexels.com/photos/2582937/pexels-photo-2582937.jpeg",
|
| 79 |
+
"https://images.pexels.com/photos/7319074/pexels-photo-7319074.jpeg",
|
| 80 |
+
"https://images.pexels.com/photos/4164418/pexels-photo-4164418.jpeg",
|
| 81 |
+
"https://images.pexels.com/photos/3861969/pexels-photo-3861969.jpeg",
|
| 82 |
+
"https://images.pexels.com/photos/5473298/pexels-photo-5473298.jpeg",
|
| 83 |
+
"https://images.pexels.com/photos/4348401/pexels-photo-4348401.jpeg",
|
| 84 |
+
"https://images.pexels.com/photos/8386440/pexels-photo-8386440.jpeg",
|
| 85 |
+
"https://images.pexels.com/photos/5473950/pexels-photo-5473950.jpeg"
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
def get_images(self, query: str, num_images: int = 15) -> List[str]:
|
| 89 |
+
"""Main method to get images with fallback options"""
|
| 90 |
+
# First try Pixabay
|
| 91 |
+
images = self.get_pixabay_images(query)
|
| 92 |
+
|
| 93 |
+
# If no Pixabay images, try with technology-related terms
|
| 94 |
+
if not images:
|
| 95 |
+
print("No images found for original query, trying tech terms...")
|
| 96 |
+
tech_terms = ["digital security", "technology", "cyber security", "data protection"]
|
| 97 |
+
for term in tech_terms:
|
| 98 |
+
images.extend(self.get_pixabay_images(term))
|
| 99 |
+
if images:
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
# If still no images, use stock images
|
| 103 |
+
if not images:
|
| 104 |
+
print("Using fallback stock images...")
|
| 105 |
+
images = self.get_stock_images()
|
| 106 |
+
|
| 107 |
+
# Remove duplicates and limit to num_images
|
| 108 |
+
unique_images = list(dict.fromkeys(images))[:num_images]
|
| 109 |
+
print(f"Returning {len(unique_images)} images")
|
| 110 |
+
return unique_images
|
| 111 |
+
|
| 112 |
+
def verify_image_url(self, url: str) -> bool:
|
| 113 |
+
"""Verify if an image URL is accessible"""
|
| 114 |
+
try:
|
| 115 |
+
response = requests.head(url, timeout=5)
|
| 116 |
+
return response.status_code == 200
|
| 117 |
+
except:
|
| 118 |
+
return False
|
| 119 |
+
|
| 120 |
def generate_fallback_audio(self, script: str) -> AudioFileClip:
|
| 121 |
"""Generate fallback audio using gTTS"""
|
| 122 |
try:
|
|
|
|
| 126 |
return AudioFileClip(str(audio_path))
|
| 127 |
except Exception as e:
|
| 128 |
print(f"Fallback audio generation failed: {e}")
|
| 129 |
+
duration = len(script.split()) * 0.3
|
|
|
|
| 130 |
return AudioFileClip(duration=duration)
|
| 131 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
def get_stock_images(self) -> List[str]:
|
| 134 |
"""Return preset stock images as fallback"""
|
|
|
|
| 193 |
print(f"Unsplash scraping error: {e}")
|
| 194 |
return urls
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
class EnhancedVideoGenerator:
|
| 197 |
def __init__(self):
|
| 198 |
try:
|
|
|
|
| 761 |
prompt = st.text_area("Enter your video script", height=200)
|
| 762 |
|
| 763 |
if prompt:
|
| 764 |
+
with st.spinner("Fetching images..."):
|
| 765 |
+
try:
|
| 766 |
+
st.subheader("Select Images for Your Video")
|
| 767 |
+
images = self.generator.image_scraper.get_images(prompt)
|
| 768 |
+
|
| 769 |
+
if not images:
|
| 770 |
+
st.warning("No images found. Loading default technology images...")
|
| 771 |
+
images = self.generator.image_scraper.get_stock_images()
|
| 772 |
+
|
| 773 |
+
# Image Selection Grid
|
| 774 |
+
selected_images = []
|
| 775 |
+
cols = st.columns(3)
|
| 776 |
+
|
| 777 |
+
for idx, img_url in enumerate(images):
|
| 778 |
+
with cols[idx % 3]:
|
| 779 |
+
try:
|
| 780 |
+
# Verify image URL before displaying
|
| 781 |
+
if self.generator.image_scraper.verify_image_url(img_url):
|
| 782 |
+
st.image(img_url, use_container_width=True)
|
| 783 |
+
if st.checkbox(f"Select Image {idx + 1}", key=f"img_{idx}"):
|
| 784 |
+
selected_images.append(img_url)
|
| 785 |
+
except Exception as e:
|
| 786 |
+
print(f"Error displaying image {idx}: {str(e)}")
|
| 787 |
+
continue
|
| 788 |
+
|
| 789 |
+
# Only show video settings if images are available
|
| 790 |
+
if selected_images:
|
| 791 |
+
st.subheader("Video Settings")
|
| 792 |
+
col1, col2 = st.columns(2)
|
| 793 |
+
with col1:
|
| 794 |
+
style = st.selectbox(
|
| 795 |
+
"Choose style",
|
| 796 |
+
options=["Professional", "Creative", "Educational"],
|
| 797 |
+
index=0
|
| 798 |
+
)
|
| 799 |
+
with col2:
|
| 800 |
+
duration = st.slider(
|
| 801 |
+
"Video duration (seconds)",
|
| 802 |
+
min_value=30,
|
| 803 |
+
max_value=180,
|
| 804 |
+
value=60,
|
| 805 |
+
step=30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
)
|
| 807 |
+
|
| 808 |
+
if st.button("Generate Video", type="primary"):
|
| 809 |
+
with st.spinner("Generating your video..."):
|
| 810 |
+
try:
|
| 811 |
+
output_path = f"vaultgenix_video_{int(time.time())}.mp4"
|
| 812 |
+
video_path = self.generator.create_video(
|
| 813 |
+
prompt,
|
| 814 |
+
style,
|
| 815 |
+
duration,
|
| 816 |
+
output_path,
|
| 817 |
+
selected_images
|
| 818 |
)
|
| 819 |
+
|
| 820 |
+
if os.path.exists(video_path):
|
| 821 |
+
st.success("✨ Video generated successfully!")
|
| 822 |
+
st.video(video_path)
|
| 823 |
+
|
| 824 |
+
with open(video_path, 'rb') as video_file:
|
| 825 |
+
st.download_button(
|
| 826 |
+
"⬇️ Download Video",
|
| 827 |
+
video_file.read(),
|
| 828 |
+
file_name=output_path,
|
| 829 |
+
mime="video/mp4"
|
| 830 |
+
)
|
| 831 |
+
except Exception as e:
|
| 832 |
+
st.error(f"Failed to generate video: {str(e)}")
|
| 833 |
+
print(f"Video generation error: {str(e)}")
|
| 834 |
+
else:
|
| 835 |
+
st.error("No valid images found. Please try a different search term.")
|
| 836 |
+
|
| 837 |
+
except Exception as e:
|
| 838 |
+
st.error(f"An error occurred: {str(e)}")
|
| 839 |
+
print(f"Setup error: {str(e)}")
|
| 840 |
|
| 841 |
if __name__ == "__main__":
|
| 842 |
ui = VideoGeneratorUI()
|