Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,34 +28,43 @@ DEFAULT_SYSTEM_PROMPT = (
|
|
| 28 |
"Then generate %^%^%^"
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Advanced Voice Matrix routing for Edge-TTS
|
| 32 |
VOICE_MATRIX = {
|
| 33 |
"English": {"Male": "en-US-ChristopherNeural", "Female": "en-US-AvaNeural", "gtts": "en"},
|
| 34 |
"German": {"Male": "de-DE-ChristophNeural", "Female": "de-DE-KatjaNeural", "gtts": "de"}
|
| 35 |
}
|
| 36 |
|
| 37 |
# ==============================================================================
|
| 38 |
-
# TTS AUDIO GENERATOR
|
| 39 |
# ==============================================================================
|
| 40 |
def generate_tts_audio(text, lang="English", gender="Male", engine="edge-tts"):
|
|
|
|
|
|
|
|
|
|
| 41 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 42 |
temp_path = temp_file.name
|
| 43 |
temp_file.close()
|
| 44 |
|
| 45 |
try:
|
|
|
|
| 46 |
if engine == "edge-tts":
|
| 47 |
-
voice = VOICE_MATRIX
|
| 48 |
result = subprocess.run([
|
| 49 |
"edge-tts",
|
| 50 |
"--voice", voice,
|
| 51 |
"--text", text,
|
| 52 |
"--write-media", temp_path
|
| 53 |
], capture_output=True, text=True)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
tts.save(temp_path)
|
| 60 |
|
| 61 |
if os.path.exists(temp_path) and os.path.getsize(temp_path) > 0:
|
|
@@ -69,12 +78,12 @@ def generate_tts_audio(text, lang="English", gender="Male", engine="edge-tts"):
|
|
| 69 |
# STREAMING ENGINE
|
| 70 |
# ==============================================================================
|
| 71 |
def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_engine):
|
| 72 |
-
# Determine input source (Audio takes priority if
|
| 73 |
user_query = text_input
|
| 74 |
if audio_input is not None:
|
| 75 |
yield "ποΈ Transcribing audio...", gr.skip()
|
| 76 |
asr_res = asr_pipe(audio_input, generate_kwargs={"language": lang.lower()})
|
| 77 |
-
user_query = asr_res
|
| 78 |
|
| 79 |
if not user_query or not user_query.strip():
|
| 80 |
yield "Please provide text or audio input.", gr.skip()
|
|
@@ -82,14 +91,28 @@ def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_eng
|
|
| 82 |
|
| 83 |
formatted_chat_prompt = f"System: {system_prompt}\nUser: {user_query}\nAssistant:"
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
process = subprocess.Popen(
|
| 95 |
cmd,
|
|
@@ -100,9 +123,8 @@ def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_eng
|
|
| 100 |
)
|
| 101 |
|
| 102 |
accumulator = ""
|
| 103 |
-
prompt_cleared = False
|
| 104 |
LOOKAHEAD_SIZE = 45
|
| 105 |
-
stop_markers = ["%^%^%^","%%%","[end of text]", "User:", "Assistant:"]
|
| 106 |
|
| 107 |
while True:
|
| 108 |
char = process.stdout.read(1)
|
|
@@ -111,10 +133,14 @@ def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_eng
|
|
| 111 |
|
| 112 |
accumulator += char
|
| 113 |
|
|
|
|
| 114 |
if not prompt_cleared:
|
| 115 |
if "Assistant:" in accumulator:
|
| 116 |
prompt_cleared = True
|
| 117 |
accumulator = accumulator.split("Assistant:")[-1].lstrip()
|
|
|
|
|
|
|
|
|
|
| 118 |
continue
|
| 119 |
|
| 120 |
stop_triggered = False
|
|
@@ -132,13 +158,17 @@ def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_eng
|
|
| 132 |
safe_to_display = accumulator[:len(accumulator) - LOOKAHEAD_SIZE]
|
| 133 |
yield safe_to_display.strip(), gr.skip()
|
| 134 |
|
|
|
|
| 135 |
final_text = accumulator.strip()
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if final_text:
|
| 138 |
audio_path = generate_tts_audio(final_text, lang, gender, engine=tts_engine)
|
| 139 |
yield final_text, audio_path
|
| 140 |
else:
|
| 141 |
-
yield
|
| 142 |
|
| 143 |
# ==============================================================================
|
| 144 |
# TECHNICAL REPORT MARKDOWN TEXT
|
|
@@ -212,7 +242,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 212 |
### Welcome to the Future of Learning! π
|
| 213 |
This interactive demo uses **Natural Language Processing (NLP)** to help you learn by *thinking* rather than just giving you the answers.
|
| 214 |
|
| 215 |
-
Instead of acting like a standard search engine, this AI
|
| 216 |
|
| 217 |
**π¬ The Science Behind the Scenes:** Normally, AI models require massive, power-hungry supercomputers. But this demo runs on a revolutionary **"1-Bit" Architecture**. By compressing complex AI math down into simple ternary numbers (-1, 0, and 1), this highly intelligent system can run lightning-fast on a standard computer processor!
|
| 218 |
|
|
|
|
| 28 |
"Then generate %^%^%^"
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Advanced Voice Matrix routing for Edge-TTS and gTTS
|
| 32 |
VOICE_MATRIX = {
|
| 33 |
"English": {"Male": "en-US-ChristopherNeural", "Female": "en-US-AvaNeural", "gtts": "en"},
|
| 34 |
"German": {"Male": "de-DE-ChristophNeural", "Female": "de-DE-KatjaNeural", "gtts": "de"}
|
| 35 |
}
|
| 36 |
|
| 37 |
# ==============================================================================
|
| 38 |
+
# TTS AUDIO GENERATOR (WITH AUTOMATIC FALLBACK)
|
| 39 |
# ==============================================================================
|
| 40 |
def generate_tts_audio(text, lang="English", gender="Male", engine="edge-tts"):
|
| 41 |
+
if not text or not text.strip():
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 45 |
temp_path = temp_file.name
|
| 46 |
temp_file.close()
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
success = False
|
| 50 |
if engine == "edge-tts":
|
| 51 |
+
voice = VOICE_MATRIX.get(lang, {}).get(gender, "en-US-ChristopherNeural")
|
| 52 |
result = subprocess.run([
|
| 53 |
"edge-tts",
|
| 54 |
"--voice", voice,
|
| 55 |
"--text", text,
|
| 56 |
"--write-media", temp_path
|
| 57 |
], capture_output=True, text=True)
|
| 58 |
+
|
| 59 |
+
if result.returncode == 0 and os.path.exists(temp_path) and os.path.getsize(temp_path) > 0:
|
| 60 |
+
success = True
|
| 61 |
+
else:
|
| 62 |
+
print(f"[TTS WARNING] edge-tts error: {result.stderr.strip() if result else 'unknown'}. Falling back to gTTS...")
|
| 63 |
+
|
| 64 |
+
# If gTTS is selected or edge-tts failed, fallback gracefully to gTTS
|
| 65 |
+
if not success:
|
| 66 |
+
gtts_lang = VOICE_MATRIX.get(lang, {}).get("gtts", "en")
|
| 67 |
+
tts = gTTS(text=text, lang=gtts_lang, slow=False)
|
| 68 |
tts.save(temp_path)
|
| 69 |
|
| 70 |
if os.path.exists(temp_path) and os.path.getsize(temp_path) > 0:
|
|
|
|
| 78 |
# STREAMING ENGINE
|
| 79 |
# ==============================================================================
|
| 80 |
def streaming_chat(text_input, audio_input, lang, gender, system_prompt, tts_engine):
|
| 81 |
+
# Determine input source (Audio takes priority if provided)
|
| 82 |
user_query = text_input
|
| 83 |
if audio_input is not None:
|
| 84 |
yield "ποΈ Transcribing audio...", gr.skip()
|
| 85 |
asr_res = asr_pipe(audio_input, generate_kwargs={"language": lang.lower()})
|
| 86 |
+
user_query = asr_res.get("text", "")
|
| 87 |
|
| 88 |
if not user_query or not user_query.strip():
|
| 89 |
yield "Please provide text or audio input.", gr.skip()
|
|
|
|
| 91 |
|
| 92 |
formatted_chat_prompt = f"System: {system_prompt}\nUser: {user_query}\nAssistant:"
|
| 93 |
|
| 94 |
+
# Check if compiled binary llama-cli exists directly to bypass Python wrapper overhead
|
| 95 |
+
if os.path.exists("./build/bin/llama-cli"):
|
| 96 |
+
cmd = [
|
| 97 |
+
"./build/bin/llama-cli",
|
| 98 |
+
"-m", MODEL_PATH,
|
| 99 |
+
"-p", formatted_chat_prompt,
|
| 100 |
+
"-n", "120",
|
| 101 |
+
"--temp", "0.4",
|
| 102 |
+
"-t", "2",
|
| 103 |
+
"--no-display-prompt"
|
| 104 |
+
]
|
| 105 |
+
prompt_cleared = True
|
| 106 |
+
else:
|
| 107 |
+
cmd = [
|
| 108 |
+
"python3", "run_inference.py",
|
| 109 |
+
"-m", MODEL_PATH,
|
| 110 |
+
"-p", formatted_chat_prompt,
|
| 111 |
+
"-n", "120",
|
| 112 |
+
"-temp", "0.4",
|
| 113 |
+
"-t", "2"
|
| 114 |
+
]
|
| 115 |
+
prompt_cleared = False
|
| 116 |
|
| 117 |
process = subprocess.Popen(
|
| 118 |
cmd,
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
accumulator = ""
|
|
|
|
| 126 |
LOOKAHEAD_SIZE = 45
|
| 127 |
+
stop_markers = ["%^%^%^", "%%%", "[end of text]", "User:", "Assistant:", "<|end|>", "</s>"]
|
| 128 |
|
| 129 |
while True:
|
| 130 |
char = process.stdout.read(1)
|
|
|
|
| 133 |
|
| 134 |
accumulator += char
|
| 135 |
|
| 136 |
+
# Adaptive clearing of prompt echo (handles both echoed and non-echoed CLI modes)
|
| 137 |
if not prompt_cleared:
|
| 138 |
if "Assistant:" in accumulator:
|
| 139 |
prompt_cleared = True
|
| 140 |
accumulator = accumulator.split("Assistant:")[-1].lstrip()
|
| 141 |
+
elif len(accumulator) > len(formatted_chat_prompt) + 15:
|
| 142 |
+
# If prompt was not echoed by updated llama.cpp, start streaming
|
| 143 |
+
prompt_cleared = True
|
| 144 |
continue
|
| 145 |
|
| 146 |
stop_triggered = False
|
|
|
|
| 158 |
safe_to_display = accumulator[:len(accumulator) - LOOKAHEAD_SIZE]
|
| 159 |
yield safe_to_display.strip(), gr.skip()
|
| 160 |
|
| 161 |
+
# Final cleanup of remaining stop sequences
|
| 162 |
final_text = accumulator.strip()
|
| 163 |
+
for marker in stop_markers:
|
| 164 |
+
if marker in final_text:
|
| 165 |
+
final_text = final_text.split(marker)[0].strip()
|
| 166 |
|
| 167 |
if final_text:
|
| 168 |
audio_path = generate_tts_audio(final_text, lang, gender, engine=tts_engine)
|
| 169 |
yield final_text, audio_path
|
| 170 |
else:
|
| 171 |
+
yield "No response generated. Please check model status.", gr.skip()
|
| 172 |
|
| 173 |
# ==============================================================================
|
| 174 |
# TECHNICAL REPORT MARKDOWN TEXT
|
|
|
|
| 242 |
### Welcome to the Future of Learning! π
|
| 243 |
This interactive demo uses **Natural Language Processing (NLP)** to help you learn by *thinking* rather than just giving you the answers.
|
| 244 |
|
| 245 |
+
Instead of acting like a standard search engine, this AI responds using the ancient **Socratic Method**. When you ask it a question, it won't just hand you a list of facts. Instead, it will analyze your question and reply with deep, thought-provoking questions of its own to guide your brain toward the answer!
|
| 246 |
|
| 247 |
**π¬ The Science Behind the Scenes:** Normally, AI models require massive, power-hungry supercomputers. But this demo runs on a revolutionary **"1-Bit" Architecture**. By compressing complex AI math down into simple ternary numbers (-1, 0, and 1), this highly intelligent system can run lightning-fast on a standard computer processor!
|
| 248 |
|