62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
from flask import Flask, render_template, Response, stream_with_context, request
|
|
from flask_flatpages import FlatPages
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
import requests
|
|
|
|
app = Flask(__name__)
|
|
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
|
|
|
|
app.config.update(
|
|
FLATPAGES_AUTO_RELOAD = True,
|
|
FLATPAGES_EXTENSION = '.md',
|
|
FLATPAGES_MARKDOWN_EXTENSIONS = ['fenced_code', 'tables']
|
|
)
|
|
|
|
pages = FlatPages(app)
|
|
|
|
@app.route('/')
|
|
def index():
|
|
# Sort posts by date metadata
|
|
posts = sorted(pages, key=lambda p: p.meta.get('date'), reverse=True)
|
|
return render_template('index.html', posts=posts)
|
|
|
|
@app.route('/about')
|
|
def about():
|
|
return render_template('about.html')
|
|
|
|
@app.route('/proxy-chat', methods=['POST'])
|
|
def proxy_chat():
|
|
target_url = "http://192.168.0.37:5002/v1/chat/completions"
|
|
|
|
# Forward the request to your local LLM
|
|
# We use stream=True here to get the chunks from the backend
|
|
req = requests.post(target_url, json=request.json, stream=True)
|
|
|
|
def generate():
|
|
# Yield each chunk as it arrives from the LLM
|
|
for chunk in req.iter_content(chunk_size=1024):
|
|
yield chunk
|
|
|
|
return Response(stream_with_context(generate()), content_type=req.headers['content-type'])
|
|
|
|
@app.route('/post/<path:path>/') # Adding /post/ prefix helps organize URLs
|
|
def post(path):
|
|
page = pages.get_or_404(path)
|
|
return render_template('post.html', page=page)
|
|
|
|
@app.route('/tag/<string:tag_name>/')
|
|
def tag(tag_name):
|
|
tagged_pages = [p for p in pages if tag_name in p.meta.get('tags', [])]
|
|
return render_template('tag.html', pages=tagged_pages, tag_name=tag_name)
|
|
|
|
@app.context_processor
|
|
def inject_tags():
|
|
all_tags = set()
|
|
for page in pages:
|
|
t = page.meta.get('tags', [])
|
|
if isinstance(t, list):
|
|
all_tags.update(t)
|
|
return dict(all_cloud_tags=sorted(list(all_tags)))
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host='0.0.0.0', port=5001, debug=True) |