Spaces:
Running on T4
Running on T4
remove sentence bank and pre-written option
Browse files- app.py +5 -4
- src/generate.py +21 -111
app.py
CHANGED
|
@@ -195,10 +195,11 @@ with gr.Blocks(title="Voice Consent Gate") as demo:
|
|
| 195 |
"""
|
| 196 |
)
|
| 197 |
with gr.Column():
|
| 198 |
-
consent_method = gr.Dropdown(
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
| 202 |
asr_model = gr.Dropdown(label="Speech recognition model",
|
| 203 |
choices=["openai/whisper-tiny.en", # fastest (CPU-friendly)
|
| 204 |
"openai/whisper-base.en", # better accuracy, a bit slower
|
|
|
|
| 195 |
"""
|
| 196 |
)
|
| 197 |
with gr.Column():
|
| 198 |
+
consent_method = gr.Dropdown(
|
| 199 |
+
label="Sentence generation method",
|
| 200 |
+
choices=["Llama 3.2 3B Instruct"],
|
| 201 |
+
value="Llama 3.2 3B Instruct"
|
| 202 |
+
)
|
| 203 |
asr_model = gr.Dropdown(label="Speech recognition model",
|
| 204 |
choices=["openai/whisper-tiny.en", # fastest (CPU-friendly)
|
| 205 |
"openai/whisper-base.en", # better accuracy, a bit slower
|
src/generate.py
CHANGED
|
@@ -9,16 +9,10 @@ Hugging Face Space for Llama 3.2 3B Instruct) to generate natural-sounding
|
|
| 9 |
sentences that users can read aloud to give informed consent for voice cloning.
|
| 10 |
|
| 11 |
If the model call fails (e.g., due to rate limits or network issues),
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Functions:
|
| 15 |
-
- _extract_llama_text(): Normalize the API output from the Llama demo.
|
| 16 |
-
- gen_sentence_llm(): Generate a consent sentence from the Llama model Space.
|
| 17 |
-
- gen_sentence_set(): Select a random prewritten sentence (for fallback/testing).
|
| 18 |
"""
|
| 19 |
|
| 20 |
import os
|
| 21 |
-
import random
|
| 22 |
from typing import Any
|
| 23 |
from gradio_client import Client
|
| 24 |
|
|
@@ -26,24 +20,7 @@ import src.process as process
|
|
| 26 |
from src.prompts import get_consent_generation_prompt
|
| 27 |
|
| 28 |
|
| 29 |
-
# ------------------- Sentence Bank (unchanged) -------------------
|
| 30 |
-
SENTENCE_BANK = [
|
| 31 |
-
"The quick brown fox jumps over the lazy dog.",
|
| 32 |
-
"I promise to speak clearly and at a steady pace.",
|
| 33 |
-
"Open source makes AI more transparent and inclusive.",
|
| 34 |
-
"Hugging Face Spaces make demos easy to share.",
|
| 35 |
-
"Today the weather in Berlin is pleasantly cool.",
|
| 36 |
-
"Privacy and transparency should go hand in hand.",
|
| 37 |
-
"Please generate a new sentence for me to read.",
|
| 38 |
-
"Machine learning can amplify or reduce inequality.",
|
| 39 |
-
"Responsible AI requires participation from everyone.",
|
| 40 |
-
"This microphone test checks my pronunciation accuracy.",
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
|
| 44 |
# ------------------- Model / Space Configuration -------------------
|
| 45 |
-
# The demo connects to the Llama 3.2 3B Instruct Space on Hugging Face.
|
| 46 |
-
# You can override these defaults by setting environment variables in your Space.
|
| 47 |
LLAMA_SPACE_ID = os.getenv(
|
| 48 |
"LLAMA_SPACE_ID", "huggingface-projects/llama-3.2-3B-Instruct"
|
| 49 |
)
|
|
@@ -54,29 +31,12 @@ HF_TOKEN = os.getenv("HF_TOKEN") # Optional; not required for public Spaces.
|
|
| 54 |
def _extract_llama_text(result: Any) -> str:
|
| 55 |
"""
|
| 56 |
Normalize the API response from the Llama 3.2 3B demo Space into plain text.
|
| 57 |
-
|
| 58 |
-
The Space’s `/chat` endpoint may return different shapes depending on how
|
| 59 |
-
the Gradio app is structured — sometimes a string, other times a dictionary
|
| 60 |
-
or list. This function recursively traverses and extracts the first
|
| 61 |
-
meaningful text string it finds.
|
| 62 |
-
|
| 63 |
-
Parameters
|
| 64 |
-
----------
|
| 65 |
-
result : Any
|
| 66 |
-
The raw output returned by `client.predict()`.
|
| 67 |
-
|
| 68 |
-
Returns
|
| 69 |
-
-------
|
| 70 |
-
str
|
| 71 |
-
Cleaned text output (may be empty string if extraction fails).
|
| 72 |
"""
|
| 73 |
if isinstance(result, str):
|
| 74 |
return result.strip()
|
| 75 |
if isinstance(result, (int, float, bool)):
|
| 76 |
return str(result)
|
| 77 |
if isinstance(result, list):
|
| 78 |
-
# If multiple segments are returned (e.g., multiple sentences),
|
| 79 |
-
# join them into one string.
|
| 80 |
parts = []
|
| 81 |
for x in result:
|
| 82 |
s = _extract_llama_text(x)
|
|
@@ -84,7 +44,6 @@ def _extract_llama_text(result: Any) -> str:
|
|
| 84 |
parts.append(s)
|
| 85 |
return " ".join(parts).strip()
|
| 86 |
if isinstance(result, dict):
|
| 87 |
-
# Common key names used in Gradio JSON responses
|
| 88 |
for key in ("text", "response", "content", "generated_text", "message"):
|
| 89 |
v = result.get(key)
|
| 90 |
if isinstance(v, str) and v.strip():
|
|
@@ -92,60 +51,33 @@ def _extract_llama_text(result: Any) -> str:
|
|
| 92 |
return ""
|
| 93 |
|
| 94 |
|
| 95 |
-
def gen_sentence(
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
| 111 |
) -> str:
|
| 112 |
"""
|
| 113 |
Generate a consent sentence using the Llama 3.2 3B Instruct demo Space.
|
| 114 |
|
| 115 |
-
|
| 116 |
-
requirements for a consent sentence (via `get_consent_generation_prompt`)
|
| 117 |
-
and sends it to the Llama demo hosted on Hugging Face Spaces.
|
| 118 |
-
|
| 119 |
-
The response is normalized into a single English sentence suitable
|
| 120 |
-
for reading aloud.
|
| 121 |
-
|
| 122 |
-
Parameters
|
| 123 |
-
----------
|
| 124 |
-
audio_model_name : str, optional
|
| 125 |
-
The name of the voice-cloning model to mention in the sentence.
|
| 126 |
-
Defaults to "Chatterbox".
|
| 127 |
-
fallback_on_error : bool, optional
|
| 128 |
-
If True, return a random fallback sentence instead of raising
|
| 129 |
-
an error when the Space call fails. Default is False for debugging.
|
| 130 |
-
|
| 131 |
-
Returns
|
| 132 |
-
-------
|
| 133 |
-
str
|
| 134 |
-
A clean, human-readable consent sentence.
|
| 135 |
-
|
| 136 |
-
Raises
|
| 137 |
-
------
|
| 138 |
-
Exception
|
| 139 |
-
Re-raises the underlying error if `fallback_on_error` is False.
|
| 140 |
"""
|
| 141 |
-
# Generate the full natural-language prompt that the LLM will receive
|
| 142 |
prompt = get_consent_generation_prompt(audio_model_name)
|
| 143 |
|
| 144 |
try:
|
| 145 |
-
# Initialize Gradio client for the Llama demo Space
|
| 146 |
client = Client(LLAMA_SPACE_ID, hf_token=HF_TOKEN)
|
| 147 |
-
|
| 148 |
-
# The Llama demo exposes a simple /chat endpoint with standard decoding params
|
| 149 |
result = client.predict(
|
| 150 |
message=prompt,
|
| 151 |
max_new_tokens=128,
|
|
@@ -156,38 +88,16 @@ def gen_sentence_llm(sentence_method="Llama 3.2 3B Instruct", audio_model_name:
|
|
| 156 |
api_name=LLAMA_API_NAME,
|
| 157 |
)
|
| 158 |
|
| 159 |
-
# Normalize and clean up model output
|
| 160 |
text = _extract_llama_text(result)
|
| 161 |
text = process.normalize_text(text, lower=False)
|
| 162 |
|
| 163 |
-
# Handle empty or malformed outputs
|
| 164 |
if not text:
|
| 165 |
raise ValueError("Empty response from Llama Space")
|
| 166 |
|
| 167 |
-
# In case the model produces multiple lines or options, pick the first full sentence
|
| 168 |
first_line = next((ln.strip() for ln in text.splitlines() if ln.strip()), "")
|
| 169 |
return first_line or text
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
print(f"[gen_sentence_llm] Llama Space call failed: {type(e).__name__}: {e}")
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
return random.choice(SENTENCE_BANK)
|
| 176 |
-
# Otherwise propagate the exception so the UI displays it
|
| 177 |
-
raise
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
def gen_sentence_set() -> str:
|
| 181 |
-
"""
|
| 182 |
-
Return a sentence from a predefined static list.
|
| 183 |
-
|
| 184 |
-
This is used as a simple fallback generator when model-based
|
| 185 |
-
generation is unavailable or for testing the ASR pipeline
|
| 186 |
-
without network access.
|
| 187 |
-
|
| 188 |
-
Returns
|
| 189 |
-
-------
|
| 190 |
-
str
|
| 191 |
-
A single English sentence from the fallback bank.
|
| 192 |
-
"""
|
| 193 |
-
return random.choice(SENTENCE_BANK)
|
|
|
|
| 9 |
sentences that users can read aloud to give informed consent for voice cloning.
|
| 10 |
|
| 11 |
If the model call fails (e.g., due to rate limits or network issues),
|
| 12 |
+
an error is surfaced to the UI (no local fallback).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import os
|
|
|
|
| 16 |
from typing import Any
|
| 17 |
from gradio_client import Client
|
| 18 |
|
|
|
|
| 20 |
from src.prompts import get_consent_generation_prompt
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ------------------- Model / Space Configuration -------------------
|
|
|
|
|
|
|
| 24 |
LLAMA_SPACE_ID = os.getenv(
|
| 25 |
"LLAMA_SPACE_ID", "huggingface-projects/llama-3.2-3B-Instruct"
|
| 26 |
)
|
|
|
|
| 31 |
def _extract_llama_text(result: Any) -> str:
|
| 32 |
"""
|
| 33 |
Normalize the API response from the Llama 3.2 3B demo Space into plain text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
if isinstance(result, str):
|
| 36 |
return result.strip()
|
| 37 |
if isinstance(result, (int, float, bool)):
|
| 38 |
return str(result)
|
| 39 |
if isinstance(result, list):
|
|
|
|
|
|
|
| 40 |
parts = []
|
| 41 |
for x in result:
|
| 42 |
s = _extract_llama_text(x)
|
|
|
|
| 44 |
parts.append(s)
|
| 45 |
return " ".join(parts).strip()
|
| 46 |
if isinstance(result, dict):
|
|
|
|
| 47 |
for key in ("text", "response", "content", "generated_text", "message"):
|
| 48 |
v = result.get(key)
|
| 49 |
if isinstance(v, str) and v.strip():
|
|
|
|
| 51 |
return ""
|
| 52 |
|
| 53 |
|
| 54 |
+
def gen_sentence(_ignored_method="Llama 3.2 3B Instruct", audio_model_name="Chatterbox"):
|
| 55 |
+
"""
|
| 56 |
+
Always generate a sentence via the LLM. UI may still pass a 'method' arg,
|
| 57 |
+
but it's ignored to keep the callback signature stable.
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
return gen_sentence_llm(audio_model_name=audio_model_name, fallback_on_error=False)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
# Show a helpful message directly in the Target sentence box
|
| 63 |
+
return f"[ERROR calling LLM] {type(e).__name__}: {e}"
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def gen_sentence_llm(
|
| 67 |
+
sentence_method: str = "Llama 3.2 3B Instruct",
|
| 68 |
+
audio_model_name: str = "Chatterbox",
|
| 69 |
+
*,
|
| 70 |
+
fallback_on_error: bool = False # kept for signature parity; does nothing now
|
| 71 |
) -> str:
|
| 72 |
"""
|
| 73 |
Generate a consent sentence using the Llama 3.2 3B Instruct demo Space.
|
| 74 |
|
| 75 |
+
Returns a single English sentence suitable for reading aloud.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
|
|
|
| 77 |
prompt = get_consent_generation_prompt(audio_model_name)
|
| 78 |
|
| 79 |
try:
|
|
|
|
| 80 |
client = Client(LLAMA_SPACE_ID, hf_token=HF_TOKEN)
|
|
|
|
|
|
|
| 81 |
result = client.predict(
|
| 82 |
message=prompt,
|
| 83 |
max_new_tokens=128,
|
|
|
|
| 88 |
api_name=LLAMA_API_NAME,
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
text = _extract_llama_text(result)
|
| 92 |
text = process.normalize_text(text, lower=False)
|
| 93 |
|
|
|
|
| 94 |
if not text:
|
| 95 |
raise ValueError("Empty response from Llama Space")
|
| 96 |
|
|
|
|
| 97 |
first_line = next((ln.strip() for ln in text.splitlines() if ln.strip()), "")
|
| 98 |
return first_line or text
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
print(f"[gen_sentence_llm] Llama Space call failed: {type(e).__name__}: {e}")
|
| 102 |
+
# No local fallback anymore; surface the error to the UI.
|
| 103 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|