Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,8 @@ import torch
|
|
| 5 |
import spaces
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
@spaces.GPU(duration=120)
|
| 12 |
-
def transcribe_and_respond(audio_file, chat_history):
|
| 13 |
try:
|
| 14 |
pipe = transformers.pipeline(
|
| 15 |
model='sarvamai/shuka_v1',
|
|
@@ -21,54 +18,35 @@ def transcribe_and_respond(audio_file, chat_history):
|
|
| 21 |
# Load the audio file
|
| 22 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
print(f"Audio dtype: {audio.dtype}, Audio shape: {audio.shape}, Sample rate: {sr}")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
# Debug: Print the
|
| 32 |
-
print(f"
|
| 33 |
|
| 34 |
-
# Call the model with the
|
| 35 |
output = pipe({'audio': audio, 'turns': turns, 'sampling_rate': sr}, max_new_tokens=512)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
turns.append({'role': 'system', 'content': output})
|
| 39 |
-
|
| 40 |
-
# Debug: Print the model's response
|
| 41 |
print(f"Model output: {output}")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
chat_history_for_display = []
|
| 45 |
-
for turn in turns:
|
| 46 |
-
if turn['role'] == 'user':
|
| 47 |
-
chat_history_for_display.append(("User", "🗣️ (Spoken Audio)"))
|
| 48 |
-
else:
|
| 49 |
-
chat_history_for_display.append(("AI", turn['content']))
|
| 50 |
-
|
| 51 |
-
return chat_history_for_display, turns # Return the formatted chat history for display and the updated history
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
return f"Error: {str(e)}"
|
| 55 |
|
| 56 |
-
# Define the Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=transcribe_and_respond,
|
| 59 |
-
inputs=
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.Chatbot(label="Conversation History"), # Display the conversation
|
| 65 |
-
gr.State([]) # Hidden state to keep track of the updated conversation history
|
| 66 |
-
],
|
| 67 |
-
title="Shuka demo",
|
| 68 |
-
description="shuka live demo",
|
| 69 |
-
live=True, # Enable live mode for real-time interaction
|
| 70 |
-
allow_flagging="auto",
|
| 71 |
-
# enable_queue=True
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import spaces
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
@spaces.GPU(duration=20)
|
| 9 |
+
def transcribe_and_respond(audio_file):
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
pipe = transformers.pipeline(
|
| 12 |
model='sarvamai/shuka_v1',
|
|
|
|
| 18 |
# Load the audio file
|
| 19 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 20 |
|
| 21 |
+
# Print audio properties for debugging
|
| 22 |
print(f"Audio dtype: {audio.dtype}, Audio shape: {audio.shape}, Sample rate: {sr}")
|
| 23 |
|
| 24 |
+
turns = [
|
| 25 |
+
{'role': 'system', 'content': 'Respond naturally and informatively.'},
|
| 26 |
+
{'role': 'user', 'content': '<|audio|>'}
|
| 27 |
+
]
|
| 28 |
|
| 29 |
+
# Debug: Print the initial turns
|
| 30 |
+
print(f"Initial turns: {turns}")
|
| 31 |
|
| 32 |
+
# Call the model with the audio and prompt
|
| 33 |
output = pipe({'audio': audio, 'turns': turns, 'sampling_rate': sr}, max_new_tokens=512)
|
| 34 |
|
| 35 |
+
# Debug: Print the final output from the model
|
|
|
|
|
|
|
|
|
|
| 36 |
print(f"Model output: {output}")
|
| 37 |
|
| 38 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
+
return f"Error: {str(e)}"
|
| 42 |
|
|
|
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=transcribe_and_respond,
|
| 45 |
+
inputs=gr.Audio(sources="microphone", type="filepath"),
|
| 46 |
+
outputs="text",
|
| 47 |
+
title="Live Transcription and Response",
|
| 48 |
+
description="Speak into your microphone, and the model will respond naturally and informatively.",
|
| 49 |
+
live=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|