Spaces:
Sleeping
Sleeping
Download app.py from ProgramComputer/Speech2Face: direct link, hf CLI and curl.
- Browser
- Download file 5.4 kB
-
https://huggingface.co/spaces/ProgramComputer/Speech2Face/resolve/main/app.py
- Command line
-
hf download hf://spaces/ProgramComputer/Speech2Face/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ProgramComputer/Speech2Face/resolve/main/app.py
5.4 kB
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| from threading import Lock, Thread | |
| from typing import Any | |
| from s2f import runtime as _runtime | |
| os.environ["GRADIO_ANALYTICS_ENABLED"] = "False" | |
| import gradio as gr | |
| from s2f.pipeline import PAIRS, SpeechToFace | |
| from example_data import load_examples | |
| ASSETS = Path(os.environ.get("S2F_ASSETS", Path(__file__).parent / "assets")) | |
| MODEL = SpeechToFace(ASSETS) | |
| _PAIR_LOAD_LOCKS = {pair: Lock() for pair in PAIRS} | |
| _MODEL_LOAD_LOCK = Lock() | |
| _READY_PAIRS: set[str] = set() | |
| EXAMPLES = load_examples(Path(os.environ.get("S2F_EXAMPLES", Path(__file__).parent / "examples"))) | |
| TITLE = "Speech2Face" | |
| INTRO = ( | |
| "Record or upload about six seconds of speech. It guesses a frontal, neutral face: " | |
| "a rough, average-looking estimate, not the speaker's real face. " | |
| "Do not use it to identify anyone." | |
| ) | |
| STORY = ( | |
| "Follows Speech2Face (Oh et al., 2019) and Cole et al. (2017), with substitutions: " | |
| "voice to VGG-Face features to face. Trained on AVSpeech and VGGFace2." | |
| ) | |
| CREDITS = ( | |
| "**Credits**\n" | |
| "- Speech2Face: Oh et al., CVPR 2019. Face decoder: Cole et al., CVPR 2017. " | |
| "This is an independent version with substitutions.\n" | |
| "- AVSpeech (Ephrat et al., 2018): annotations © Google LLC, " | |
| "[CC BY 4.0](https://creativecommons.org/licenses/by/4.0/). " | |
| "The videos belong to their owners.\n" | |
| "- VGGFace2 (Cao et al., 2018). No images are included.\n" | |
| "- VGG-Face (Parkhi et al., 2015), CC BY-NC 4.0. Not included; these models depend on it.\n" | |
| "- The faces can reflect biases in the training data.\n" | |
| "- Licence: [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/), " | |
| "non-commercial use only." | |
| ) | |
| def warm_models() -> None: | |
| failed = False | |
| for pair in PAIRS: | |
| try: | |
| with _PAIR_LOAD_LOCKS[pair]: | |
| if pair not in _READY_PAIRS: | |
| with _MODEL_LOAD_LOCK: | |
| MODEL.voice(pair) | |
| MODEL.decoder(pair) | |
| _READY_PAIRS.add(pair) | |
| except Exception: | |
| if not failed: | |
| print("Model warm-up failed; requests will retry loading.", flush=True) | |
| failed = True | |
| def generate(audio: str | None, pair_label: str) -> Any: | |
| if audio is None: | |
| raise gr.Error("Record or upload a voice first.") | |
| pair = next(key for key, label in PAIRS.items() if label == pair_label) | |
| with _PAIR_LOAD_LOCKS[pair]: | |
| if pair not in _READY_PAIRS: | |
| with _MODEL_LOAD_LOCK: | |
| result = MODEL(audio, pair) | |
| _READY_PAIRS.add(pair) | |
| return result | |
| return MODEL(audio, pair) | |
| def load_example(index: int | str | None) -> tuple[str | None, str | None, None]: | |
| if index is None: | |
| return None, None, None | |
| item = EXAMPLES[int(index)] | |
| return item["audio"], item["audio"], None | |
| with gr.Blocks(title=TITLE, analytics_enabled=False) as demo: | |
| gr.Markdown(f"# {TITLE}\n{INTRO}") | |
| with gr.Tab("Try it"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| audio = gr.Audio( | |
| sources=["microphone", "upload"], | |
| type="filepath", | |
| format=None, | |
| label="Your voice (about 6 s)", | |
| buttons=[], | |
| ) | |
| pair = gr.Radio(list(PAIRS.values()), value=PAIRS["arcface"], label="Decoder") | |
| button = gr.Button("Generate face", variant="primary") | |
| with gr.Column(): | |
| face = gr.Image(label="Generated face", type="numpy", height=320) | |
| button.click(generate, inputs=[audio, pair], outputs=face) | |
| if EXAMPLES: | |
| with gr.Accordion("Examples", open=True): | |
| gr.Markdown( | |
| "CC BY covers these clips; it does not give consent from the people shown." | |
| ) | |
| gr.Markdown("To request removal of an example, use this Space's Community tab.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| example = gr.Dropdown( | |
| [(f"Example {i + 1}", i) for i in range(len(EXAMPLES))], | |
| value=0, | |
| label="Example voice", | |
| ) | |
| load_button = gr.Button("Load example") | |
| example_audio = gr.Audio( | |
| type="filepath", | |
| format=None, | |
| label="Example voice", | |
| interactive=False, | |
| buttons=[], | |
| ) | |
| load_button.click( | |
| load_example, | |
| inputs=example, | |
| outputs=[audio, example_audio, face], | |
| ) | |
| with gr.Tab("How it was built"): | |
| gr.Markdown(f"{STORY}\n\n{CREDITS}") | |
| if EXAMPLES: | |
| example_credits = gr.Textbox( | |
| value="\n\n".join( | |
| f"Example {i + 1}: {item['credit']}" for i, item in enumerate(EXAMPLES) | |
| ), | |
| label="Example credits", | |
| lines=12, | |
| interactive=False, | |
| ) | |
| if __name__ == "__main__": | |
| Thread(target=warm_models, daemon=True).start() | |
| demo.launch() | |