
#  Identifying audio language

```
Exercise ID 1590075
```

##  Assignment 

You've learned that you're not only limited to creating a single request, and that you can actually feed the output of one model as an input to another! This is called **chaining**, and it opens to the doors to more complex, multi-modal use cases.

In this exercise, you'll practice model chaining to identify the language used in an audio file. You'll do this by bringing together OpenAI's audio transcription functionality and its text models with only a few lines of code.

##  Pre exercise code 

```
import shutil
import openai

# Copy file so learners don't need to add directory path
shutil.copy("/usr/local/share/datasets/arne-german-automotive-forecast.wav", "audio.wav")
```



##  Instructions 

- Assign your API key to `openai.api_key`.
- Open the [`audio.wav`](https://assets.datacamp.com/production/repositories/6309/datasets/f214c0ab90fb25d9f532a9327f15c4112d8c03f5/arne-german-automotive-forecast.wav) file and assign to `audio_file`.
- Create a transcript from `audio_file` and assign to `transcript`.
- Prompt a text model using the text from `transcript` to discover the language used in `audio.wav`; print the model's response.



```
# Set your API key
openai.api_key = "____"

# Open the audio.wav file
audio_file = ____(____, "rb")

# Create a transcription request using audio_file
audio_response = ____

# Create a request to the API to identify the language spoken
chat_response = ____
print(____)
```

##  Hints 

- The `open()` function can be used to open files in Python.
- Use `openai.Audio.transcribe()` to transcribe the audio file.
- You can use either `Completion` or the `ChatCompletion` endpoints to determine the language used in the transcript.



##  Solution 

```
# Set your API key
openai.api_key = "<OPENAI_API_TOKEN>"

# Open the audio.wav file
audio_file = open("audio.wav", "rb")

# Create a transcription request using audio_file
audio_response = openai.Audio.transcribe("whisper-1", audio_file)

# Create a request to the API to identify the language spoken
chat_response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a languages specialist."},
    {"role": "user", "content": "Identify the language used in the following text: " + audio_response["text"]}
  ]
)
print(chat_response["choices"][0]["message"]["content"])
```


