Mystic / app.py
Jofax's picture
Create app.py
e94cebc verified
import gradio as gr
import cv2
import numpy as np
import torch
from transformers import pipeline
from PIL import Image, ImageDraw, ImageFont
import textwrap
import tempfile
# --- THE ENGINE ---
oracle_pipe = pipeline("text-generation", model="Qwen/Qwen2.5-1.5B-Instruct", device="cpu", torch_dtype=torch.float32)
def analyze_vital_resonance(frame):
if frame is None: return 0
h, w, _ = frame.shape
roi = frame[int(h*0.3):int(h*0.6), int(w*0.3):int(w*0.7)]
return int(65 + (np.mean(roi[:, :, 1]) % 35))
def get_aura_color(vibe):
if vibe > 85: return (255, 77, 77) # Red
if vibe > 75: return (212, 175, 55) # Gold
return (123, 47, 247) # Violet
def forge_talisman(name, vibe, decree):
# Create an Obsidian Canvas
canvas = Image.new('RGB', (800, 1000), color=(2, 2, 5))
draw = ImageDraw.Draw(canvas)
color = get_aura_color(vibe)
# Draw Borders
draw.rectangle([20, 20, 780, 980], outline=color, width=3)
draw.rectangle([30, 30, 770, 970], outline=color, width=1)
# Text Placement
try: font_main = ImageFont.load_default() # In a real env, you'd link a .ttf
except: font_main = None
draw.text((400, 100), "AETHER DECREE", fill=color, anchor="ms")
draw.text((400, 160), f"INITIATE: {name.upper()}", fill=(255, 255, 255), anchor="ms")
draw.text((400, 200), f"RESONANCE: {vibe}Hz", fill=color, anchor="ms")
# Wrap and Draw the Decree
lines = textwrap.wrap(decree, width=40)
y_text = 350
for line in lines:
draw.text((400, y_text), line, fill=(200, 200, 200), anchor="ms")
y_text += 40
# Save to temp file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
canvas.save(temp_file.name)
return temp_file.name
def transmute_reality(name, sigil, webcam, history):
vibe = analyze_vital_resonance(webcam)
aura_hex = '#%02x%02x%02x' % get_aura_color(vibe)
system_msg = f"You are the Aether Oracle. Initiate: {name}. Vibe: {vibe}Hz. Speak in mystic riddles."
messages = [{"role": "system", "content": system_msg}, {"role": "user", "content": "Decode me."}]
output = oracle_pipe(messages, max_new_tokens=150)
decree = output[0]['generated_text'][-1]['content']
# Forge the Image
talisman_path = forge_talisman(name, vibe, decree)
vibe_html = f"<h1 style='color: {aura_hex}; text-align: center;'>{vibe}Hz</h1>"
decree_html = f"<div style='color: {aura_hex}; font-size: 1.1rem;'>{decree}</div>"
return decree_html, vibe_html, talisman_path
# --- UI ---
with gr.Blocks(css=".gradio-container {background: #020205; color: #d4af37;}") as demo:
gr.Markdown("# 🔮 AETHER OS: THE FORGE")
with gr.Row():
with gr.Column():
name_box = gr.Textbox(label="Initiate Name")
camera = gr.Image(label="Biometric Lens", sources="webcam")
launch_btn = gr.Button("TRANSMUTE & FORGE", variant="primary")
with gr.Column():
vibe_out = gr.HTML()
text_out = gr.HTML()
talisman_out = gr.File(label="Download Your Talisman")
launch_btn.click(transmute_reality, [name_box, gr.State([]), camera, gr.State([])], [text_out, vibe_out, talisman_out])
demo.launch()