Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,164 +1,269 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
from datasets import load_dataset, Dataset
|
| 6 |
-
from transformers import pipeline, AutoProcessor, WhisperForConditionalGeneration
|
| 7 |
-
from diffusers import StableDiffusionPipeline
|
| 8 |
-
import sounddevice as sd
|
| 9 |
import librosa
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|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gr.Button("💨
|
| 109 |
-
mint_btn = gr.Button("🪙 MINT FARTWORK
|
| 110 |
-
|
| 111 |
with gr.Tab("Fartifact™ Capsule"):
|
| 112 |
-
gr.
|
| 113 |
-
output_audio = gr.Audio(label="Fart Audio", interactive=False)
|
| 114 |
output_idea = gr.Textbox(label="Idea Capsule")
|
| 115 |
-
output_art = gr.Image(label="
|
| 116 |
-
output_review = gr.Textbox(label="
|
| 117 |
-
tts_output = gr.Audio(label="
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
dataset_view = gr.Dataframe(headers=["
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
record_btn.click(
|
| 125 |
-
fn=
|
| 126 |
-
|
|
|
|
| 127 |
)
|
| 128 |
-
|
| 129 |
-
# Minting mechanism
|
| 130 |
mint_btn.click(
|
| 131 |
-
fn=
|
| 132 |
-
inputs=[
|
| 133 |
-
outputs=[dataset_view]
|
| 134 |
)
|
| 135 |
|
| 136 |
-
# ==============
|
| 137 |
-
# FART LEDGER
|
| 138 |
-
# ==============
|
| 139 |
-
class FartDatabase:
|
| 140 |
-
def __init__(self):
|
| 141 |
-
self.schema = {
|
| 142 |
-
"id": Value("string"),
|
| 143 |
-
"timestamp": Value("string"),
|
| 144 |
-
"audio": Audio(sampling_rate=16000),
|
| 145 |
-
"idea": Value("string"),
|
| 146 |
-
"fart_type": Value("string"),
|
| 147 |
-
"art": Image(),
|
| 148 |
-
"review": Value("string"),
|
| 149 |
-
"token_value": Value("float32")
|
| 150 |
-
}
|
| 151 |
-
self.dataset = Dataset.from_dict({k: [] for k in self.schema.keys()})
|
| 152 |
-
|
| 153 |
-
def add_item(self, capsule):
|
| 154 |
-
"""Add Fartifact™ to decentralized ledger"""
|
| 155 |
-
self.dataset = self.dataset.add_item(capsule)
|
| 156 |
-
self.dataset.push_to_hub("fart_db", private=False)
|
| 157 |
-
return self.dataset.to_pandas().tail(10)
|
| 158 |
-
|
| 159 |
-
# ==============
|
| 160 |
-
# START FARTING
|
| 161 |
-
# ==============
|
| 162 |
if __name__ == "__main__":
|
| 163 |
-
|
| 164 |
-
ui.launch(server_port=7860, share=True)
|
|
|
|
| 1 |
+
import os, io, json, hashlib, datetime, threading
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
import numpy as np
|
| 5 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import librosa
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
from transformers import pipeline, AutoProcessor, WhisperForConditionalGeneration
|
| 11 |
+
|
| 12 |
+
from datasets import Dataset, Features, Value, Audio, Image as HFImage
|
| 13 |
+
|
| 14 |
+
# ---------- Runtime / device ----------
|
| 15 |
+
def device_map():
|
| 16 |
+
if torch.cuda.is_available():
|
| 17 |
+
return {"device": "cuda", "dtype": torch.float16}
|
| 18 |
+
if torch.backends.mps.is_available(): # Apple
|
| 19 |
+
return {"device": "mps", "dtype": torch.float16}
|
| 20 |
+
return {"device": "cpu", "dtype": torch.float32}
|
| 21 |
+
|
| 22 |
+
RUNTIME = device_map()
|
| 23 |
+
|
| 24 |
+
# ---------- Persistence (local ETL) ----------
|
| 25 |
+
DATA_DIR = Path("./data")
|
| 26 |
+
ART_DIR = DATA_DIR / "art"
|
| 27 |
+
DB_PARQUET = DATA_DIR / "fart_db.parquet"
|
| 28 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 29 |
+
ART_DIR.mkdir(parents=True, exist_ok=True)
|
| 30 |
+
_db_lock = threading.Lock()
|
| 31 |
+
|
| 32 |
+
DB_FEATURES = Features({
|
| 33 |
+
"id": Value("string"),
|
| 34 |
+
"timestamp": Value("string"),
|
| 35 |
+
"audio_path": Value("string"),
|
| 36 |
+
"idea": Value("string"),
|
| 37 |
+
"fart_type": Value("string"),
|
| 38 |
+
"art_path": Value("string"),
|
| 39 |
+
"review": Value("string"),
|
| 40 |
+
"laugh_verified": Value("bool"),
|
| 41 |
+
"token_value": Value("float32"),
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
def load_ledger() -> Dataset:
|
| 45 |
+
if DB_PARQUET.exists():
|
| 46 |
+
return Dataset.from_parquet(str(DB_PARQUET))
|
| 47 |
+
return Dataset.from_dict({k: [] for k in DB_FEATURES.keys()}).cast(DB_FEATURES)
|
| 48 |
+
|
| 49 |
+
def save_ledger(ds: Dataset) -> None:
|
| 50 |
+
with _db_lock:
|
| 51 |
+
ds.to_parquet(str(DB_PARQUET))
|
| 52 |
+
|
| 53 |
+
LEDGER = load_ledger()
|
| 54 |
+
|
| 55 |
+
# ---------- Models (lazy init where expensive) ----------
|
| 56 |
+
# Audio classifier (emotion model repurposed as proxy demo; deterministic label selection)
|
| 57 |
+
_audio_cls = pipeline(
|
| 58 |
+
"audio-classification",
|
| 59 |
+
model="superb/hubert-base-superb-er",
|
| 60 |
+
device=0 if RUNTIME["device"] == "cuda" else -1
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Whisper small (local)
|
| 64 |
+
_processor = AutoProcessor.from_pretrained("openai/whisper-small")
|
| 65 |
+
_asr = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
|
| 66 |
+
_asr = _asr.to(RUNTIME["device"]).to(dtype=RUNTIME["dtype"])
|
| 67 |
+
|
| 68 |
+
# Optional: Stable Diffusion (will be disabled if no GPU; safe fallback image if CPU-only)
|
| 69 |
+
_sd_pipe = None
|
| 70 |
+
if RUNTIME["device"] == "cuda":
|
| 71 |
+
try:
|
| 72 |
+
from diffusers import StableDiffusionPipeline
|
| 73 |
+
_sd_pipe = StableDiffusionPipeline.from_pretrained(
|
| 74 |
+
"stabilityai/stable-diffusion-2-1",
|
| 75 |
+
torch_dtype=torch.float16
|
| 76 |
+
).to("cuda")
|
| 77 |
+
except Exception:
|
| 78 |
+
_sd_pipe = None
|
| 79 |
+
|
| 80 |
+
# ---------- Utility ----------
|
| 81 |
+
def _mono_float32(wave: np.ndarray) -> np.ndarray:
|
| 82 |
+
if wave.ndim == 2:
|
| 83 |
+
wave = np.mean(wave, axis=1)
|
| 84 |
+
wave = wave.astype(np.float32)
|
| 85 |
+
# normalize if outside [-1,1]
|
| 86 |
+
mx = np.max(np.abs(wave)) + 1e-8
|
| 87 |
+
if mx > 1.0:
|
| 88 |
+
wave = wave / mx
|
| 89 |
+
return wave
|
| 90 |
+
|
| 91 |
+
def _hash_bytes(b: bytes) -> str:
|
| 92 |
+
return hashlib.sha256(b).hexdigest()
|
| 93 |
+
|
| 94 |
+
def _deterministic_value(s: str) -> float:
|
| 95 |
+
# Map SHA256 -> [0, 1) via first 8 bytes
|
| 96 |
+
h = hashlib.sha256(s.encode()).digest()[:8]
|
| 97 |
+
n = int.from_bytes(h, "big")
|
| 98 |
+
return (n % 10_000_000) / 10_000_000.0
|
| 99 |
+
|
| 100 |
+
# ---------- Core pipeline ----------
|
| 101 |
+
def detect_fart(audio_tuple):
|
| 102 |
+
sr, wave = audio_tuple
|
| 103 |
+
wave = _mono_float32(wave)
|
| 104 |
+
out = _audio_cls({"array": wave, "sampling_rate": sr})
|
| 105 |
+
# Take top label
|
| 106 |
+
label = max(out, key=lambda x: float(x["score"]))["label"]
|
| 107 |
+
return label
|
| 108 |
+
|
| 109 |
+
def transcribe_idea(audio_tuple):
|
| 110 |
+
sr, wave = audio_tuple
|
| 111 |
+
wave = _mono_float32(wave)
|
| 112 |
+
inputs = _processor(wave, sampling_rate=sr, return_tensors="pt")
|
| 113 |
+
with torch.inference_mode():
|
| 114 |
+
input_feats = inputs.input_features.to(RUNTIME["device"])
|
| 115 |
+
pred_ids = _asr.generate(input_feats)
|
| 116 |
+
text = _processor.batch_decode(pred_ids, skip_special_tokens=True)[0].strip()
|
| 117 |
+
return text
|
| 118 |
+
|
| 119 |
+
def generate_art(idea: str, art_id: str) -> str:
|
| 120 |
+
out_path = ART_DIR / f"{art_id}.png"
|
| 121 |
+
if _sd_pipe is None:
|
| 122 |
+
# Fallback: render text as simple image (CPU-safe)
|
| 123 |
+
img = Image.new("RGB", (768, 512), (0, 0, 0))
|
| 124 |
+
# Minimal pillow text (no additional deps): leave clean black image with no text to avoid font issues
|
| 125 |
+
img.save(out_path)
|
| 126 |
+
return str(out_path)
|
| 127 |
+
prompt = f"surreal, absurd, high-contrast, orange accents on black, {idea}"
|
| 128 |
+
with torch.inference_mode():
|
| 129 |
+
img = _sd_pipe(prompt).images[0]
|
| 130 |
+
img.save(out_path)
|
| 131 |
+
return str(out_path)
|
| 132 |
+
|
| 133 |
+
def review_text(idea: str, fart_type: str) -> str:
|
| 134 |
+
# Lightweight deterministic roast without LLM (keyless)
|
| 135 |
+
base = f"VC Review | type={fart_type} | idea='{idea[:120]}'"
|
| 136 |
+
score = _deterministic_value(idea + fart_type)
|
| 137 |
+
tier = "reject" if score < 0.33 else ("revise" if score < 0.66 else "fund")
|
| 138 |
+
return f"{base} | decision={tier} | score={score:.3f}"
|
| 139 |
+
|
| 140 |
+
def laugh_to_mint(audio_tuple) -> bool:
|
| 141 |
+
sr, wave = audio_tuple
|
| 142 |
+
wave = _mono_float32(wave)
|
| 143 |
+
# Energy-based laugh heuristic (deterministic threshold on log-energy variance)
|
| 144 |
+
frame = max(2048, int(0.05 * sr))
|
| 145 |
+
hop = frame // 2
|
| 146 |
+
rmse = librosa.feature.rms(y=wave, frame_length=frame, hop_length=hop)[0]
|
| 147 |
+
v = float(np.var(np.log(rmse + 1e-8)))
|
| 148 |
+
return v > 0.25
|
| 149 |
+
|
| 150 |
+
def create_capsule(audio_tuple, idea: str, art_path: str, review: str, fart_type: str):
|
| 151 |
+
sr, wave = audio_tuple
|
| 152 |
+
wave = _mono_float32(wave)
|
| 153 |
+
timestamp = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()
|
| 154 |
+
audio_bytes = wave.tobytes()
|
| 155 |
+
audio_hash = _hash_bytes(audio_bytes)
|
| 156 |
+
cid = f"fart-{audio_hash[:8]}"
|
| 157 |
+
|
| 158 |
+
# Persist audio as WAV (float32 PCM via soundfile; avoid PyAV)
|
| 159 |
+
audio_path = DATA_DIR / f"{cid}.wav"
|
| 160 |
+
try:
|
| 161 |
+
import soundfile as sf
|
| 162 |
+
sf.write(str(audio_path), wave, sr)
|
| 163 |
+
except Exception:
|
| 164 |
+
# Fallback: numpy npy
|
| 165 |
+
np.save(str(DATA_DIR / f"{cid}.npy"), wave)
|
| 166 |
+
audio_path = DATA_DIR / f"{cid}.npy"
|
| 167 |
+
|
| 168 |
+
value = 0.1 + 0.2 * _deterministic_value(idea) + 0.3 * _deterministic_value(review)
|
| 169 |
+
|
| 170 |
+
capsule = {
|
| 171 |
+
"id": cid,
|
| 172 |
+
"timestamp": timestamp,
|
| 173 |
+
"audio_path": str(audio_path),
|
| 174 |
+
"idea": idea,
|
| 175 |
+
"fart_type": fart_type,
|
| 176 |
+
"art_path": art_path,
|
| 177 |
+
"review": review,
|
| 178 |
+
"laugh_verified": True,
|
| 179 |
+
"token_value": float(value),
|
| 180 |
+
}
|
| 181 |
+
return capsule
|
| 182 |
+
|
| 183 |
+
def add_to_ledger(capsule: dict) -> Dataset:
|
| 184 |
+
global LEDGER
|
| 185 |
+
with _db_lock:
|
| 186 |
+
LEDGER = LEDGER.add_item(capsule)
|
| 187 |
+
save_ledger(LEDGER)
|
| 188 |
+
return LEDGER
|
| 189 |
+
|
| 190 |
+
# ---------- Gradio UI ----------
|
| 191 |
+
THEME_CSS = """
|
| 192 |
+
.gradio-container {background-color:#0b0b0b}
|
| 193 |
+
button, .tab-nav button {border-radius:10px}
|
| 194 |
+
:root {--button-primary-background-fill:#ff7a00; --button-primary-text-color:#000}
|
| 195 |
+
label, .markdown-body, .label-wrap, .tabs {color:#ffb26b}
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
state_capsule = gr.State(value=None)
|
| 199 |
+
|
| 200 |
+
def process_handler(audio):
|
| 201 |
+
# audio: dict or tuple depending on Gradio; normalize to (sr, np.ndarray)
|
| 202 |
+
if isinstance(audio, dict):
|
| 203 |
+
sr, wave = audio["sample_rate"], np.array(audio["data"], dtype=np.float32)
|
| 204 |
+
else:
|
| 205 |
+
sr, wave = audio # already (sr, np.ndarray)
|
| 206 |
+
|
| 207 |
+
audio_tuple = (sr, wave)
|
| 208 |
+
fart_type = detect_fart(audio_tuple)
|
| 209 |
+
idea = transcribe_idea(audio_tuple)
|
| 210 |
+
art_id = _hash_bytes(wave.tobytes())[:8]
|
| 211 |
+
art_path = generate_art(idea, art_id)
|
| 212 |
+
review = review_text(idea, fart_type)
|
| 213 |
+
minted = laugh_to_mint(audio_tuple)
|
| 214 |
+
capsule = create_capsule(audio_tuple, idea, art_path, review, fart_type if minted else "unverified")
|
| 215 |
+
|
| 216 |
+
# Persist capsule only on Mint click; here we just stage it.
|
| 217 |
+
state_capsule.value = capsule
|
| 218 |
+
|
| 219 |
+
# Return UI-friendly payloads
|
| 220 |
+
fart_audio_value = (sr, wave) # gr.Audio expects (sr, np.ndarray)
|
| 221 |
+
art_img = Image.open(art_path)
|
| 222 |
+
review_audio_none = None # No TTS (keyless)
|
| 223 |
+
return fart_audio_value, idea, art_img, review, review_audio_none, json.dumps(capsule, indent=2)
|
| 224 |
+
|
| 225 |
+
def mint_handler(staged_json):
|
| 226 |
+
# `staged_json` is the JSON textbox value from last process; prevent mint without stage
|
| 227 |
+
try:
|
| 228 |
+
capsule = json.loads(staged_json)
|
| 229 |
+
except Exception:
|
| 230 |
+
return None, "Mint failed: no staged capsule."
|
| 231 |
+
ds = add_to_ledger(capsule)
|
| 232 |
+
# Build small table for UI
|
| 233 |
+
tail = ds.to_pandas().tail(10)[["id", "idea", "fart_type", "token_value"]]
|
| 234 |
+
return tail, f"Minted {capsule['id']}"
|
| 235 |
+
|
| 236 |
+
with gr.Blocks(title="FARTORY™ v1.0", css=THEME_CSS, theme=gr.themes.Default()) as ui:
|
| 237 |
+
gr.Markdown("## FART-AS-INFRASTRUCTURE™ • orange/black")
|
| 238 |
with gr.Row():
|
| 239 |
+
record_btn = gr.Button("💨 PROCESS MIC INPUT", variant="primary")
|
| 240 |
+
mint_btn = gr.Button("🪙 MINT FARTWORK", variant="secondary")
|
| 241 |
+
|
| 242 |
with gr.Tab("Fartifact™ Capsule"):
|
| 243 |
+
output_audio = gr.Audio(label="Fart Audio", interactive=False, type="numpy")
|
|
|
|
| 244 |
output_idea = gr.Textbox(label="Idea Capsule")
|
| 245 |
+
output_art = gr.Image(label="Art", type="pil")
|
| 246 |
+
output_review = gr.Textbox(label="VC Verdict")
|
| 247 |
+
tts_output = gr.Audio(label="Review Audio (off)", interactive=False, type="numpy")
|
| 248 |
+
staged_capsule = gr.Textbox(label="Staged Capsule (JSON)", interactive=False)
|
| 249 |
+
|
| 250 |
+
with gr.Tab("Fart Ledger"):
|
| 251 |
+
dataset_view = gr.Dataframe(headers=["id", "idea", "fart_type", "token_value"])
|
| 252 |
+
mint_status = gr.Markdown("")
|
| 253 |
+
|
| 254 |
+
mic = gr.Audio(sources=["microphone"], label="Microphone", type="numpy")
|
| 255 |
+
|
| 256 |
record_btn.click(
|
| 257 |
+
fn=process_handler,
|
| 258 |
+
inputs=[mic],
|
| 259 |
+
outputs=[output_audio, output_idea, output_art, output_review, tts_output, staged_capsule]
|
| 260 |
)
|
| 261 |
+
|
|
|
|
| 262 |
mint_btn.click(
|
| 263 |
+
fn=mint_handler,
|
| 264 |
+
inputs=[staged_capsule],
|
| 265 |
+
outputs=[dataset_view, mint_status]
|
| 266 |
)
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
if __name__ == "__main__":
|
| 269 |
+
ui.launch(server_port=7860, share=False)
|
|
|