"""Chat de demostración: servido en el pod y accesible por túnel SSH."""
import json
import urllib.request
import urllib.error
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
MODEL = 'qwen3.5:9b'
PAGE = r'''
Mi IA · Runpod
La conversación comienza contigo.
Comprobando conexión…
El chat y el modelo se ejecutan en tu pod. Cerrar esta página no apaga el equipo.
'''
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass # No registrar conversaciones.
def send(self, code, body, mime='application/json'):
if not isinstance(body, bytes):
body=json.dumps(body,ensure_ascii=False).encode()
self.send_response(code)
self.send_header('Content-Type',mime+'; charset=utf-8')
self.send_header('Content-Length',str(len(body)))
self.send_header('Cache-Control','no-store')
self.send_header('X-Content-Type-Options','nosniff')
self.end_headers()
self.wfile.write(body)
def do_GET(self):
if self.path=='/':
return self.send(200,PAGE.encode(),'text/html')
if self.path=='/health':
try:
with urllib.request.urlopen('http://127.0.0.1:11434/api/tags',timeout=5) as r:
data=json.load(r)
ready=any(m.get('name')==MODEL for m in data.get('models',[]))
return self.send(200 if ready else 503,{'ready':ready})
except (OSError,ValueError):
return self.send(503,{'ready':False})
self.send(404,{'error':'Ruta inexistente'})
def do_POST(self):
if self.path!='/chat':return self.send(404,{'error':'Ruta inexistente'})
origin=self.headers.get('Origin')
if origin and origin not in ('http://'+self.headers.get('Host',''),'https://'+self.headers.get('Host','')):
return self.send(403,{'error':'Origen no permitido'})
try:
length=int(self.headers.get('Content-Length','0'))
if not 0