\
import http.server, json, urllib.request
from socket import error as SocketError
CHAT_PAGE = b"""
YT Claw Chat
Ready
"""
class H(http.server.BaseHTTPRequestHandler):
def _cors(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
def do_OPTIONS(self):
self.send_response(200)
self._cors()
self.end_headers()
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self._cors()
self.end_headers()
self.wfile.write(CHAT_PAGE)
def do_POST(self):
if self.path == '/chat':
content_len = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_len)
try:
user_data = json.loads(body)
msgs = user_data.get('messages', [])
except:
msgs = []
# talk directly to Ollama's native chat API (port 11434)
payload = json.dumps({
'model': 'hf.co/bartowski/DeepSeek-R1-Distill-Qwen-14B-GGUF:IQ3_XS',
'messages': msgs,
'stream': False,
'options': {
'num_ctx': 131072,
'num_predict': 4096,
'temperature': 0.7
}
}).encode()
req = urllib.request.Request(
'http://127.0.0.1:11434/api/chat',
data=payload,
headers={'Content-Type': 'application/json'}
)
try:
with urllib.request.urlopen(req, timeout=600) as resp:
data = resp.read()
# Ollama returns {"model":"...","message":{"role":"assistant","content":"..."}}
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self._cors()
self.end_headers()
self.wfile.write(data)
except BrokenPipeError:
pass
except Exception as e:
try:
self.send_response(502)
self._cors()
self.end_headers()
self.wfile.write(json.dumps({'error': str(e)}).encode())
except:
pass
else:
self.send_response(200)
self._cors()
self.end_headers()
self.wfile.write(b'OK')
def log_message(self, *a):
pass
http.server.HTTPServer(('0.0.0.0', 8080), H).serve_forever()