-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
136 lines (109 loc) Β· 4.24 KB
/
Copy pathexample_usage.py
File metadata and controls
136 lines (109 loc) Β· 4.24 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Example script showing how to use the AI Voice Detection API
"""
import requests
import base64
import json
def detect_voice_from_file(audio_file_path, api_url, api_key):
"""
Detect if a voice in an audio file is AI-generated or human
Args:
audio_file_path: Path to MP3 audio file
api_url: API endpoint URL (e.g., https://your-app.onrender.com/detect-voice)
api_key: Your API key
Returns:
dict: API response with classification and confidence
"""
# Read and encode the audio file to base64
with open(audio_file_path, 'rb') as audio_file:
audio_bytes = audio_file.read()
audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
# Prepare the request
headers = {
'X-API-Key': api_key,
'Content-Type': 'application/json'
}
payload = {
'audio_base64': audio_base64
}
# Send POST request
response = requests.post(api_url, json=payload, headers=headers)
# Check if request was successful
if response.status_code == 200:
return response.json()
else:
return {
'error': f'Request failed with status code {response.status_code}',
'details': response.text
}
# Example usage
if __name__ == '__main__':
# Configuration
API_URL = "https://your-app-name.onrender.com/detect-voice" # Replace with your actual URL
API_KEY = "HCL_GUVI_2024_VOICE_DETECTION_KEY"
AUDIO_FILE = "sample_audio.mp3" # Replace with your audio file
print("π€ AI Voice Detection API - Example Usage\n")
print(f"Testing with audio file: {AUDIO_FILE}")
print(f"API Endpoint: {API_URL}\n")
try:
# Detect voice
result = detect_voice_from_file(AUDIO_FILE, API_URL, API_KEY)
# Display results
print("="*60)
print("RESULTS")
print("="*60)
print(json.dumps(result, indent=2))
print("="*60)
# Interpret results
if 'classification' in result:
classification = result['classification']
confidence = result['confidence_score']
explanation = result['explanation']
print(f"\nπ Classification: {classification}")
print(f"π― Confidence: {confidence:.1%}")
print(f"π‘ Explanation: {explanation}")
if classification == "AI_GENERATED":
print("\nβ οΈ This voice appears to be AI-generated")
else:
print("\nβ
This voice appears to be human")
except FileNotFoundError:
print(f"β Error: Audio file '{AUDIO_FILE}' not found!")
print("Please provide a valid MP3 file path")
except Exception as e:
print(f"β Error: {str(e)}")
# Example for batch processing multiple files
def batch_detect_voices(audio_files, api_url, api_key):
"""
Process multiple audio files
"""
results = {}
for audio_file in audio_files:
print(f"Processing {audio_file}...")
result = detect_voice_from_file(audio_file, api_url, api_key)
results[audio_file] = result
return results
# Example for testing all 5 languages
def test_multilingual():
"""
Example showing how to test with different language samples
"""
API_URL = "https://your-app-name.onrender.com/detect-voice"
API_KEY = "HCL_GUVI_2024_VOICE_DETECTION_KEY"
language_samples = {
'Tamil': 'sample_tamil.mp3',
'English': 'sample_english.mp3',
'Hindi': 'sample_hindi.mp3',
'Malayalam': 'sample_malayalam.mp3',
'Telugu': 'sample_telugu.mp3'
}
print("π Testing Multi-Language Support\n")
for language, audio_file in language_samples.items():
try:
result = detect_voice_from_file(audio_file, API_URL, API_KEY)
classification = result.get('classification', 'UNKNOWN')
confidence = result.get('confidence_score', 0)
print(f"{language:12} - {classification:14} (Confidence: {confidence:.1%})")
except FileNotFoundError:
print(f"{language:12} - Sample file not found")
except Exception as e:
print(f"{language:12} - Error: {str(e)}")