import base64
import json
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.withhopper.com/v1",
api_key=os.environ["HOPPER_API_KEY"],
)
audio = base64.b64encode(open("call.wav", "rb").read()).decode()
response = client.chat.completions.create(
model="audio-perception-1",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Classify the primary expressed emotion in this clip."},
{"type": "input_audio", "input_audio": {"data": audio, "format": "wav"}},
],
}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "emotion",
"strict": True,
"schema": {
"type": "object",
"properties": {
"primary_emotion": {"enum": ["happy", "neutral", "sad"]},
"confidence": {"type": "number"},
"evidence": {"type": "string"},
},
"required": ["primary_emotion", "confidence", "evidence"],
},
},
},
)
print(json.loads(response.choices[0].message.content))