import os
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"))
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
Skip to content
Programming Dad Joke
Dry humor for engineers after hours, pulled live from your local model.
Hedvig Letters Serif, 25pt / 1.4
{{ joke }}
Setting type on the press (30 to 50s typical)…
0% (0s)
Proofed on {{ model }}
Release v{{ version }}
Three rules on the press
Every groaner on the sheet clears three checks before it leaves the studio.
-
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.
-
Keep the setup short
One question across the counter. If the premise needs a whiteboard diagram, the punchline already missed its train.
-
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 = request_joke()
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)