Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import pyvista as pv
|
| 3 |
from pyvista import examples
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
import requests
|
| 7 |
from io import BytesIO
|
| 8 |
from PIL import Image
|
|
@@ -676,6 +676,19 @@ def create_360_viewer_html(image_paths, audio_paths, output_path):
|
|
| 676 |
|
| 677 |
return output_path
|
| 678 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
# Update the process_and_display function
|
| 680 |
def process_and_display(audio_input, generate_audio, chunk_duration):
|
| 681 |
# Validate chunk duration
|
|
@@ -732,8 +745,8 @@ def process_and_display(audio_input, generate_audio, chunk_duration):
|
|
| 732 |
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as tmp_file:
|
| 733 |
viewer_html_path = create_360_viewer_html(all_360_images, all_music_paths, tmp_file.name)
|
| 734 |
|
| 735 |
-
#
|
| 736 |
-
yield [gr.HTML("")] + group_visibility + outputs + [viewer_html_path,
|
| 737 |
|
| 738 |
# Update the clear_all function to handle the new outputs
|
| 739 |
def clear_all():
|
|
@@ -770,22 +783,60 @@ def load_example_audio(example_name):
|
|
| 770 |
return None
|
| 771 |
|
| 772 |
# Function to generate a shareable link
|
| 773 |
-
def generate_share_link():
|
| 774 |
try:
|
| 775 |
# Check if we're on Hugging Face Spaces
|
| 776 |
space_id = os.getenv('SPACE_ID')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
if space_id:
|
| 778 |
-
#
|
| 779 |
-
|
| 780 |
-
return f"
|
| 781 |
else:
|
| 782 |
-
#
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
return "Share link is not available. Make sure to set share=True when launching."
|
| 787 |
except Exception as e:
|
| 788 |
-
return f"Error generating share link: {str(e)}
|
|
|
|
|
|
|
| 789 |
|
| 790 |
|
| 791 |
|
|
@@ -892,6 +943,8 @@ with gr.Blocks(title="Affective Virtual Environments - Chunked Processing") as i
|
|
| 892 |
return example_path, example_name
|
| 893 |
|
| 894 |
# Set up the button clicks
|
|
|
|
|
|
|
| 895 |
process_btn.click(
|
| 896 |
fn=process_and_display,
|
| 897 |
inputs=[audio_input, generate_audio_checkbox, chunk_duration_input],
|
|
@@ -902,7 +955,7 @@ with gr.Blocks(title="Affective Virtual Environments - Chunked Processing") as i
|
|
| 902 |
container['image'],
|
| 903 |
container['image_360'],
|
| 904 |
container['music']
|
| 905 |
-
]] + [viewer_html_output, js_output]
|
| 906 |
)
|
| 907 |
|
| 908 |
clear_btn.click(
|
|
@@ -927,12 +980,12 @@ with gr.Blocks(title="Affective Virtual Environments - Chunked Processing") as i
|
|
| 927 |
# Set up the share button click
|
| 928 |
share_btn.click(
|
| 929 |
fn=generate_share_link,
|
| 930 |
-
inputs=[],
|
| 931 |
outputs=[share_output]
|
| 932 |
)
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
|
| 937 |
|
| 938 |
# Check if we're running on Hugging Face Spaces
|
|
|
|
| 2 |
import pyvista as pv
|
| 3 |
from pyvista import examples
|
| 4 |
import numpy as np
|
| 5 |
+
import librosaa
|
| 6 |
import requests
|
| 7 |
from io import BytesIO
|
| 8 |
from PIL import Image
|
|
|
|
| 676 |
|
| 677 |
return output_path
|
| 678 |
|
| 679 |
+
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
|
| 683 |
+
|
| 684 |
+
|
| 685 |
+
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
|
| 689 |
+
|
| 690 |
+
|
| 691 |
+
|
| 692 |
# Update the process_and_display function
|
| 693 |
def process_and_display(audio_input, generate_audio, chunk_duration):
|
| 694 |
# Validate chunk duration
|
|
|
|
| 745 |
with tempfile.NamedTemporaryFile(suffix=".html", delete=False) as tmp_file:
|
| 746 |
viewer_html_path = create_360_viewer_html(all_360_images, all_music_paths, tmp_file.name)
|
| 747 |
|
| 748 |
+
# After processing, return the results along with other outputs
|
| 749 |
+
yield [gr.HTML("")] + group_visibility + outputs + [viewer_html_path, js_output, results]
|
| 750 |
|
| 751 |
# Update the clear_all function to handle the new outputs
|
| 752 |
def clear_all():
|
|
|
|
| 783 |
return None
|
| 784 |
|
| 785 |
# Function to generate a shareable link
|
| 786 |
+
def generate_share_link(audio_input=None, generate_audio=True, chunk_duration=10, results=None):
|
| 787 |
try:
|
| 788 |
# Check if we're on Hugging Face Spaces
|
| 789 |
space_id = os.getenv('SPACE_ID')
|
| 790 |
+
|
| 791 |
+
# If we don't have results yet, just share the current app
|
| 792 |
+
if results is None:
|
| 793 |
+
if space_id:
|
| 794 |
+
space_url = f"https://huggingface.co/spaces/{space_id}"
|
| 795 |
+
return f"Your Space is already public! Share this URL: {space_url}"
|
| 796 |
+
else:
|
| 797 |
+
if hasattr(interface, 'share_url') and interface.share_url:
|
| 798 |
+
return "Share this URL to let others use the app: " + interface.share_url
|
| 799 |
+
else:
|
| 800 |
+
return "Share link is not available. Make sure to set share=True when launching."
|
| 801 |
+
|
| 802 |
+
# If we have results, create a shareable link with the data
|
| 803 |
+
# For simplicity, we'll just create a JSON representation of the results
|
| 804 |
+
# In a real implementation, you might want to store this data in a database
|
| 805 |
+
# and generate a unique ID for it
|
| 806 |
+
|
| 807 |
+
# Prepare data for sharing
|
| 808 |
+
share_data = {
|
| 809 |
+
"audio_input": audio_input if isinstance(audio_input, str) else "uploaded_audio",
|
| 810 |
+
"generate_audio": generate_audio,
|
| 811 |
+
"chunk_duration": chunk_duration,
|
| 812 |
+
"results": []
|
| 813 |
+
}
|
| 814 |
+
|
| 815 |
+
# Add result data (simplified for sharing)
|
| 816 |
+
for result in results:
|
| 817 |
+
share_data["results"].append({
|
| 818 |
+
"chunk_index": result.get("chunk_index", 0),
|
| 819 |
+
"emotion": result.get("emotion", ""),
|
| 820 |
+
"transcription": result.get("transcription", ""),
|
| 821 |
+
"sentiment": result.get("sentiment", "")
|
| 822 |
+
})
|
| 823 |
+
|
| 824 |
+
# Encode the data as a URL parameter
|
| 825 |
+
encoded_data = urllib.parse.quote(json.dumps(share_data))
|
| 826 |
+
|
| 827 |
if space_id:
|
| 828 |
+
# On Spaces, we can't easily create direct links to results
|
| 829 |
+
# So we'll just show the data and tell users to visit the space
|
| 830 |
+
return f"Share this data with others and have them visit https://huggingface.co/spaces/{space_id} to see similar results:\n\n{json.dumps(share_data, indent=2)}"
|
| 831 |
else:
|
| 832 |
+
# For local deployment, we can create a link with the data
|
| 833 |
+
# In a real implementation, you'd need a way to load this data
|
| 834 |
+
return f"To share these results, copy this data and have the recipient load it in the app:\n\n{json.dumps(share_data, indent=2)}"
|
| 835 |
+
|
|
|
|
| 836 |
except Exception as e:
|
| 837 |
+
return f"Error generating share link: {str(e)}"
|
| 838 |
+
|
| 839 |
+
|
| 840 |
|
| 841 |
|
| 842 |
|
|
|
|
| 943 |
return example_path, example_name
|
| 944 |
|
| 945 |
# Set up the button clicks
|
| 946 |
+
|
| 947 |
+
# Update the process_btn click handler to include results in the output
|
| 948 |
process_btn.click(
|
| 949 |
fn=process_and_display,
|
| 950 |
inputs=[audio_input, generate_audio_checkbox, chunk_duration_input],
|
|
|
|
| 955 |
container['image'],
|
| 956 |
container['image_360'],
|
| 957 |
container['music']
|
| 958 |
+
]] + [viewer_html_output, js_output, gr.State()]
|
| 959 |
)
|
| 960 |
|
| 961 |
clear_btn.click(
|
|
|
|
| 980 |
# Set up the share button click
|
| 981 |
share_btn.click(
|
| 982 |
fn=generate_share_link,
|
| 983 |
+
inputs=[audio_input, generate_audio_checkbox, chunk_duration_input, gr.State()],
|
| 984 |
outputs=[share_output]
|
| 985 |
)
|
| 986 |
+
|
| 987 |
+
# Add a state component to store the results
|
| 988 |
+
results_state = gr.State()
|
| 989 |
|
| 990 |
|
| 991 |
# Check if we're running on Hugging Face Spaces
|