Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import requests
|
|
| 4 |
import json
|
| 5 |
import speech_recognition as sr
|
| 6 |
from tempfile import NamedTemporaryFile
|
| 7 |
-
import pyttsx3
|
| 8 |
import logging
|
| 9 |
import time
|
| 10 |
from huggingface_hub import HfApi
|
|
@@ -15,7 +14,6 @@ logger = logging.getLogger(__name__)
|
|
| 15 |
|
| 16 |
# Environment Variables
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 18 |
-
#HF_REPO_ID = os.environ.get("HF_REPO_ID") # e.g., username/dataset
|
| 19 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 20 |
GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768")
|
| 21 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
@@ -25,9 +23,6 @@ headers = {
|
|
| 25 |
"Content-Type": "application/json"
|
| 26 |
}
|
| 27 |
|
| 28 |
-
# Hugging Face API Client
|
| 29 |
-
hf_api = HfApi()
|
| 30 |
-
|
| 31 |
# Emotion descriptions
|
| 32 |
emotion_options = {
|
| 33 |
"neutral": "Neutral or balanced mood",
|
|
@@ -44,6 +39,7 @@ emotion_options = {
|
|
| 44 |
conversation_history = []
|
| 45 |
|
| 46 |
# Transcribe audio
|
|
|
|
| 47 |
def transcribe_audio(audio_path):
|
| 48 |
recognizer = sr.Recognizer()
|
| 49 |
try:
|
|
@@ -56,6 +52,7 @@ def transcribe_audio(audio_path):
|
|
| 56 |
return ""
|
| 57 |
|
| 58 |
# Generate Groq response
|
|
|
|
| 59 |
def get_groq_response(prompt, history):
|
| 60 |
messages = [{"role": "system", "content": prompt}]
|
| 61 |
for msg in history:
|
|
@@ -76,31 +73,32 @@ def get_groq_response(prompt, history):
|
|
| 76 |
logger.error(f"Groq API error: {e}")
|
| 77 |
return "Error contacting AI."
|
| 78 |
|
| 79 |
-
# Generate
|
|
|
|
| 80 |
def generate_speech_and_upload(text):
|
| 81 |
try:
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
hf_path = f"audio_responses/{os.path.basename(audio_path)}"
|
| 90 |
-
hf_api.upload_file(
|
| 91 |
-
path_or_fileobj=audio_path,
|
| 92 |
-
path_in_repo=hf_path,
|
| 93 |
-
repo_id=HF_REPO_ID,
|
| 94 |
-
repo_type="dataset",
|
| 95 |
-
token=HF_TOKEN
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
return audio_path
|
| 99 |
except Exception as e:
|
| 100 |
-
logger.error(f"
|
| 101 |
return None
|
| 102 |
|
| 103 |
# Main handler
|
|
|
|
| 104 |
def chat_with_ai(audio, text_input, emotion, history):
|
| 105 |
global conversation_history
|
| 106 |
user_text = text_input or ""
|
|
@@ -123,6 +121,7 @@ def chat_with_ai(audio, text_input, emotion, history):
|
|
| 123 |
audio_path = generate_speech_and_upload(ai_response)
|
| 124 |
return ai_response, audio_path, history + [[user_text, ai_response]]
|
| 125 |
|
|
|
|
| 126 |
def clear_conversation():
|
| 127 |
global conversation_history
|
| 128 |
conversation_history = []
|
|
@@ -134,13 +133,15 @@ iface = gr.Blocks()
|
|
| 134 |
with iface:
|
| 135 |
gr.Markdown("# Mind AID AI Assistant")
|
| 136 |
gr.Markdown("Talk or type to the AI assistant. Your emotional state helps tailor the response.")
|
| 137 |
-
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column(scale=3):
|
| 140 |
emotion = gr.Dropdown(label="Your emotion?", choices=list(emotion_options.keys()), value="neutral")
|
| 141 |
emotion_description = gr.Markdown("**Current mood:** Neutral")
|
|
|
|
| 142 |
def update_emotion_desc(em):
|
| 143 |
return f"**Current mood:** {emotion_options.get(em, 'Unknown')}"
|
|
|
|
| 144 |
emotion.change(fn=update_emotion_desc, inputs=[emotion], outputs=[emotion_description])
|
| 145 |
with gr.Column(scale=1):
|
| 146 |
clear_btn = gr.Button("Clear Conversation")
|
|
@@ -173,4 +174,8 @@ with iface:
|
|
| 173 |
outputs=[chat_history, audio_input, text_input, status]
|
| 174 |
)
|
| 175 |
|
| 176 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
import speech_recognition as sr
|
| 6 |
from tempfile import NamedTemporaryFile
|
|
|
|
| 7 |
import logging
|
| 8 |
import time
|
| 9 |
from huggingface_hub import HfApi
|
|
|
|
| 14 |
|
| 15 |
# Environment Variables
|
| 16 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 18 |
GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768")
|
| 19 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Emotion descriptions
|
| 27 |
emotion_options = {
|
| 28 |
"neutral": "Neutral or balanced mood",
|
|
|
|
| 39 |
conversation_history = []
|
| 40 |
|
| 41 |
# Transcribe audio
|
| 42 |
+
|
| 43 |
def transcribe_audio(audio_path):
|
| 44 |
recognizer = sr.Recognizer()
|
| 45 |
try:
|
|
|
|
| 52 |
return ""
|
| 53 |
|
| 54 |
# Generate Groq response
|
| 55 |
+
|
| 56 |
def get_groq_response(prompt, history):
|
| 57 |
messages = [{"role": "system", "content": prompt}]
|
| 58 |
for msg in history:
|
|
|
|
| 73 |
logger.error(f"Groq API error: {e}")
|
| 74 |
return "Error contacting AI."
|
| 75 |
|
| 76 |
+
# Generate TTS using Yarngpt
|
| 77 |
+
|
| 78 |
def generate_speech_and_upload(text):
|
| 79 |
try:
|
| 80 |
+
hf_model_id = "saheedniyi/Yarngpt"
|
| 81 |
+
inference_url = f"https://api-inference.huggingface.co/models/{hf_model_id}"
|
| 82 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 83 |
+
payload = {"inputs": text}
|
| 84 |
+
|
| 85 |
+
response = requests.post(inference_url, headers=headers, json=payload)
|
| 86 |
+
if response.status_code != 200:
|
| 87 |
+
logger.error(f"Hugging Face TTS API error: {response.text}")
|
| 88 |
+
return None
|
| 89 |
+
|
| 90 |
temp_file = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 91 |
+
with open(temp_file.name, "wb") as f:
|
| 92 |
+
f.write(response.content)
|
| 93 |
+
|
| 94 |
+
return temp_file.name
|
| 95 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
+
logger.error(f"Hugging Face TTS error: {e}")
|
| 98 |
return None
|
| 99 |
|
| 100 |
# Main handler
|
| 101 |
+
|
| 102 |
def chat_with_ai(audio, text_input, emotion, history):
|
| 103 |
global conversation_history
|
| 104 |
user_text = text_input or ""
|
|
|
|
| 121 |
audio_path = generate_speech_and_upload(ai_response)
|
| 122 |
return ai_response, audio_path, history + [[user_text, ai_response]]
|
| 123 |
|
| 124 |
+
|
| 125 |
def clear_conversation():
|
| 126 |
global conversation_history
|
| 127 |
conversation_history = []
|
|
|
|
| 133 |
with iface:
|
| 134 |
gr.Markdown("# Mind AID AI Assistant")
|
| 135 |
gr.Markdown("Talk or type to the AI assistant. Your emotional state helps tailor the response.")
|
| 136 |
+
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column(scale=3):
|
| 139 |
emotion = gr.Dropdown(label="Your emotion?", choices=list(emotion_options.keys()), value="neutral")
|
| 140 |
emotion_description = gr.Markdown("**Current mood:** Neutral")
|
| 141 |
+
|
| 142 |
def update_emotion_desc(em):
|
| 143 |
return f"**Current mood:** {emotion_options.get(em, 'Unknown')}"
|
| 144 |
+
|
| 145 |
emotion.change(fn=update_emotion_desc, inputs=[emotion], outputs=[emotion_description])
|
| 146 |
with gr.Column(scale=1):
|
| 147 |
clear_btn = gr.Button("Clear Conversation")
|
|
|
|
| 174 |
outputs=[chat_history, audio_input, text_input, status]
|
| 175 |
)
|
| 176 |
|
| 177 |
+
iface.launch()
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
Here is the complete revised code with Yarngpt integrated for text-to-speech output via Hugging Face. Make sure your HF_TOKEN is correctly set in your environment and has access to the model saheedniyi/Yarngpt. Let me know if you need help deploying this.
|
| 181 |
+
|