Spaces:
Paused
Paused
Commit ·
e99dd36
1
Parent(s): 8ca9f59
feat: wire Whisper-small ASR and Qwen2.5-3B Q&A into app (sprint steps 9-10)
Browse files- Add inference.py: on-demand Whisper-small transcription + Qwen2.5-3B-Instruct
grounded story Q&A with context retrieval
- Update app.py handle_question_submit to use real ASR + Q&A + TTS pipeline
- Add test_modules/test_whisper_qwen.py for model verification
- Add .github/copilot-instructions.md for Copilot context
- Update sprint.md: mark steps 9 and 10 complete
- Add sample_sounds/ to .gitignore (generated at runtime)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- .github/copilot-instructions.md +48 -0
- .gitignore +1 -0
- app.py +50 -6
- inference.py +155 -0
- sprint.md +2 -2
- test_modules/test_whisper_qwen.py +111 -0
.github/copilot-instructions.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copilot Instructions
|
| 2 |
+
|
| 3 |
+
## Running the App
|
| 4 |
+
|
| 5 |
+
```bash
|
| 6 |
+
pip install -r requirements.txt
|
| 7 |
+
python app.py
|
| 8 |
+
# → http://localhost:7860
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
Requires a GPU (T4 or A10G) for inference. No Docker, no database, no external APIs.
|
| 12 |
+
|
| 13 |
+
## Architecture
|
| 14 |
+
|
| 15 |
+
Single-process Gradio app (`app.py`, ~1200 lines) that orchestrates three ML models:
|
| 16 |
+
|
| 17 |
+
- **QWEN-TTS-0.6B** — voice cloning + TTS (always loaded)
|
| 18 |
+
- **Qwen2.5-3B-Instruct** — story Q&A, 4-bit quantized on T4 (always loaded)
|
| 19 |
+
- **Whisper-small** — child speech-to-text (loaded on demand)
|
| 20 |
+
|
| 21 |
+
`tts.py` wraps Supertonic TTS with sentence-level chunking and background-threaded streaming via a queue (maxsize=2 buffer).
|
| 22 |
+
|
| 23 |
+
Stories are plain `.txt` files in `stories/` — title on line 1, blank line, then prose. No metadata DB.
|
| 24 |
+
|
| 25 |
+
**State machine** governs playback: `playing → paused → playing`, `playing → asking → answering → resuming → playing`. All other transitions are illegal. The UI disables buttons for illegal transitions.
|
| 26 |
+
|
| 27 |
+
## Key Conventions
|
| 28 |
+
|
| 29 |
+
- **All inference is local** — no external APIs, no data leaves the server. This is a hard privacy requirement.
|
| 30 |
+
- **In-memory session cache only** — no database, no persistent storage of user data.
|
| 31 |
+
- **Interruptible chunked streaming** — paragraphs are synthesized and played one at a time. Cached chunks enable instant replay/resume.
|
| 32 |
+
- **Pre-generated Q&A** — anticipated questions generated in background during narration for sub-1s cache hits.
|
| 33 |
+
- **VRAM budget awareness** — total ~5-6 GB on T4 (16 GB). Load ASR only on demand. Use 4-bit quantization for the LLM.
|
| 34 |
+
|
| 35 |
+
## Story Pipeline
|
| 36 |
+
|
| 37 |
+
`story_downloader/` contains utilities for acquiring new stories from Project Gutenberg:
|
| 38 |
+
1. `gutenberg_downloader.py` — reusable downloader/parser
|
| 39 |
+
2. `download_stories.py` — fetches stories by Gutenberg ID
|
| 40 |
+
3. `clean_stories.py` — strips headers/footers/illustration tags for TTS-clean output
|
| 41 |
+
|
| 42 |
+
## UI
|
| 43 |
+
|
| 44 |
+
Gradio 5.x with custom CSS (`static/style.css`) for a Google Stitch-inspired design. Uses warm palette (#FFB347 accent, #FFF8E7 background), Nunito/Fredoka fonts, rounded cards, and micro-animations.
|
| 45 |
+
|
| 46 |
+
## Deployment
|
| 47 |
+
|
| 48 |
+
Push to Hugging Face Spaces. The `README.md` frontmatter configures the Space (sdk: gradio, app_file: app.py).
|
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
__pycache__/
|
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
+
sample_sounds/
|
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import time
|
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
from tts import split_into_chunks, generate_audio_stream
|
|
|
|
| 10 |
|
| 11 |
# Create directories for sample audio files
|
| 12 |
os.makedirs("sample_sounds", exist_ok=True)
|
|
@@ -869,34 +870,77 @@ with gr.Blocks(css=css_code, title="VoiceBook Gradio Hub") as demo:
|
|
| 869 |
)
|
| 870 |
|
| 871 |
# 9. Submit question
|
| 872 |
-
def handle_question_submit(question_txt, question_audio_path):
|
| 873 |
if not question_txt.strip() and question_audio_path is None:
|
| 874 |
answer_html = """
|
| 875 |
<div style="padding: 10px; background: #fef2f2; border: 1px solid #fecaca; border-radius: 10px; font-size: 12px; color: #991b1b;">
|
| 876 |
-
|
| 877 |
</div>
|
| 878 |
"""
|
| 879 |
return answer_html, gr.Audio(visible=False)
|
| 880 |
|
| 881 |
-
|
| 882 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 883 |
|
| 884 |
answer_html = f"""
|
| 885 |
<div style="margin-top: 12px; padding: 16px; background: rgba(240,253,244,0.1); border: 1px solid rgba(187,247,208,0.3); border-radius: 14px;">
|
| 886 |
<div style="font-size: 10px; font-weight: 700; text-transform: uppercase; color: #4ade80; margin-bottom: 6px;">Answer in Narrator's Voice</div>
|
| 887 |
<div style="font-family: 'Playfair Display', Georgia, serif; font-style: italic; color: #FAF7F2; font-size: 13px; line-height: 1.6;">
|
| 888 |
-
“{
|
| 889 |
</div>
|
| 890 |
<div style="margin-top: 8px; font-size: 10px; color: #94a3b8;">
|
| 891 |
Q: <em>{q_text}</em>
|
| 892 |
</div>
|
| 893 |
</div>
|
| 894 |
"""
|
|
|
|
|
|
|
| 895 |
return answer_html, gr.Audio(value="sample_sounds/cloned_preview.wav", visible=True)
|
| 896 |
|
| 897 |
submit_question_btn.click(
|
| 898 |
handle_question_submit,
|
| 899 |
-
inputs=[question_text, question_audio],
|
| 900 |
outputs=[answer_display, answer_audio]
|
| 901 |
)
|
| 902 |
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
from tts import split_into_chunks, generate_audio_stream
|
| 10 |
+
from inference import transcribe_audio, answer_story_question
|
| 11 |
|
| 12 |
# Create directories for sample audio files
|
| 13 |
os.makedirs("sample_sounds", exist_ok=True)
|
|
|
|
| 870 |
)
|
| 871 |
|
| 872 |
# 9. Submit question
|
| 873 |
+
def handle_question_submit(question_txt, question_audio_path, paragraphs, slider_val):
|
| 874 |
if not question_txt.strip() and question_audio_path is None:
|
| 875 |
answer_html = """
|
| 876 |
<div style="padding: 10px; background: #fef2f2; border: 1px solid #fecaca; border-radius: 10px; font-size: 12px; color: #991b1b;">
|
| 877 |
+
Please type a question or record one with the microphone.
|
| 878 |
</div>
|
| 879 |
"""
|
| 880 |
return answer_html, gr.Audio(visible=False)
|
| 881 |
|
| 882 |
+
# Step 9: Transcribe audio if text is empty (on-demand ASR)
|
| 883 |
+
if question_txt.strip():
|
| 884 |
+
q_text = question_txt.strip()
|
| 885 |
+
else:
|
| 886 |
+
try:
|
| 887 |
+
q_text = transcribe_audio(question_audio_path)
|
| 888 |
+
if not q_text:
|
| 889 |
+
q_text = "(could not understand audio)"
|
| 890 |
+
except Exception as e:
|
| 891 |
+
q_text = f"(transcription failed: {e})"
|
| 892 |
+
|
| 893 |
+
# Step 10: Generate grounded answer from story context using Qwen
|
| 894 |
+
current_idx = int(slider_val) if slider_val else 0
|
| 895 |
+
try:
|
| 896 |
+
answer_text = answer_story_question(q_text, paragraphs, current_idx)
|
| 897 |
+
if not answer_text:
|
| 898 |
+
answer_text = "Hmm, I'm not sure about that! Let's keep listening to find out."
|
| 899 |
+
except Exception as e:
|
| 900 |
+
answer_text = "Oops, I couldn't think of an answer right now. Let's keep reading!"
|
| 901 |
+
import logging
|
| 902 |
+
logging.getLogger(__name__).exception("Q&A failed: %s", e)
|
| 903 |
+
|
| 904 |
+
# Synthesize answer in cloned voice via TTS
|
| 905 |
+
answer_audio_path = None
|
| 906 |
+
try:
|
| 907 |
+
from tts import split_into_chunks as _split, generate_audio_stream as _gen_stream
|
| 908 |
+
import soundfile as sf
|
| 909 |
+
import numpy as np
|
| 910 |
+
|
| 911 |
+
chunks = _split(answer_text)
|
| 912 |
+
audio_segments = []
|
| 913 |
+
sample_rate = 16000
|
| 914 |
+
for sr, wav, idx, total, err in _gen_stream(chunks):
|
| 915 |
+
if wav is not None:
|
| 916 |
+
audio_segments.append(wav)
|
| 917 |
+
sample_rate = sr
|
| 918 |
+
if audio_segments:
|
| 919 |
+
full_audio = np.concatenate(audio_segments)
|
| 920 |
+
answer_audio_path = "sample_sounds/qa_answer.wav"
|
| 921 |
+
sf.write(answer_audio_path, full_audio, sample_rate)
|
| 922 |
+
except Exception:
|
| 923 |
+
# Fall back to no audio if TTS fails
|
| 924 |
+
pass
|
| 925 |
|
| 926 |
answer_html = f"""
|
| 927 |
<div style="margin-top: 12px; padding: 16px; background: rgba(240,253,244,0.1); border: 1px solid rgba(187,247,208,0.3); border-radius: 14px;">
|
| 928 |
<div style="font-size: 10px; font-weight: 700; text-transform: uppercase; color: #4ade80; margin-bottom: 6px;">Answer in Narrator's Voice</div>
|
| 929 |
<div style="font-family: 'Playfair Display', Georgia, serif; font-style: italic; color: #FAF7F2; font-size: 13px; line-height: 1.6;">
|
| 930 |
+
“{answer_text}”
|
| 931 |
</div>
|
| 932 |
<div style="margin-top: 8px; font-size: 10px; color: #94a3b8;">
|
| 933 |
Q: <em>{q_text}</em>
|
| 934 |
</div>
|
| 935 |
</div>
|
| 936 |
"""
|
| 937 |
+
if answer_audio_path:
|
| 938 |
+
return answer_html, gr.Audio(value=answer_audio_path, visible=True)
|
| 939 |
return answer_html, gr.Audio(value="sample_sounds/cloned_preview.wav", visible=True)
|
| 940 |
|
| 941 |
submit_question_btn.click(
|
| 942 |
handle_question_submit,
|
| 943 |
+
inputs=[question_text, question_audio, paragraphs_state, timeline_slider],
|
| 944 |
outputs=[answer_display, answer_audio]
|
| 945 |
)
|
| 946 |
|
inference.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Inference module for ASR (Whisper-small) and story Q&A (Qwen2.5-3B-Instruct).
|
| 3 |
+
Models are loaded on-demand and cached globally for reuse.
|
| 4 |
+
"""
|
| 5 |
+
import logging
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
# ---------------------------------------------------------------------------
|
| 11 |
+
# ASR — Whisper-small (loaded on demand)
|
| 12 |
+
# ---------------------------------------------------------------------------
|
| 13 |
+
|
| 14 |
+
_asr_pipe = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_asr_pipeline():
|
| 18 |
+
"""Load Whisper-small pipeline on first call, cache thereafter."""
|
| 19 |
+
global _asr_pipe
|
| 20 |
+
if _asr_pipe is None:
|
| 21 |
+
from transformers import pipeline
|
| 22 |
+
|
| 23 |
+
logger.info("Loading Whisper-small for ASR...")
|
| 24 |
+
_asr_pipe = pipeline(
|
| 25 |
+
"automatic-speech-recognition",
|
| 26 |
+
model="openai/whisper-small",
|
| 27 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 28 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 29 |
+
)
|
| 30 |
+
logger.info("Whisper-small loaded.")
|
| 31 |
+
return _asr_pipe
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def transcribe_audio(audio_path: str) -> str:
|
| 35 |
+
"""Transcribe an audio file to text using Whisper-small."""
|
| 36 |
+
if not audio_path:
|
| 37 |
+
return ""
|
| 38 |
+
pipe = get_asr_pipeline()
|
| 39 |
+
result = pipe(audio_path, generate_kwargs={"language": "en"})
|
| 40 |
+
return result.get("text", "").strip()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ---------------------------------------------------------------------------
|
| 44 |
+
# Q&A — Qwen2.5-3B-Instruct (always loaded after first call)
|
| 45 |
+
# ---------------------------------------------------------------------------
|
| 46 |
+
|
| 47 |
+
_qa_tokenizer = None
|
| 48 |
+
_qa_model = None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def get_qa_model():
|
| 52 |
+
"""Load Qwen2.5-3B-Instruct on first call, cache thereafter."""
|
| 53 |
+
global _qa_tokenizer, _qa_model
|
| 54 |
+
if _qa_model is None:
|
| 55 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 56 |
+
|
| 57 |
+
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
| 58 |
+
logger.info("Loading %s...", model_id)
|
| 59 |
+
|
| 60 |
+
_qa_tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 61 |
+
|
| 62 |
+
load_kwargs = {"device_map": "auto"}
|
| 63 |
+
if torch.cuda.is_available():
|
| 64 |
+
load_kwargs["torch_dtype"] = torch.float16
|
| 65 |
+
# Use 4-bit on Linux (HF Spaces) if bitsandbytes available
|
| 66 |
+
try:
|
| 67 |
+
from transformers import BitsAndBytesConfig
|
| 68 |
+
load_kwargs["quantization_config"] = BitsAndBytesConfig(
|
| 69 |
+
load_in_4bit=True,
|
| 70 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 71 |
+
bnb_4bit_quant_type="nf4",
|
| 72 |
+
)
|
| 73 |
+
logger.info("Using 4-bit quantization.")
|
| 74 |
+
except Exception:
|
| 75 |
+
logger.info("bitsandbytes unavailable, using float16.")
|
| 76 |
+
else:
|
| 77 |
+
load_kwargs["torch_dtype"] = torch.float32
|
| 78 |
+
load_kwargs["device_map"] = "cpu"
|
| 79 |
+
|
| 80 |
+
_qa_model = AutoModelForCausalLM.from_pretrained(model_id, **load_kwargs)
|
| 81 |
+
logger.info("Qwen2.5-3B-Instruct loaded.")
|
| 82 |
+
return _qa_tokenizer, _qa_model
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def _get_relevant_context(paragraphs: list[str], current_idx: int, question: str) -> str:
|
| 86 |
+
"""Simple TF-IDF-like retrieval: return the most relevant paragraphs as context."""
|
| 87 |
+
if not paragraphs:
|
| 88 |
+
return ""
|
| 89 |
+
|
| 90 |
+
# Use paragraphs around the current position + simple keyword overlap
|
| 91 |
+
question_words = set(question.lower().split())
|
| 92 |
+
|
| 93 |
+
scored = []
|
| 94 |
+
for i, para in enumerate(paragraphs):
|
| 95 |
+
para_words = set(para.lower().split())
|
| 96 |
+
overlap = len(question_words & para_words)
|
| 97 |
+
# Boost paragraphs near current position
|
| 98 |
+
proximity_bonus = max(0, 3 - abs(i - current_idx))
|
| 99 |
+
scored.append((overlap + proximity_bonus, i, para))
|
| 100 |
+
|
| 101 |
+
scored.sort(key=lambda x: x[0], reverse=True)
|
| 102 |
+
# Take top 3 most relevant paragraphs
|
| 103 |
+
top_paras = [s[2] for s in scored[:3]]
|
| 104 |
+
return "\n\n".join(top_paras)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def answer_story_question(
|
| 108 |
+
question: str,
|
| 109 |
+
paragraphs: list[str],
|
| 110 |
+
current_idx: int = 0,
|
| 111 |
+
) -> str:
|
| 112 |
+
"""
|
| 113 |
+
Generate a short, grounded answer to a child's question about the story.
|
| 114 |
+
Returns the answer text (1-2 sentences).
|
| 115 |
+
"""
|
| 116 |
+
if not question.strip():
|
| 117 |
+
return ""
|
| 118 |
+
|
| 119 |
+
tokenizer, model = get_qa_model()
|
| 120 |
+
|
| 121 |
+
context = _get_relevant_context(paragraphs, current_idx, question)
|
| 122 |
+
if not context:
|
| 123 |
+
context = "\n\n".join(paragraphs[:5])
|
| 124 |
+
|
| 125 |
+
messages = [
|
| 126 |
+
{
|
| 127 |
+
"role": "system",
|
| 128 |
+
"content": (
|
| 129 |
+
"You are a friendly storyteller answering a child's question about a bedtime story. "
|
| 130 |
+
"Answer in 1-2 short, simple sentences using ONLY information from the story context below. "
|
| 131 |
+
"If the story doesn't contain the answer, say so gently. "
|
| 132 |
+
"Use warm, age-appropriate language."
|
| 133 |
+
),
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"role": "user",
|
| 137 |
+
"content": f"Story context:\n{context}\n\nChild's question: {question}",
|
| 138 |
+
},
|
| 139 |
+
]
|
| 140 |
+
|
| 141 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 142 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 143 |
+
|
| 144 |
+
with torch.no_grad():
|
| 145 |
+
outputs = model.generate(
|
| 146 |
+
**inputs,
|
| 147 |
+
max_new_tokens=80,
|
| 148 |
+
temperature=0.7,
|
| 149 |
+
do_sample=True,
|
| 150 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
answer_tokens = outputs[0][inputs["input_ids"].shape[1]:]
|
| 154 |
+
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True).strip()
|
| 155 |
+
return answer
|
sprint.md
CHANGED
|
@@ -26,8 +26,8 @@ Ship a public Hugging Face Space: parent clones voice → story streams in that
|
|
| 26 |
| 6 | Build Gradio app with 3 tabs (Clone, Listen, Ask) | 1h | ☑ | *(4 tabs: Explore, Library+Player, Clone Voice Studio, Profile & Sandbox)* |
|
| 27 |
| 7 | Tab 1 (Clone): `gr.Audio` record/upload + preview button | 45m | ☑ | *(Clone Voice Studio tab: mic recorder, extract button, status pipeline, preview audio)* |
|
| 28 |
| 8 | Tab 2 (Listen): story dropdown + play/pause/resume controls for streamed chunks | 45m | ☑ | *(Library tab: book card grid + integrated player panel with play/pause/resume/chunk status)* |
|
| 29 |
-
| 9 | Add on-demand ASR for child voice input; use lighter ASR fallback if needed | 30m |
|
| 30 |
-
| 10 | Tab 3 (Ask): interrupt narration → short grounded Qwen answer → TTS → resume story | 2h |
|
| 31 |
| 10a | Pre-generate 2–3 anticipated Q&A pairs per chunk during narration playback (background task) | 30m | ☐ |
|
| 32 |
|
| 33 |
**Checkpoint:** Full loop works locally — clone → listen → interrupt → ask → resume. Ugly but functional.
|
|
|
|
| 26 |
| 6 | Build Gradio app with 3 tabs (Clone, Listen, Ask) | 1h | ☑ | *(4 tabs: Explore, Library+Player, Clone Voice Studio, Profile & Sandbox)* |
|
| 27 |
| 7 | Tab 1 (Clone): `gr.Audio` record/upload + preview button | 45m | ☑ | *(Clone Voice Studio tab: mic recorder, extract button, status pipeline, preview audio)* |
|
| 28 |
| 8 | Tab 2 (Listen): story dropdown + play/pause/resume controls for streamed chunks | 45m | ☑ | *(Library tab: book card grid + integrated player panel with play/pause/resume/chunk status)* |
|
| 29 |
+
| 9 | Add on-demand ASR for child voice input; use lighter ASR fallback if needed | 30m | ☑ | *(Whisper-small loaded on-demand in `inference.py`; transcribes child audio in `handle_question_submit`)* |
|
| 30 |
+
| 10 | Tab 3 (Ask): interrupt narration → short grounded Qwen answer → TTS → resume story | 2h | ☑ | *(Qwen2.5-3B-Instruct in `inference.py` generates grounded 1-2 sentence answers from story context; answer synthesized via Supertonic TTS; full pipeline wired in `handle_question_submit`)* |
|
| 31 |
| 10a | Pre-generate 2–3 anticipated Q&A pairs per chunk during narration playback (background task) | 30m | ☐ |
|
| 32 |
|
| 33 |
**Checkpoint:** Full loop works locally — clone → listen → interrupt → ask → resume. Ugly but functional.
|
test_modules/test_whisper_qwen.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test script: Download and verify Whisper-small (ASR) and Qwen2.5-3B-Instruct (Q&A).
|
| 3 |
+
Run this to confirm models load correctly before wiring into app.py.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
python test_modules/test_whisper_qwen.py
|
| 7 |
+
"""
|
| 8 |
+
import sys
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
print("=" * 60)
|
| 12 |
+
print("Step 1: Testing Whisper-small (ASR)")
|
| 13 |
+
print("=" * 60)
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
import torch
|
| 17 |
+
from transformers import pipeline
|
| 18 |
+
|
| 19 |
+
start = time.time()
|
| 20 |
+
print("Loading whisper-small pipeline...")
|
| 21 |
+
asr_pipe = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model="openai/whisper-small",
|
| 24 |
+
device="cuda" if torch.cuda.is_available() else "cpu",
|
| 25 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 26 |
+
)
|
| 27 |
+
elapsed = time.time() - start
|
| 28 |
+
print(f"[OK] Whisper-small loaded in {elapsed:.1f}s")
|
| 29 |
+
print(f" Device: {'cuda' if torch.cuda.is_available() else 'cpu'}")
|
| 30 |
+
|
| 31 |
+
# Test with a short synthetic audio array
|
| 32 |
+
import numpy as np
|
| 33 |
+
dummy_audio = np.zeros(16000, dtype=np.float32) # 1 second of silence
|
| 34 |
+
result = asr_pipe({"raw": dummy_audio, "sampling_rate": 16000})
|
| 35 |
+
print(f"[OK] Whisper inference test passed (result: '{result['text'].strip()}')")
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"[FAIL] Whisper-small failed: {e}")
|
| 39 |
+
sys.exit(1)
|
| 40 |
+
|
| 41 |
+
print()
|
| 42 |
+
print("=" * 60)
|
| 43 |
+
print("Step 2: Testing Qwen2.5-3B-Instruct (Q&A)")
|
| 44 |
+
print("=" * 60)
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 48 |
+
|
| 49 |
+
start = time.time()
|
| 50 |
+
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
| 51 |
+
print(f"Loading {model_id}...")
|
| 52 |
+
|
| 53 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 54 |
+
|
| 55 |
+
# Use float16 on GPU, float32 on CPU. 4-bit quantization used on HF Spaces (Linux).
|
| 56 |
+
if torch.cuda.is_available():
|
| 57 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
model_id,
|
| 59 |
+
torch_dtype=torch.float16,
|
| 60 |
+
device_map="auto",
|
| 61 |
+
)
|
| 62 |
+
else:
|
| 63 |
+
print(" (No GPU -- loading in float32 on CPU, will be slow)")
|
| 64 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 65 |
+
model_id,
|
| 66 |
+
torch_dtype=torch.float32,
|
| 67 |
+
device_map="cpu",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
elapsed = time.time() - start
|
| 71 |
+
print(f"[OK] Qwen2.5-3B-Instruct loaded in {elapsed:.1f}s")
|
| 72 |
+
|
| 73 |
+
# Test inference with a story Q&A prompt
|
| 74 |
+
story_context = "Peter Rabbit squeezed under the gate into Mr. McGregor's garden. He ate some lettuces and French beans."
|
| 75 |
+
question = "What did Peter Rabbit eat?"
|
| 76 |
+
|
| 77 |
+
messages = [
|
| 78 |
+
{"role": "system", "content": "You are a friendly storyteller answering a child's question about a story. Answer in 1-2 short sentences using only information from the story context provided."},
|
| 79 |
+
{"role": "user", "content": f"Story context: {story_context}\n\nChild's question: {question}"}
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 83 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 84 |
+
|
| 85 |
+
start = time.time()
|
| 86 |
+
with torch.no_grad():
|
| 87 |
+
outputs = model.generate(
|
| 88 |
+
**inputs,
|
| 89 |
+
max_new_tokens=80,
|
| 90 |
+
temperature=0.7,
|
| 91 |
+
do_sample=True,
|
| 92 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 93 |
+
)
|
| 94 |
+
answer_tokens = outputs[0][inputs["input_ids"].shape[1]:]
|
| 95 |
+
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
|
| 96 |
+
elapsed = time.time() - start
|
| 97 |
+
|
| 98 |
+
print(f"[OK] Qwen inference test passed in {elapsed:.1f}s")
|
| 99 |
+
print(f" Q: {question}")
|
| 100 |
+
print(f" A: {answer}")
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"[FAIL] Qwen2.5-3B-Instruct failed: {e}")
|
| 104 |
+
import traceback
|
| 105 |
+
traceback.print_exc()
|
| 106 |
+
sys.exit(1)
|
| 107 |
+
|
| 108 |
+
print()
|
| 109 |
+
print("=" * 60)
|
| 110 |
+
print("[OK] ALL MODELS VERIFIED -- ready for integration")
|
| 111 |
+
print("=" * 60)
|