Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,10 +16,13 @@ print("Initializing Whisper model...")
|
|
| 16 |
model = whisper.load_model("base")
|
| 17 |
print("System Ready.")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# 2. Main Processing Logic
|
| 20 |
-
def process_voice_conversation(audio_path,
|
| 21 |
if not audio_path:
|
| 22 |
-
return
|
| 23 |
|
| 24 |
try:
|
| 25 |
# Step A: Speech-to-Text
|
|
@@ -27,37 +30,43 @@ def process_voice_conversation(audio_path, history):
|
|
| 27 |
user_text = transcription["text"].strip()
|
| 28 |
|
| 29 |
if not user_text:
|
| 30 |
-
return
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
# Step B: LLM Processing via Groq
|
| 35 |
-
messages = [
|
| 36 |
-
{"role": "system", "content": "You are a professional, intelligent AI assistant demonstrating a low-latency voice architecture. Provide concise, highly accurate, and polite responses."}
|
| 37 |
-
]
|
| 38 |
-
messages.extend(history)
|
| 39 |
-
|
| 40 |
chat_completion = client.chat.completions.create(
|
| 41 |
-
messages=
|
| 42 |
model="llama-3.3-70b-versatile",
|
| 43 |
)
|
| 44 |
ai_text = chat_completion.choices[0].message.content
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Step C: Text-to-Speech
|
| 48 |
tts = gTTS(text=ai_text, lang='en', slow=False)
|
| 49 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 50 |
tts.save(temp_audio.name)
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
error_msg = f"System Error: {str(e)}"
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# 3. Professional UI Design
|
| 60 |
-
# Using a clean, monochrome theme favored in academic/enterprise tools
|
| 61 |
custom_theme = gr.themes.Monochrome(
|
| 62 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 63 |
primary_hue="slate",
|
|
@@ -66,6 +75,9 @@ custom_theme = gr.themes.Monochrome(
|
|
| 66 |
|
| 67 |
with gr.Blocks(title="VocaFree AI - Research Prototype", theme=custom_theme) as demo:
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
# Header
|
| 70 |
gr.Markdown(
|
| 71 |
"""
|
|
@@ -75,16 +87,15 @@ with gr.Blocks(title="VocaFree AI - Research Prototype", theme=custom_theme) as
|
|
| 75 |
"""
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# Use Tabs to separate the Demo from the Documentation
|
| 79 |
with gr.Tabs():
|
| 80 |
|
| 81 |
# TAB 1: The Live App
|
| 82 |
with gr.Tab("ποΈ Live Interaction"):
|
| 83 |
with gr.Row():
|
| 84 |
with gr.Column(scale=2):
|
|
|
|
| 85 |
chatbot = gr.Chatbot(
|
| 86 |
label="Conversation Transcript",
|
| 87 |
-
type="messages",
|
| 88 |
height=450,
|
| 89 |
avatar_images=(None, "βοΈ") # Professional gear icon for the AI
|
| 90 |
)
|
|
@@ -105,7 +116,7 @@ with gr.Blocks(title="VocaFree AI - Research Prototype", theme=custom_theme) as
|
|
| 105 |
autoplay=True,
|
| 106 |
interactive=False
|
| 107 |
)
|
| 108 |
-
clear_btn = gr.
|
| 109 |
|
| 110 |
# TAB 2: Architecture & Documentation
|
| 111 |
with gr.Tab("π System Architecture"):
|
|
@@ -134,11 +145,18 @@ with gr.Blocks(title="VocaFree AI - Research Prototype", theme=custom_theme) as
|
|
| 134 |
"""
|
| 135 |
)
|
| 136 |
|
| 137 |
-
# Event Wiring
|
| 138 |
submit_btn.click(
|
| 139 |
fn=process_voice_conversation,
|
| 140 |
-
inputs=[audio_input, chatbot],
|
| 141 |
-
outputs=[chatbot, audio_output, audio_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|
|
|
|
| 16 |
model = whisper.load_model("base")
|
| 17 |
print("System Ready.")
|
| 18 |
|
| 19 |
+
# The core instructions for the AI
|
| 20 |
+
SYSTEM_PROMPT = {"role": "system", "content": "You are a professional, intelligent AI assistant demonstrating a low-latency voice architecture. Provide concise, highly accurate, and polite responses."}
|
| 21 |
+
|
| 22 |
# 2. Main Processing Logic
|
| 23 |
+
def process_voice_conversation(audio_path, chat_history, llm_state):
|
| 24 |
if not audio_path:
|
| 25 |
+
return chat_history, llm_state, None, None
|
| 26 |
|
| 27 |
try:
|
| 28 |
# Step A: Speech-to-Text
|
|
|
|
| 30 |
user_text = transcription["text"].strip()
|
| 31 |
|
| 32 |
if not user_text:
|
| 33 |
+
return chat_history, llm_state, None, None
|
| 34 |
|
| 35 |
+
# Add to AI's internal memory
|
| 36 |
+
llm_state.append({"role": "user", "content": user_text})
|
| 37 |
|
| 38 |
# Step B: LLM Processing via Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
chat_completion = client.chat.completions.create(
|
| 40 |
+
messages=llm_state,
|
| 41 |
model="llama-3.3-70b-versatile",
|
| 42 |
)
|
| 43 |
ai_text = chat_completion.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
# Add response to AI's internal memory
|
| 46 |
+
llm_state.append({"role": "assistant", "content": ai_text})
|
| 47 |
+
|
| 48 |
+
# Add the conversation pair to the UI Chatbot
|
| 49 |
+
chat_history.append((user_text, ai_text))
|
| 50 |
|
| 51 |
# Step C: Text-to-Speech
|
| 52 |
tts = gTTS(text=ai_text, lang='en', slow=False)
|
| 53 |
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 54 |
tts.save(temp_audio.name)
|
| 55 |
|
| 56 |
+
# Return UI history, Memory state, Output Audio, and clear Input Audio
|
| 57 |
+
return chat_history, llm_state, temp_audio.name, None
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
error_msg = f"System Error: {str(e)}"
|
| 61 |
+
# Display the error in the chat interface safely
|
| 62 |
+
chat_history.append(("Audio processed...", error_msg))
|
| 63 |
+
return chat_history, llm_state, None, None
|
| 64 |
+
|
| 65 |
+
# Function to completely wipe the session memory and UI
|
| 66 |
+
def reset_conversation():
|
| 67 |
+
return [], [SYSTEM_PROMPT], None, None
|
| 68 |
|
| 69 |
# 3. Professional UI Design
|
|
|
|
| 70 |
custom_theme = gr.themes.Monochrome(
|
| 71 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 72 |
primary_hue="slate",
|
|
|
|
| 75 |
|
| 76 |
with gr.Blocks(title="VocaFree AI - Research Prototype", theme=custom_theme) as demo:
|
| 77 |
|
| 78 |
+
# Hidden state variable to hold the LLM's memory securely
|
| 79 |
+
llm_state = gr.State([SYSTEM_PROMPT])
|
| 80 |
+
|
| 81 |
# Header
|
| 82 |
gr.Markdown(
|
| 83 |
"""
|
|
|
|
| 87 |
"""
|
| 88 |
)
|
| 89 |
|
|
|
|
| 90 |
with gr.Tabs():
|
| 91 |
|
| 92 |
# TAB 1: The Live App
|
| 93 |
with gr.Tab("ποΈ Live Interaction"):
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column(scale=2):
|
| 96 |
+
# Removed the problematic 'type="messages"' argument
|
| 97 |
chatbot = gr.Chatbot(
|
| 98 |
label="Conversation Transcript",
|
|
|
|
| 99 |
height=450,
|
| 100 |
avatar_images=(None, "βοΈ") # Professional gear icon for the AI
|
| 101 |
)
|
|
|
|
| 116 |
autoplay=True,
|
| 117 |
interactive=False
|
| 118 |
)
|
| 119 |
+
clear_btn = gr.Button("ποΈ Reset Session")
|
| 120 |
|
| 121 |
# TAB 2: Architecture & Documentation
|
| 122 |
with gr.Tab("π System Architecture"):
|
|
|
|
| 145 |
"""
|
| 146 |
)
|
| 147 |
|
| 148 |
+
# Event Wiring: Submit Audio
|
| 149 |
submit_btn.click(
|
| 150 |
fn=process_voice_conversation,
|
| 151 |
+
inputs=[audio_input, chatbot, llm_state],
|
| 152 |
+
outputs=[chatbot, llm_state, audio_output, audio_input]
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Event Wiring: Clear Session (Wipes UI and AI Memory)
|
| 156 |
+
clear_btn.click(
|
| 157 |
+
fn=reset_conversation,
|
| 158 |
+
inputs=[],
|
| 159 |
+
outputs=[chatbot, llm_state, audio_input, audio_output]
|
| 160 |
)
|
| 161 |
|
| 162 |
if __name__ == "__main__":
|