Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,6 +30,16 @@ from gradio_client import Client
|
|
| 30 |
from urllib.parse import quote
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# 🎭 App Configuration - Because every app needs a good costume!
|
| 34 |
Site_Name = '🐙GitCosmos🌌 - AI Azure Cosmos DB and Github Agent'
|
| 35 |
title = "🐙GitCosmos🌌 - AI Azure Cosmos DB and Github Agent"
|
|
@@ -797,6 +807,107 @@ def get_video_html(video_path, width="100%"):
|
|
| 797 |
</video>
|
| 798 |
'''
|
| 799 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 800 |
# Function to create HTML for audio player (when life needs a soundtrack 🎶)
|
| 801 |
def get_audio_html(audio_path, width="100%"):
|
| 802 |
audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
|
|
|
|
| 30 |
from urllib.parse import quote
|
| 31 |
|
| 32 |
|
| 33 |
+
# Add these imports at the top of your file
|
| 34 |
+
from gradio_client import Client, handle_file
|
| 35 |
+
import tempfile
|
| 36 |
+
from PIL import Image
|
| 37 |
+
import io
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
# 🎭 App Configuration - Because every app needs a good costume!
|
| 44 |
Site_Name = '🐙GitCosmos🌌 - AI Azure Cosmos DB and Github Agent'
|
| 45 |
title = "🐙GitCosmos🌌 - AI Azure Cosmos DB and Github Agent"
|
|
|
|
| 807 |
</video>
|
| 808 |
'''
|
| 809 |
|
| 810 |
+
|
| 811 |
+
|
| 812 |
+
|
| 813 |
+
# *********
|
| 814 |
+
|
| 815 |
+
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
|
| 819 |
+
def generate_video_from_image(image_data, seed=None, motion_bucket_id=127, fps_id=6):
|
| 820 |
+
"""Generate video from image using Stable Video Diffusion"""
|
| 821 |
+
try:
|
| 822 |
+
# Create a temporary file to save the uploaded image
|
| 823 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_img:
|
| 824 |
+
# If image_data is a PIL Image, convert to bytes
|
| 825 |
+
if isinstance(image_data, Image.Image):
|
| 826 |
+
img_byte_arr = io.BytesIO()
|
| 827 |
+
image_data.save(img_byte_arr, format='PNG')
|
| 828 |
+
temp_img.write(img_byte_arr.getvalue())
|
| 829 |
+
else:
|
| 830 |
+
temp_img.write(image_data)
|
| 831 |
+
temp_img_path = temp_img.name
|
| 832 |
+
|
| 833 |
+
# Initialize the Gradio client
|
| 834 |
+
client = Client("awacke1/stable-video-diffusion")
|
| 835 |
+
|
| 836 |
+
# Get random seed if none provided
|
| 837 |
+
if seed is None:
|
| 838 |
+
seed = client.predict(api_name="/get_random_value")
|
| 839 |
+
|
| 840 |
+
# Generate video
|
| 841 |
+
result = client.predict(
|
| 842 |
+
image=temp_img_path,
|
| 843 |
+
seed=seed,
|
| 844 |
+
randomize_seed=True,
|
| 845 |
+
motion_bucket_id=motion_bucket_id,
|
| 846 |
+
fps_id=fps_id,
|
| 847 |
+
api_name="/video"
|
| 848 |
+
)
|
| 849 |
+
|
| 850 |
+
# result[0] contains Dict with video path
|
| 851 |
+
video_path = result[0]['video']
|
| 852 |
+
return video_path, result[1] # Return video path and used seed
|
| 853 |
+
|
| 854 |
+
except Exception as e:
|
| 855 |
+
st.error(f"Error generating video: {str(e)}")
|
| 856 |
+
return None, None
|
| 857 |
+
|
| 858 |
+
# Add this to the 'Show as Run AI' section in your main function,
|
| 859 |
+
# right after the "🤖 Run AI" button:
|
| 860 |
+
|
| 861 |
+
# Add image upload and video generation
|
| 862 |
+
st.image_uploader = st.file_uploader("Upload Image for Video Generation 🖼️", type=['png', 'jpg', 'jpeg'])
|
| 863 |
+
st.video_gen_params = {
|
| 864 |
+
'motion_bucket_id': st.slider("Motion Intensity 🌊", 1, 255, 127),
|
| 865 |
+
'fps_id': st.slider("Frames per Second 🎬", 1, 30, 6)
|
| 866 |
+
}
|
| 867 |
+
|
| 868 |
+
if st.image_uploader is not None:
|
| 869 |
+
if st.button("🎥 Generate Video"):
|
| 870 |
+
with st.spinner("Generating video... 🎬"):
|
| 871 |
+
# Read uploaded image
|
| 872 |
+
image_bytes = st.image_uploader.read()
|
| 873 |
+
|
| 874 |
+
# Generate video
|
| 875 |
+
video_path, used_seed = generate_video_from_image(
|
| 876 |
+
image_bytes,
|
| 877 |
+
motion_bucket_id=st.video_gen_params['motion_bucket_id'],
|
| 878 |
+
fps_id=st.video_gen_params['fps_id']
|
| 879 |
+
)
|
| 880 |
+
|
| 881 |
+
if video_path:
|
| 882 |
+
# Save video to local storage
|
| 883 |
+
video_filename = f"generated_video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
| 884 |
+
shutil.copy(video_path, video_filename)
|
| 885 |
+
|
| 886 |
+
st.success(f"Video generated successfully! Seed used: {used_seed}")
|
| 887 |
+
|
| 888 |
+
# Display the generated video
|
| 889 |
+
st.video(video_filename)
|
| 890 |
+
|
| 891 |
+
# Save to Cosmos DB if needed
|
| 892 |
+
if container:
|
| 893 |
+
video_record = {
|
| 894 |
+
"id": generate_unique_id(),
|
| 895 |
+
"type": "generated_video",
|
| 896 |
+
"filename": video_filename,
|
| 897 |
+
"seed": used_seed,
|
| 898 |
+
"motion_bucket_id": st.video_gen_params['motion_bucket_id'],
|
| 899 |
+
"fps_id": st.video_gen_params['fps_id'],
|
| 900 |
+
"timestamp": datetime.now().isoformat()
|
| 901 |
+
}
|
| 902 |
+
success, message = insert_record(container, video_record)
|
| 903 |
+
if success:
|
| 904 |
+
st.success("Video record saved to database")
|
| 905 |
+
else:
|
| 906 |
+
st.error(f"Error saving video record: {message}")
|
| 907 |
+
|
| 908 |
+
|
| 909 |
+
# ******************************************
|
| 910 |
+
|
| 911 |
# Function to create HTML for audio player (when life needs a soundtrack 🎶)
|
| 912 |
def get_audio_html(audio_path, width="100%"):
|
| 913 |
audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
|