Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,93 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
-
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
# GGUF inference engine, like llama-cpp-python.
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Try to initialize the Llama model
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
print("Llama model initialized successfully!")
|
| 20 |
except Exception as e:
|
| 21 |
-
print(f"Error initializing
|
| 22 |
-
|
| 23 |
-
print("Please check if the model is compatible with llama-cpp-python.")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
# You can access it in the code like this:
|
| 30 |
-
CHATTERBOX_ENDPOINT = os.environ.get("CHATTERBOX_ENDPOINT", "http://localhost:5000")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
def process_audio_and_generate(audio_file_path):
|
| 34 |
"""
|
| 35 |
-
This function handles the
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
3. Passes the transcribed text to the GGUF model.
|
| 39 |
-
4. Generates a text response.
|
| 40 |
-
|
| 41 |
-
Args:
|
| 42 |
-
audio_file_path (str): The file path of the recorded audio.
|
| 43 |
-
|
| 44 |
-
Returns:
|
| 45 |
-
tuple: A tuple containing the transcription and the Gemma response.
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
if
|
| 71 |
try:
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
completion = llm.create_completion(
|
| 78 |
-
prompt,
|
| 79 |
-
max_tokens=150, # Limits the length of the response
|
| 80 |
-
stop=["### User:"], # Stops generation when it sees the next user turn
|
| 81 |
-
echo=False, # Don't repeat the input prompt in the output
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
response_text = completion['choices'][0]['text']
|
| 85 |
-
|
| 86 |
except Exception as e:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
iface = gr.Interface(
|
| 94 |
-
fn=
|
| 95 |
-
inputs=gr.Audio(sources=["microphone"],
|
| 96 |
outputs=[
|
| 97 |
gr.Textbox(label="Transcription"),
|
| 98 |
-
gr.Textbox(label="
|
| 99 |
],
|
| 100 |
-
title="
|
| 101 |
-
description="Speak into the microphone and get a
|
| 102 |
)
|
| 103 |
|
| 104 |
# Launch the Gradio app
|
| 105 |
if __name__ == "__main__":
|
| 106 |
-
iface.launch()
|
| 107 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from supabase import create_client, Client
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
+
# ======================================================================
|
| 8 |
+
# --- SETUP AND CONFIGURATION ---
|
| 9 |
+
# ======================================================================
|
|
|
|
| 10 |
|
| 11 |
+
# Retrieve Supabase credentials from environment variables (Hugging Face Space secrets)
|
| 12 |
+
# This is a secure way to store your API keys and other sensitive information.
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
+
supabase_url = os.environ.get("SUPABASE_URL")
|
| 15 |
+
supabase_key = os.environ.get("SUPABASE_KEY")
|
| 16 |
+
supabase: Client = create_client(supabase_url, supabase_key)
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
+
print(f"Error initializing Supabase client: {e}")
|
| 19 |
+
supabase = None
|
|
|
|
| 20 |
|
| 21 |
+
# Set the endpoint for your voice generation engine
|
| 22 |
+
# Make sure to replace this with the actual URL from your RunPod instance.
|
| 23 |
+
CHATTTERBOX_ENDPOINT = os.environ.get("CHATTERBOX_ENDPOINT")
|
| 24 |
|
| 25 |
+
# ======================================================================
|
| 26 |
+
# --- CORE LOGIC ---
|
| 27 |
+
# ======================================================================
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
def process_voice_command(audio):
|
|
|
|
| 30 |
"""
|
| 31 |
+
This function handles the core logic of the voice assistant.
|
| 32 |
+
It takes an audio file, transcribes it, gets a response from a language model,
|
| 33 |
+
saves the interaction to Supabase, and generates an audio response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"""
|
| 35 |
+
|
| 36 |
+
# Placeholder for the actual transcription logic
|
| 37 |
+
# You will replace this with a call to your speech-to-text model.
|
| 38 |
+
transcribed_text = "Placeholder: Your transcribed text will appear here."
|
| 39 |
|
| 40 |
+
# Placeholder for the response from the language model
|
| 41 |
+
# You will replace this with a call to your Gemma model.
|
| 42 |
+
model_response = "Placeholder: This is Gemma's generated response."
|
| 43 |
+
|
| 44 |
+
# Save the chat to Supabase
|
| 45 |
+
if supabase:
|
| 46 |
+
try:
|
| 47 |
+
# We are using a dummy table name 'chats', make sure this matches your Supabase table.
|
| 48 |
+
supabase.table("chats").insert({
|
| 49 |
+
"user_input": transcribed_text,
|
| 50 |
+
"model_response": model_response
|
| 51 |
+
}).execute()
|
| 52 |
+
print("Chat successfully saved to Supabase! 💾")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error saving chat to Supabase: {e}")
|
| 55 |
+
else:
|
| 56 |
+
print("Supabase client not initialized. Skipping database save.")
|
| 57 |
|
| 58 |
+
# Placeholder for the voice generation logic using Chatterbox
|
| 59 |
+
# You will replace this with an actual API call to your Chatterbox endpoint.
|
| 60 |
+
if CHATTTERBOX_ENDPOINT:
|
| 61 |
try:
|
| 62 |
+
# Example API call structure
|
| 63 |
+
payload = {"text": model_response}
|
| 64 |
+
headers = {"Content-Type": "application/json"}
|
| 65 |
+
requests.post(CHATTTERBOX_ENDPOINT, data=json.dumps(payload), headers=headers)
|
| 66 |
+
print("Response sent to Chatterbox endpoint! 🎙️")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
+
print(f"Error sending data to Chatterbox: {e}")
|
| 69 |
+
else:
|
| 70 |
+
print("Chatterbox endpoint not set. Skipping voice generation.")
|
| 71 |
+
|
| 72 |
+
# Return the transcription and the response to the Gradio interface.
|
| 73 |
+
return transcribed_text, model_response
|
| 74 |
|
| 75 |
+
# ======================================================================
|
| 76 |
+
# --- GRADIO INTERFACE ---
|
| 77 |
+
# ======================================================================
|
| 78 |
|
| 79 |
+
# Create the Gradio interface
|
| 80 |
iface = gr.Interface(
|
| 81 |
+
fn=process_voice_command,
|
| 82 |
+
inputs=gr.Audio(sources=["microphone"], label="Speak your command here..."),
|
| 83 |
outputs=[
|
| 84 |
gr.Textbox(label="Transcription"),
|
| 85 |
+
gr.Textbox(label="AI's Response")
|
| 86 |
],
|
| 87 |
+
title="My Personal Voice Assistant",
|
| 88 |
+
description="Speak into the microphone and get a response from the AI, with chats saved to your Supabase database."
|
| 89 |
)
|
| 90 |
|
| 91 |
# Launch the Gradio app
|
| 92 |
if __name__ == "__main__":
|
| 93 |
+
iface.launch()
|
|
|