import os import random import configparser import requests from flask import Flask, render_template_string, jsonify, send_from_directory app = Flask(__name__) OLLAMA_HOST = os.environ.get("OLLAMA_HOST", "http://harbor.freshbrewed.science:11444") OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL", "gemma4:e4b") PROMPT = os.environ.get("JOKE_PROMPT", "tell me a dad joke about programming") PORT = int(os.environ.get("PORT", "80")) PRESET_JOKES = [ "What do you call parenting with a software engineer? Pair programming (or O-pair programming).", "My kids were rolling on the floor and screaming. My wife asked what was happening, and I told her it was a version conflict.", "When I wrote this code, only God and I knew how it worked. Now, only God knows.", "A programmer is sent to the store by his wife: “Get a gallon of milk, and if they have eggs, get a dozen.” The programmer returns home with 13 gallons of milk and says, “They had eggs”.", "Where do coder dads store all their projects? In a dad-abase.", "Why don't programmers like going outside into nature? Too many bugs.", "Why did the programmer die taking a shower? The shampoo bottle instructions said: Lather. Rinse. Repeat." ] def get_app_version(): config = configparser.ConfigParser() version_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "version.ini") if os.path.exists(version_file): try: config.read(version_file) return config.get("metadata", "version", fallback="unknown") except Exception: return "unknown" return "unknown" HTML_TEMPLATE = """ Programming Dad Joke

Programming Dad Joke

Dry humor for engineers after hours, pulled live from your local model.

{{ joke }}

Three rules on the press

Every groaner on the sheet clears three checks before it leaves the studio.

  1. Pun on the literal

    Take a noun every developer says ten times before lunch, such as cache, branch, or deadlock, and read it as plain kitchen English.

  2. Keep the setup short

    One question across the counter. If the premise needs a whiteboard diagram, the punchline already missed its train.

  3. Aim for the eye roll

    A proper dad joke never chases an applause break. It settles for a quiet sigh from across the table.

""" def request_joke(): url = f"{OLLAMA_HOST.rstrip('/')}/api/generate" payload = { "model": OLLAMA_MODEL, "prompt": PROMPT, "stream": False } try: resp = requests.post(url, json=payload, timeout=60) resp.raise_for_status() data = resp.json() return data.get("response", "No response received from Ollama.") except Exception as e: return f"Error contacting Ollama ({OLLAMA_HOST}): {e}" @app.route("/") def index(): joke = random.choice(PRESET_JOKES) version = get_app_version() return render_template_string( HTML_TEMPLATE, joke=joke, host=OLLAMA_HOST, model=OLLAMA_MODEL, prompt=PROMPT, version=version ) @app.route("/tokens.css") def tokens_css(): return send_from_directory(os.path.dirname(os.path.abspath(__file__)), "tokens.css", mimetype="text/css") @app.route("/base.css") def base_css(): return send_from_directory(os.path.dirname(os.path.abspath(__file__)), "base.css", mimetype="text/css") @app.route("/api/joke") def api_joke(): joke = request_joke() version = get_app_version() return jsonify({ "joke": joke, "version": version }) if __name__ == "__main__": version = get_app_version() print(f"Starting server v{version} on port {PORT} with model '{OLLAMA_MODEL}' at {OLLAMA_HOST}...") app.run(host="0.0.0.0", port=PORT)