deneme / analysis_ui.py
candeliat's picture
Update DeepInsight ZeroGPU analysis compatibility check
0ba6be7 verified
Raw History Blame Contribute Delete
8.43 kB
"""ZeroGPU smoke check; package only with scripts/package_hf_space.py."""
import os
import tempfile
import shutil
import subprocess
import sys
import json
from pathlib import Path
# Must precede torch / whisper imports for ZeroGPU's CUDA lifecycle.
import spaces
os.environ.setdefault("TF_USE_LEGACY_KERAS", "1")
os.environ.setdefault("TF_CPP_MIN_LOG_LEVEL", "2")
os.environ.setdefault("GRADIO_TEMP_DIR", "/tmp/deepinsight-gradio")
os.environ.setdefault("WHISPER_MODEL", "small")
import gradio as gr
username = os.environ.get("DEMO_USERNAME", "")
password = os.environ.get("DEMO_PASSWORD", "")
if not username or len(password) < 16:
raise RuntimeError("Add DEMO_USERNAME and DEMO_PASSWORD (16+ characters) in Space Settings > Secrets.")
import logging
import time
from app.services.analytics import AudioAnalyzer, _convert_to_native
from app.services.whisper_service import get_whisper_model
from probe import MAX_BYTES, prepare_media, validate_upload
from zerogpu_runtime import move_whisper_to_cuda
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("deepinsight.hf")
# Download/prepare models at startup; ZeroGPU emulates PyTorch CUDA placement
# outside decorated calls. Do not lazily move models to CUDA during inference.
whisper_model = move_whisper_to_cuda(get_whisper_model(device="cpu"))
WORK_ROOT = Path(tempfile.gettempdir()) / "deepinsight-hf-jobs"
WORK_ROOT.mkdir(exist_ok=True)
def cleanup_state(state):
if not state or not state.get("directory"):
return
directory = Path(state["directory"]).resolve()
if directory.parent == WORK_ROOT.resolve():
shutil.rmtree(directory, ignore_errors=True)
def prepare_sample(file_path, language, consent, previous_state):
cleanup_state(previous_state)
try:
source = validate_upload(file_path, consent, language)
except ValueError as error:
raise gr.Error(str(error)) from None
started = time.monotonic()
stage = "media_decode"
directory = tempfile.mkdtemp(prefix="sample-", dir=WORK_ROOT)
try:
media, audio, duration, has_video = prepare_media(source, directory)
# TensorFlow and librosa may initialize native thread pools. Keep them
# out of the parent process that ZeroGPU snapshots/forks for PyTorch.
stage = "cpu_analysis"
output = Path(directory) / "cpu-result.json"
command = [sys.executable, str(Path(__file__).with_name("probe.py")),
"--audio", str(audio), "--output", str(output)]
if has_video:
command.extend(["--video", str(media)])
completed = subprocess.run(command, capture_output=True, text=True, timeout=180)
if completed.returncode:
if output.is_file():
failure = json.loads(output.read_text(encoding="utf-8"))
raise ValueError(f"CPU analysis failed ({failure.get('stage')}: {failure.get('error_type')}): {failure.get('message')}")
raise ValueError("CPU analysis process stopped without a result; check Space logs.")
cpu_result = json.loads(output.read_text(encoding="utf-8"))
return {"directory": directory, "audio": str(audio), "language": language,
"duration": duration, "video_result": cpu_result["video_result"],
"pauses": cpu_result["pauses"], "started": started}
except ValueError as error:
cleanup_state({"directory": directory})
raise gr.Error(str(error)) from None
except Exception as error:
cleanup_state({"directory": directory})
logger.exception("Model compatibility check failed")
raise gr.Error(f"Analiz tamamlanamadı ({stage}: {type(error).__name__}). Space loglarını ve ZeroGPU kotasını kontrol edin.") from None
@spaces.GPU(duration=15)
def transcribe_prepared(state):
# This decorated function is a Gradio event itself, so ZeroGPU sees the
# short allocation instead of reserving the default duration for a wrapper.
if not state:
raise gr.Error("Önce örnek kaydı hazırlayın.")
try:
# Record the device inside the allocated GPU worker. Reading the parent
# model after ZeroGPU releases the lease can misleadingly report CPU.
inference_device = str(get_whisper_model().device)
transcript = AudioAnalyzer.transcribe(state["audio"], state["language"])
if not transcript["text"]:
raise gr.Error("Konuşma çözümlenemedi. Sesli bir örnek deneyin.")
return {**state, "transcript": transcript["text"], "whisper_device": inference_device}
except Exception as error:
cleanup_state(state)
logger.exception("Whisper GPU check failed")
raise gr.Error(f"Whisper tamamlanamadı ({type(error).__name__}).") from None
@spaces.GPU(duration=5)
def check_gpu():
import torch
value = torch.ones(4, device="cuda").sum().item()
return {"cuda_available": torch.cuda.is_available(), "compute_check": value}
def finish_sample(state):
if not state or not state.get("transcript"):
raise gr.Error("Transkripsiyon tamamlanmadı.")
try:
result = {
"transcript": state["transcript"], "language": state["language"],
"recording_seconds": state["duration"],
"speech_rate_wpm": AudioAnalyzer.calculate_speech_rate(state["transcript"], state["duration"]),
"filler_words": AudioAnalyzer.count_filler_words(state["transcript"], state["language"]),
"pauses": state["pauses"], "video_analysis": state["video_result"],
"elapsed_seconds": round(time.monotonic() - state["started"], 2),
"runtime": {"whisper": state["whisper_device"], "tensorflow": "CPU", "mediapipe": "CPU"},
}
return _convert_to_native(result), None
finally:
cleanup_state(state)
with gr.Blocks(delete_cache=(300, 600)) as demo:
gr.Markdown("# DeepInsight analiz denemesi\nKendi 5–10 saniyelik örnek kaydınızı kullanın. Sonuçlar veritabanına kaydedilmez.")
sample = gr.File(label="Ses veya video (en fazla 20 saniye / 25 MiB)", type="filepath",
file_types=[".mp4", ".webm", ".mov", ".wav", ".mp3", ".m4a"])
language = gr.Radio([("Türkçe", "tr"), ("English", "en")], value="tr", label="Konuşma dili")
consent = gr.Checkbox(label="Kendi örnek kaydımın analiz edilmesini onaylıyorum.")
button = gr.Button("Analizi çalıştır")
result = gr.JSON(label="Gerçek analiz sonucu")
# Paths stay in server-owned session state, not in client-provided JSON.
state = gr.State(value=None, time_to_live=600, delete_callback=cleanup_state)
preparation = button.click(prepare_sample, inputs=[sample, language, consent, state], outputs=state,
api_name="prepare_sample", concurrency_id="analysis")
inference = preparation.success(transcribe_prepared, inputs=state, outputs=state,
api_name="transcribe_prepared", concurrency_id="analysis")
inference.success(finish_sample, inputs=state, outputs=[result, state], api_name="finish_sample",
concurrency_id="analysis")
gpu_check = gr.Button("GPU bağlantısını kontrol et")
gpu_check.click(check_gpu, outputs=result, api_name="check_gpu", concurrency_id="analysis")
if __name__ == "__main__" and Path(__file__).with_name("serve.py").is_file():
# Keep @spaces.GPU declarations in the Space entrypoint for startup detection.
# spaces 0.51.3 normally packs model tensors and reports readiness from
# Blocks.launch(). An ASGI mount uses uvicorn instead, so run that same
# startup hook explicitly before serving requests.
from spaces import zero
if hasattr(zero, "startup"):
zero.startup()
from serve import mount_api
import uvicorn
app = mount_api(demo, username, password, MAX_BYTES)
server = uvicorn.Server(uvicorn.Config(app, host="0.0.0.0", port=int(os.getenv("PORT", "7860")), workers=1))
# ZeroGPU uses this port to close inherited listening sockets in GPU forks.
demo.server = server
server.run()
elif __name__ == "__main__":
demo.queue(max_size=2, default_concurrency_limit=1).launch(
server_name="0.0.0.0", server_port=7860, share=False,
auth=(username, password), max_file_size=MAX_BYTES, show_error=False,
blocked_paths=[str(Path(__file__).resolve().parent / "app")],
)