An AI joke generator with topic and style selection, powered by Ollama running locally. Includes a Flask web UI to share jokes — zero cost, zero API key.
A well-structured prompt produces much better jokes than a simple "tell me a joke".
import requests, json
def generate_joke(topic='programming', style='pun', audience='general'):
style_map = {
'pun': 'a clever pun or wordplay joke',
'one-liner': 'a short, sharp one-liner',
'story': 'a short two-sentence story joke with a punchline',
'why': 'a "Why did the..." setup-punchline format',
}
audience_map = {
'general': 'suitable for all ages',
'tech': 'for developers and tech people',
'kids': 'appropriate for children',
}
prompt = f"""Tell me {style_map.get(style, 'a funny joke')} about {topic}.
The joke should be {audience_map.get(audience, 'suitable for all ages')}.
Keep it short and punchy. Output ONLY the joke — no explanation, no "Here is a joke".
Output the joke now:"""
r = requests.post(
'http://localhost:11434/api/generate',
json={'model': 'llama3.2', 'prompt': prompt, 'stream': False},
timeout=60,
)
return r.json()['response'].strip()
print(generate_joke('programming', 'pun'))
print(generate_joke('cats', 'one-liner'))
print(generate_joke('Python', 'why'))
Why do Python developers prefer dark mode? Because light attracts bugs! I asked my cat if she wanted to debug my code. She said no, she already found the purr-fect solution. Why did the Python developer quit their job? Because they didn't get arrays!
A simple web form to pick the topic and style and display the joke.
# app.py
from flask import Flask, render_template_string, request
import requests, json
app = Flask(__name__)
HTML = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AI Joke Generator</title>
<style>
* { margin:0; padding:0; box-sizing:border-box; }
body { font-family:'Segoe UI',sans-serif; background:#0f172a; color:#f8fafc; min-height:100vh; display:flex; align-items:center; justify-content:center; padding:20px; }
.card { background:#1e293b; border-radius:20px; padding:36px; max-width:480px; width:100%; text-align:center; }
h1 { font-size:1.6rem; margin-bottom:24px; }
label { display:block; text-align:left; font-size:0.8rem; color:#94a3b8; text-transform:uppercase; letter-spacing:0.06em; margin-bottom:6px; }
input, select { width:100%; padding:11px 14px; background:#0f172a; border:1px solid #334155; color:#fff; border-radius:10px; font-size:0.95rem; margin-bottom:16px; }
button { width:100%; padding:13px; background:#f59e0b; border:none; border-radius:10px; color:#0f172a; font-weight:800; font-size:1rem; cursor:pointer; margin-top:4px; }
.joke-box { background:#0f172a; border-radius:12px; padding:24px; margin-top:24px; font-size:1.1rem; line-height:1.6; min-height:80px; color:#f8fafc; display:{% if joke %}block{% else %}none{% endif %}; }
</style>
</head>
<body>
<?php require_once __DIR__ . '/../promo_banner.php'; ?>
<div class="card">
<h1>😂 AI Joke Generator</h1>
<form method="POST">
<label>Topic</label>
<input type="text" name="topic" value="{{ topic or 'programming' }}" placeholder="programming, cats, pizza...">
<label>Style</label>
<select name="style">
{% for s in ['pun','one-liner','story','why'] %}
<option value="{{ s }}" {% if style==s %}selected{% endif %}>{{ s | capitalize }}</option>
{% endfor %}
</select>
<button type="submit">Generate Joke</button>
</form>
{% if joke %}
<div class="joke-box">{{ joke }}</div>
{% endif %}
</div>
</body>
</html>"""
def make_joke(topic, style):
prompt = f"""Tell me a {style} joke about {topic}. Output ONLY the joke."""
r = requests.post('http://localhost:11434/api/generate',
json={'model':'llama3.2','prompt':prompt,'stream':False}, timeout=60)
return r.json()['response'].strip()
@app.route('/', methods=['GET','POST'])
def index():
joke = None; topic = 'programming'; style = 'pun'
if request.method == 'POST':
topic = request.form.get('topic','programming')
style = request.form.get('style','pun')
joke = make_joke(topic, style)
return render_template_string(HTML, joke=joke, topic=topic, style=style)
if __name__ == '__main__':
print("Open http://localhost:5000 in your browser")
app.run(debug=False)
You built a full-stack web app in 60 lines — Flask frontend + Ollama backend — running entirely on your laptop. Share it on your local network with app.run(host="0.0.0.0") so friends on the same WiFi can access it.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.