Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,10 @@ import os
|
|
5 |
from huggingface_hub import InferenceClient
|
6 |
import requests
|
7 |
import tempfile
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Get the Hugging Face token from environment variable
|
10 |
hf_token = os.getenv("HF_TOKEN")
|
@@ -36,11 +40,29 @@ async def text_to_speech_stream(text):
|
|
36 |
|
37 |
def whisper_speech_to_text(audio_path):
|
38 |
"""Convert speech to text using Hugging Face Whisper API."""
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
async def chat_with_ai(message):
|
46 |
global chat_history
|
@@ -61,13 +83,16 @@ async def chat_with_ai(message):
|
|
61 |
|
62 |
return response_text, audio_path
|
63 |
except Exception as e:
|
64 |
-
|
65 |
return str(e), None
|
66 |
|
67 |
def transcribe_and_chat(audio):
|
|
|
|
|
|
|
68 |
text = whisper_speech_to_text(audio)
|
69 |
if not text:
|
70 |
-
return "Sorry, I couldn't understand the audio.", None
|
71 |
|
72 |
response, audio_path = asyncio.run(chat_with_ai(text))
|
73 |
return response, audio_path
|
@@ -85,7 +110,11 @@ def create_demo():
|
|
85 |
audio_output = gr.Audio(label="AI Voice Response", autoplay=True)
|
86 |
|
87 |
def process_audio(audio):
|
|
|
|
|
|
|
88 |
response, audio_path = transcribe_and_chat(audio)
|
|
|
89 |
return response, audio_path, None # Return None to clear the audio input
|
90 |
|
91 |
demo.load(None, js="""
|
|
|
5 |
from huggingface_hub import InferenceClient
|
6 |
import requests
|
7 |
import tempfile
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# Set up logging
|
11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
12 |
|
13 |
# Get the Hugging Face token from environment variable
|
14 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
40 |
|
41 |
def whisper_speech_to_text(audio_path):
|
42 |
"""Convert speech to text using Hugging Face Whisper API."""
|
43 |
+
if audio_path is None:
|
44 |
+
logging.error("Error: No audio file provided")
|
45 |
+
return ""
|
46 |
+
|
47 |
+
if not os.path.exists(audio_path):
|
48 |
+
logging.error(f"Error: Audio file not found at {audio_path}")
|
49 |
+
return ""
|
50 |
+
|
51 |
+
try:
|
52 |
+
with open(audio_path, "rb") as audio_file:
|
53 |
+
data = audio_file.read()
|
54 |
+
response = requests.post(WHISPER_API_URL, headers=headers, data=data)
|
55 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
56 |
+
result = response.json()
|
57 |
+
transcribed_text = result.get("text", "")
|
58 |
+
logging.info(f"Transcribed text: {transcribed_text}")
|
59 |
+
return transcribed_text
|
60 |
+
except requests.exceptions.RequestException as e:
|
61 |
+
logging.error(f"Error during API request: {e}")
|
62 |
+
return ""
|
63 |
+
except Exception as e:
|
64 |
+
logging.error(f"Unexpected error in whisper_speech_to_text: {e}")
|
65 |
+
return ""
|
66 |
|
67 |
async def chat_with_ai(message):
|
68 |
global chat_history
|
|
|
83 |
|
84 |
return response_text, audio_path
|
85 |
except Exception as e:
|
86 |
+
logging.error(f"Error in chat_with_ai: {e}")
|
87 |
return str(e), None
|
88 |
|
89 |
def transcribe_and_chat(audio):
|
90 |
+
if audio is None:
|
91 |
+
return "Sorry, no audio was provided. Please try recording again.", None
|
92 |
+
|
93 |
text = whisper_speech_to_text(audio)
|
94 |
if not text:
|
95 |
+
return "Sorry, I couldn't understand the audio or there was an error in transcription. Please try again.", None
|
96 |
|
97 |
response, audio_path = asyncio.run(chat_with_ai(text))
|
98 |
return response, audio_path
|
|
|
110 |
audio_output = gr.Audio(label="AI Voice Response", autoplay=True)
|
111 |
|
112 |
def process_audio(audio):
|
113 |
+
logging.info(f"Received audio: {audio}")
|
114 |
+
if audio is None:
|
115 |
+
return "No audio detected. Please try recording again.", None, None
|
116 |
response, audio_path = transcribe_and_chat(audio)
|
117 |
+
logging.info(f"Response: {response}, Audio path: {audio_path}")
|
118 |
return response, audio_path, None # Return None to clear the audio input
|
119 |
|
120 |
demo.load(None, js="""
|