Spaces:
Runtime error
Runtime error
File size: 4,478 Bytes
f7af1db 978a6ce 8aec16e 978a6ce 8aec16e 978a6ce f7af1db 978a6ce 0e04908 978a6ce 8aec16e 978a6ce 4d5d3b7 978a6ce 784383b 978a6ce 0e04908 978a6ce f7af1db 0e04908 978a6ce 8aec16e 978a6ce f7af1db 978a6ce 4d5d3b7 8aec16e 978a6ce 4d5d3b7 978a6ce f7af1db 0e04908 8aec16e 784383b 978a6ce 0e04908 978a6ce f7af1db 978a6ce f7af1db 363bda3 f7af1db 978a6ce 8aec16e 978a6ce f7af1db 978a6ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import os
from anthropic import Anthropic
import gradio as gr
# ... (your existing imports)
class ClinicalVoiceAnalyzer:
def __init__(self):
# Initialize without the API key first
self.anthropic = None
self.model = "claude-3-opus-20240229"
self.api_key = os.getenv('ANTHROPIC_API_KEY')
# Reference ranges remain the same
self.reference_ranges = {
'pitch': {'min': 150, 'max': 400},
'tempo': {'min': 90, 'max': 130},
'energy': {'min': 0.01, 'max': 0.05}
}
# Initialize Anthropic client if API key is available
self._initialize_anthropic()
def _initialize_anthropic(self):
"""Safely initialize the Anthropic client"""
try:
if self.api_key:
self.anthropic = Anthropic(api_key=self.api_key)
print("Anthropic client initialized successfully")
else:
print("Warning: ANTHROPIC_API_KEY not found in environment variables")
except Exception as e:
print(f"Error initializing Anthropic client: {str(e)}")
self.anthropic = None
def generate_clinical_analysis(self, voice_features):
"""Generate clinical analysis with fallback behavior"""
if not self.anthropic:
return self._generate_fallback_analysis(voice_features), {}
try:
prompt = self._construct_analysis_prompt(voice_features)
response = self.anthropic.messages.create(
model=self.model,
max_tokens=1000,
messages=[{
"role": "user",
"content": prompt
}]
)
return response.content, self._parse_clinical_response(response.content)
except Exception as e:
print(f"Error in clinical analysis: {str(e)}")
return self._generate_fallback_analysis(voice_features), {}
def _generate_fallback_analysis(self, features):
"""Generate basic analysis when Anthropic API is unavailable"""
pitch_status = "elevated" if features['pitch_mean'] > self.reference_ranges['pitch']['max'] else "normal"
tempo_status = "elevated" if features['tempo'] > self.reference_ranges['tempo']['max'] else "normal"
return f"""Basic Voice Analysis:
Pitch Analysis: {pitch_status} ({features['pitch_mean']:.2f} Hz)
Speech Rate: {tempo_status} ({features['tempo']:.2f} BPM)
Energy Level: {features['energy_mean']:.4f}
Note: This is a basic analysis. For detailed clinical interpretation, please ensure the Anthropic API key is configured."""
# ... (rest of your ClinicalVoiceAnalyzer methods remain the same)
# Modified analyze_audio function
def analyze_audio(audio_input):
try:
# Your existing audio processing code...
# Initialize clinical analyzer with graceful fallback
clinical_analyzer = ClinicalVoiceAnalyzer()
clinical_analysis, clinical_insights = clinical_analyzer.generate_clinical_analysis(features)
# Create enhanced summary
summary = f"""Voice Analysis Summary:
Speech Content:
{transcription}
Voice Characteristics:
- Average Pitch: {features['pitch_mean']:.2f} Hz
- Pitch Variation: {features['pitch_std']:.2f} Hz
- Speech Rate (Tempo): {features['tempo']:.2f} BPM
- Voice Energy: {features['energy_mean']:.4f}
Dominant Emotion: {max(emotion_scores.items(), key=lambda x: x[1])[0]}
Clinical Analysis:
{clinical_analysis}
"""
return summary, emotion_viz, feature_viz, clinical_insights
except Exception as e:
error_msg = f"Error in audio analysis: {str(e)}"
print(error_msg)
return error_msg, None, None, None
# ... (rest of your existing code)
# Modified Gradio interface
demo = gr.Interface(
fn=analyze_audio,
inputs=gr.Audio(
sources=["microphone", "upload"],
type="filepath",
label="Audio Input"
),
outputs=[
gr.Textbox(label="Analysis Summary", lines=15),
gr.HTML(label="Emotion Analysis"),
gr.HTML(label="Voice Feature Analysis"),
gr.JSON(label="Clinical Insights")
],
title="Advanced Voice Analysis System",
description="""This system provides comprehensive voice analysis with clinical interpretation.
Upload an audio file or record directly through your microphone."""
) |