Uses AI to scare people...more.
import os
import glob

import streamlit as st
from elevenlabs import generate, save

from utils import answer_image_question, generate_greeting, generate_video


"""# scAIry

You think AI is scary? Wait till you see `scAIry` 👻
"""

@st.cache_resource
def get_image_list():
    return sorted(glob.glob(os.path.join("data", "photos", "*")))


images = get_image_list()

image_index = st.slider(
    "🎃 Choose your trick-or-treater", min_value=0, max_value=len(images) - 1
)
st.image(images[image_index])
with st.form("kiddos"):
    submitted = st.form_submit_button("Choose")
    if submitted:
        summary = answer_image_question(
            images[image_index], 
            "List each costume you can detect in the photo."
        )
        "## Summary of trick or treaters"
        st.write(summary)
        st.session_state["costume_summary"] = summary

if "costume_summary" in st.session_state:
    GREETERS = {
        "freddy_krueger": {
            "name": "Freddy Krueger",
            "voice_id": "0fTYMUuqMxZoSywLwS0b",
            "image": "freddy.jpg"
        },
        "chucky": {
            "name": "Chucky",
            "voice_id": "zJSt7YEaVvvPws74ziWG",
            "image": "chucky.jpg"
        }
    }

    def get_greeter_name(val):
        return GREETERS[val]["name"]

    with st.form("greeting"):
        greeter_key = st.selectbox(
            "Choose a scary greeter",
            options=GREETERS.keys(),
            format_func=get_greeter_name,
        )
        greeter = GREETERS[greeter_key]
        submitted = st.form_submit_button("Generate")
        if submitted:
            "## Scary Greeting"
            with st.spinner("Generating greeting..."):
                greeting = generate_greeting(
                    greeter["name"], st.session_state["costume_summary"]
                )
                st.write(greeting)

            with st.spinner("Generating audio..."):
                response = generate(greeting, voice=greeter["voice_id"])
                file_name = f"{greeter_key}-{image_index}.wav"
                file_path = os.path.join("data", "audio", file_name)
                save(response, file_path)
                st.audio(file_path)
                st.session_state["generated_audio"] = file_path

if "generated_audio" in st.session_state:
    greeter_image = os.path.join("data", "greeters", greeter["image"])
    st.image(greeter_image, greeter["name"])
    # This takes a bit
    with st.spinner("Generating video..."):
        output = generate_video(
            greeter_image, 
            st.session_state["generated_audio"]
        )
        print(output)
        st.video(output)


app.py
craigsdennis/scairy
import replicate


def answer_image_question(image_name, question):
    output = replicate.run(
        "andreasjansson/blip-2:4b32258c42e9efd4288bb9910bc532a69727f9acd26aa08e175713a0a857a608",
        input={"image": open(image_name, "rb"), "question": question},
    )
    return output


def generate_greeting(style, description_of_costumes):
    system_prompt = f"""
        You are going to scare trick or treaters in the style of {style}.

        This is for Halloween, so you should be as creepy and scary as you possibly can.

        Try to keep things short.

        The user is going to explain who is there costume wise and you should use your scary style of {style} but address the trick or treaters to scare them personally.
    """
    output = replicate.run(
        "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
        input={
            "prompt": description_of_costumes,
            "system_prompt": system_prompt,
        },
    )

    return "".join(output) 


def generate_video(image_file_name, audio_file_name):
    print("image_file_name", image_file_name)
    print("audio_fie_nae", audio_file_name)
    output = replicate.run(
        "cjwbw/sadtalker:3aa3dac9353cc4d6bd62a8f95957bd844003b401ca4e4a9b33baa574c549d376",
        input={
            "source_image": open(image_file_name, "rb"),
            "driven_audio": open(audio_file_name, "rb"),
        }
    )
    return output
utils.py
craigsdennis/scairy