import gradio as gr
from gradio_imageslider import ImageSlider
import os
import pprint
import sys
from pathlib import Path
from collections import OrderedDict
from PIL import Image
print('\n\n================================ START')
SEP = os.path.sep
BASE_URL = "https://huggingface.co/spaces/menorki/projet-moulin-belle-ile/resolve/main/"
BASE_PATH = Path(__file__).resolve().parent
BASE_DIR = str(BASE_PATH) + SEP
IN_SPACE = os.environ.get("SPACE_AUTHOR_NAME") in ["menorki"]
TMP_PATH = BASE_PATH / 'gradio_tmp'
TMP_DIR = str(TMP_PATH) + SEP
#if TMP_DIR.exists():
# shutil.rmtree(str(TMP_DIR))
TMP_PATH.mkdir(exist_ok=True, parents=True)
os.environ['GRADIO_TEMP_DIR'] = TMP_DIR
print(f"BASE_URL: {BASE_URL}")
print(f"BASE_DIR: {BASE_DIR}")
print(f"TMP_DIR: {TMP_DIR}")
print(f"IN_SPACE: {IN_SPACE}")
dir_models3D = "assets/windmill/models3D/"
dir_images = "assets/windmill/images/"
dir_videos = "assets/windmill/videos/"
# gr.set_static_paths(paths=[f"{BASE_DIR}assets/windmill"])
model3D_names = [f for f in os.listdir(dir_models3D) if f.endswith('.gltf') or f.endswith('.obj') or f.endswith('.glb')]
model3D_files = [BASE_URL + dir_models3D + name for name in model3D_names]
images_names = [f for f in os.listdir(dir_images) if f.endswith('.jpg') or f.endswith('.png')]
images_files = [(BASE_URL + dir_images + name , name) for name in images_names]
video_names = [f for f in os.listdir(dir_videos) if f.endswith('.mp4') or f.endswith('.avi')]
video_files = [BASE_URL + dir_videos + name for name in video_names]
DESCRIPTION = """# PROJET MOULIN SIMON """
css = '''
.gradio-container {max-width: 1280px !important; height:90%;}
#gallery { height: 90% !important; } /* Adjusted for padding/margin if any */
h1{text-align:center}
'''
head = '''
'''
def read_text_file(path:str)->str :
with open(path, 'r', encoding='utf-8') as file:
content = file.read()
return content
def read_PIL_images(filenames):
images = []
for filename in filenames:
try:
print(f'Loading {filename}......')
img = Image.open(filename)
images.append(img)
print(f'Loaded {filename}')
except Exception as e:
print(f"Error opening image {filename}: {e}")
return images
def build_comparison_slider(before_url , after_url):
# @see https://img-comparison-slider.sneas.io/examples.html#always-show
html = f'''
'''
return html
with gr.Blocks(analytics_enabled=False , head=head , css=css, theme="bethecloud/storj_theme" , elem_id='gradio-container') as demo:
gr.HTML(f'''
''')
with gr.Tab("LE PROJET....."):
with gr.Row():
with gr.Column(scale=1):
before = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-BEFORE.jpg'
after = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-AFTER.jpg'
gr.HTML(build_comparison_slider(before , after))
with gr.Column(scale=1):
gr.HTML(read_text_file("assets/html/intro.txt"))
with gr.Tab("Avant/Après"):
with gr.Row():
before = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-schema-BEFORE.jpg'
after = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-AFTER.jpg'
gr.HTML(build_comparison_slider(before , after))
before = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-BEFORE.jpg'
after = BASE_URL + 'assets/images/before-after/moulin-insitu-1-_1610534-AFTER.jpg'
gr.HTML(build_comparison_slider(before , after))
print(f'{BASE_URL}assets/images/before-after/moulin-insitu-1-_1610534-schema-BEFORE.jpg')
print(f'{BASE_URL}assets/images/before-after/moulin-insitu-1-_1610534-BEFORE.jpg')
print(f'{BASE_URL}assets/images/before-after/moulin-insitu-1-_1610534-AFTER.jpg')
with gr.Tab("Images"):
image_viewer = gr.Gallery(label="Generated images",
show_label=False,
elem_id='gallery',
columns=[3],
rows=[1],
object_fit="cover",
interactive=False,
value=images_files)
with gr.Tab("Modèles 3D"):
viewer_html = '''
'''
viewer_html = viewer_html.replace('DEFAULT_MODEL_URL' , BASE_URL + dir_models3D + model3D_names[0])
viewer_html = viewer_html.replace('BASE_URL_3D' , BASE_URL + dir_models3D)
google_viewer = gr.HTML(viewer_html)
with gr.Row(visible=True):
model_selection = gr.Radio(
show_label=True,
container=True,
interactive=True,
choices=model3D_names,
value=model3D_names[0],
label="Selectionner un modèle 3D :",
)
# js = f"(name) => document.getElementById('google-3D-viewer').src = '{BASE_URL + dir_models3D}' + name"
model_selection.change(fn=None,
inputs=model_selection,
outputs=None ,
js=f"(name) => document.getElementById('google-3D-viewer').src = '{BASE_URL + dir_models3D}' + name")
with gr.Tab("Videos"):
video_viewer = gr.Video(interactive=False, value=video_files[0])
with gr.Row(visible=True):
video_selection = gr.Radio(
show_label=True,
container=True,
interactive=True,
choices=video_names,
value=video_names[0],
label="Selectionner une video:"
)
def load_video(video_name):
video_file = BASE_URL + dir_videos + video_name
print(f'LOADING VIDEO: {video_file}')
return video_file
video_selection.change(fn=load_video, inputs=video_selection, outputs=video_viewer)
if __name__ == "__main__":
demo.launch(debug=not IN_SPACE, show_api=False)