This is my submission for the Hacktoberfest Open-Source AI Challenge: Week 4 (theme: Ghost in the Machine).
What I built
Haunted Haiku is a very small app. You type a word or a mood ("empty house", "candle", "fog"), and a local model writes an eerie haiku about it. Then the app does three more things with it:
- Paints it. The same model picks two dark, haunting colours and a few spooky emoji, and the app draws the poem as a card: a two-colour gradient, soft bokeh lights and a big, faint emoji scene behind the text. You can save the card as a PNG.
- Reads it aloud. One button sends the poem to ElevenLabs text-to-speech and plays it back.
- Hums a soundtrack. Another button asks ElevenLabs Music for a 20-second instrumental that fits the poem, and loops it under the reading.
My reading of the theme: the "ghost in the machine" is the voice that whispers a poem back at you from inside your own laptop, on the thin line between the living and the dead (Halloween, Día de los Muertos). The writing is not a cloud service. It is an open-weight model, Gemma 3, running through Ollama on localhost.
Why open, and why local
-
The poet works with the Wi-Fi off. The only call that writes the poem goes to
localhost:11434. - What you type stays on the machine. A haiku prompt is not sensitive, but the same code works for things that are, like a remembrance for someone you lost.
-
The model is a one-line swap.
HAIKU_MODEL=gemma3:12b(or any other Ollama model) changes the poet without touching the code.
To be straight about it: the voice and the music are the exception. They come from ElevenLabs, a hosted service, and need an API key and internet. They are optional buttons. The haiku and the picture work fully offline.
How it works
One Python file, Dash with Dash Mantine Components for the UI, and plain requests for the model call. No SDK for the model.
The interesting part is that a single request returns both the poem and the art direction, as JSON:
PROMPT = (
"Write an eerie, haunted English {form} about: {topic}. Make it spooky: ghosts, whispers, candlelight, fog, "
"the thin line between the living and the dead (Halloween and Dia de los Muertos spirit). "
"Also design a picture for it. "
"Reply with JSON only: {{\"lines\": [{n} strings], \"emoji\": \"1 to 3 spooky emoji that show the scene\", "
"\"from\": a colour, \"to\": a different colour}}. Colours must come from: " + ", ".join(COLORS) + ". "
"Pick dark, haunting colours that match the mood (e.g. midnight indigo, deep violet, candle orange).")
def haiku(topic, n=3):
r = requests.post(f"{OLLAMA_URL}/api/chat", timeout=120, json={
"model": MODEL, "stream": False, "format": "json", "options": {"temperature": 0.9},
"messages": [{"role": "user", "content": PROMPT.format(topic=topic, form=form, n=n)}]})
r.raise_for_status()
d = json.loads(r.json()["message"]["content"])
...
Setting "format": "json" makes Ollama constrain the output to valid JSON. The colours are limited to Mantine's palette names, and the "picture" is plain CSS. There is no image model involved: a gradient, soft light spots, a large translucent emoji and the poem on top. You can also pick the colour yourself, or leave the switch on and let the model choose. A small stepper changes the poem from 1 to 10 lines.
The voice and the music
Both are a few lines with the ElevenLabs Python SDK, and both return an mp3 as a data URI, so the browser can play it with a plain html.Audio and nothing is written to disk:
def speak(text):
client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
audio = client.text_to_speech.convert(text=text, voice_id=ELEVEN_VOICE, model_id="eleven_v4")
return "data:audio/mpeg;base64," + base64.b64encode(b"".join(audio)).decode()
def music(text):
client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
audio = client.music.compose(
prompt=f"Eerie, haunted instrumental ambient music, slow and quiet, for this poem: {text}",
music_length_ms=20000, force_instrumental=True)
return "data:audio/mpeg;base64," + base64.b64encode(b"".join(audio)).decode()
The poem itself is the music prompt, so a poem about fog and a poem about candlelight get different soundtracks. force_instrumental=True keeps the track from singing over the voice.
Things that went wrong (and what I did about them)
-
The model does not always follow the schema. I asked for
"emoji": "1 to 3 emoji"as a string. Sometimes it returns a list instead. My first version printed"['🌧️', '🍂', "into the picture. The fix is three lines: if it is a list, join it. - Colours need validating. The model can answer with a colour that is not in the palette, or the same colour twice. I check both and fall back to a default pair.
- 5-7-5 is a hint, not a guarantee. Gemma does not count syllables reliably. Most haiku come out close, but not all are exactly 5-7-5.
-
My app said "needs a paid plan" for every music error, and it was wrong. Reading aloud worked, but music failed. The real error (401,
missing_permissions) said my ElevenLabs API key had nomusic_generationpermission. Keys can be restricted per feature, and mine only had text-to-speech. I turned the permission on, and the same call returned audio. If I did it again I would show the API's own message in the app instead of a guess.
Run it
Code: banana0000/haiku
git clone https://github.com/banana0000/haiku
cd haiku
ollama pull gemma3:4b
pip install -r requirements.txt
# optional, for the voice and the music:
echo "ELEVENLABS_API_KEY=your-key" > .env
python app.py
Then open http://localhost:8063. There is no login. Type a word, press Summon a haiku (or Enter), then try Read aloud, Add music and Save as PNG. The music button needs an API key with the Music Generation permission.
Prize categories
-
Best Use of Gemma: Gemma 3 (
gemma3:4b) writes the poem and chooses its colours and emoji. It is the only model that writes anything. - Best Use of ElevenLabs: the poem is read aloud with ElevenLabs text-to-speech, and ElevenLabs Music composes a short instrumental soundtrack from the poem itself.
Reflection
The pleasant surprise was how far a small model goes when you ask it for structure instead of just text. One request, one JSON object, and the model is both the poet and the art director. Adding a voice and a soundtrack changed how the whole thing feels: a card on a screen is a picture, but a card that whispers its own poem over a low drone is a little ghost. The unpleasant surprise was how often a small model almost follows a schema. Validating its output is not optional.
Top comments (0)